たま

技術的なメモ置き場。主にJava。

【Java】識別子

Javaの識別子(変数、メソッド名、クラス名など)には、以下の規則がある。

  • 予約語は使用不可
  • 数字から始められない
  • 下記の記号以外の記号は使用不可
    • アンダースコア
    • 通貨記号

通貨記号を使ってみると以下のようになる

int ¤ = 1; // 国際通貨記号
int ฿ = 2; // タイバーツ
int ¢ = 3; // セント
int € = 4; // ユーロ
int ₲ = 5; // パラグアイ・グアラニー
int ₺ = 6; // トルコリラ
int £ = 7; // ポンド
int ₩ = 8; // ウォン
int ¥ = 9; // 円

System.out.println(¤ + ฿ + ¢ + € + ₲ + ₺ + £ + ₩ + ¥); // 45

【Java】クラス宣言

Javaのクラス宣言は、 フィールドメソッド の2つから構成される。

パッケージ宣言、インポート宣言は含まれない。 これらはソースファイルに対する宣言であり、クラスに対するものではない。

// パッケージ -> クラス宣言ではない
package chapter01;

// インポート -> クラス宣言ではない
import java.util.ArrayList;

public class Hoge {

    // フィールド -> クラス宣言
    private int fuga;
    
    // コンストラクタ -> クラス宣言
    public Hoge(int fuga) {
        this.fuga = fuga;
    }
    
    // メソッド -> クラス宣言
    public void piyo() {
        // omit
    }
}

イニシャライザはクラス宣言に含まれるのだろうか?

Comparatorでソート

Comparatorを使用して、JavaBeanのリストをソートする。

使用するJavaBean

public class Student {
    private int id;
    private String name;
    private int age;
    // getter/setter
}

今回、Studentに設定するデータはJSONで定義する。 JSON -> Java Objectの変換は省略。

[{"id":1,"name":"Josy","age":32},
{"id":2,"name":"Remus","age":31},
{"id":3,"name":"Gavan","age":29},
{"id":4,"name":"Torrey","age":34},
{"id":5,"name":"Cass","age":35},
{"id":6,"name":"Hollis","age":23},
{"id":7,"name":"Gustav","age":29},
{"id":8,"name":"Trey","age":27},
{"id":9,"name":"Travis","age":27},
{"id":10,"name":"Lyndsie","age":34},
{"id":11,"name":"Erroll","age":32},
{"id":12,"name":"Martguerita","age":31},
{"id":13,"name":"Gisella","age":34},
{"id":14,"name":"Lenora","age":26},
{"id":15,"name":"Killian","age":23},
{"id":16,"name":"Samara","age":28},
{"id":17,"name":"Jo ann","age":20},
{"id":18,"name":"Betteann","age":32},
{"id":19,"name":"Catlaina","age":32},
{"id":20,"name":"Giovanna","age":30},
{"id":21,"name":"Sandy","age":22},
{"id":22,"name":"Del","age":29},
{"id":23,"name":"Bern","age":21},
{"id":24,"name":"Astrix","age":21},
{"id":25,"name":"Maible","age":26},
{"id":26,"name":"Marie-jeanne","age":34},
{"id":27,"name":"Anton","age":29},
{"id":28,"name":"Beryl","age":22},
{"id":29,"name":"Holli","age":23},
{"id":30,"name":"Timmy","age":30},
{"id":31,"name":"Dannie","age":26},
{"id":32,"name":"Berky","age":35},
{"id":33,"name":"Estell","age":24},
{"id":34,"name":"Orsola","age":29},
{"id":35,"name":"Ulysses","age":27},
{"id":36,"name":"Aldin","age":23},
{"id":37,"name":"Joan","age":25},
{"id":38,"name":"Nata","age":33},
{"id":39,"name":"Chrissy","age":23},
{"id":40,"name":"Giordano","age":28},
{"id":41,"name":"Michale","age":32},
{"id":42,"name":"Joell","age":31},
{"id":43,"name":"Hurleigh","age":31},
{"id":44,"name":"Cam","age":22},
{"id":45,"name":"Luise","age":25},
{"id":46,"name":"Saxe","age":28},
{"id":47,"name":"Irena","age":25},
{"id":48,"name":"Tarrah","age":34},
{"id":49,"name":"Claribel","age":21},
{"id":50,"name":"Myles","age":27}]

nameの昇順でソート

List<Student> students = // JSON -> Object変換

List<Student> byName = students
                .stream()
                .sorted(Comparator.comparing(Student::getName))
                .collect(Collectors.toList());
Student(id=36, name=Aldin, age=23)
Student(id=27, name=Anton, age=29)
Student(id=24, name=Astrix, age=21)
Student(id=32, name=Berky, age=35)
Student(id=23, name=Bern, age=21)
Student(id=28, name=Beryl, age=22)
Student(id=18, name=Betteann, age=32)
Student(id=44, name=Cam, age=22)
Student(id=5, name=Cass, age=35)
Student(id=19, name=Catlaina, age=32)
Student(id=39, name=Chrissy, age=23)
Student(id=49, name=Claribel, age=21)
...
省略
...
Student(id=9, name=Travis, age=27)
Student(id=8, name=Trey, age=27)
Student(id=35, name=Ulysses, age=27)

nameの降順でソート

reversed() を利用する。

List<Student> byName = students
                .stream()
                .sorted(Comparator.comparing(Student::getName).reversed())
                .collect(Collectors.toList());
Student(id=35, name=Ulysses, age=27)
Student(id=8, name=Trey, age=27)
Student(id=9, name=Travis, age=27)
Student(id=4, name=Torrey, age=34)
Student(id=30, name=Timmy, age=30)
Student(id=48, name=Tarrah, age=34)
...
省略
...
Student(id=44, name=Cam, age=22)
Student(id=18, name=Betteann, age=32)
Student(id=28, name=Beryl, age=22)
Student(id=23, name=Bern, age=21)
Student(id=32, name=Berky, age=35)
Student(id=24, name=Astrix, age=21)
Student(id=27, name=Anton, age=29)
Student(id=36, name=Aldin, age=23)

ageの降順、nameの昇順でソート

List<Student> byAgeAndName = students
                .stream()
                .sorted(Comparator.comparing(Student::getAge).reversed().thenComparing(Student::getName))
                .collect(Collectors.toList());
Student(id=32, name=Berky, age=35)
Student(id=5, name=Cass, age=35)
Student(id=13, name=Gisella, age=34)
Student(id=10, name=Lyndsie, age=34)
Student(id=26, name=Marie-jeanne, age=34)
Student(id=48, name=Tarrah, age=34)
Student(id=4, name=Torrey, age=34)
Student(id=38, name=Nata, age=33)
...
省略
...
Student(id=36, name=Aldin, age=23)
Student(id=39, name=Chrissy, age=23)
Student(id=29, name=Holli, age=23)
Student(id=6, name=Hollis, age=23)
Student(id=15, name=Killian, age=23)
Student(id=28, name=Beryl, age=22)
Student(id=44, name=Cam, age=22)
Student(id=21, name=Sandy, age=22)
Student(id=24, name=Astrix, age=21)
Student(id=23, name=Bern, age=21)
Student(id=49, name=Claribel, age=21)
Student(id=17, name=Jo ann, age=20)