Java8ã§æ©è½æ¡å¼µãããjava.util.Comparator*1ã¤ã³ã¿ãã§ã¼ã¹ã«ã¤ãã¦ã¡ã¢ãã³ã³ãã¬ã¼ã¿ãªãã¸ã§ã¯ããçæã»åæããstaticï¼defaultã¡ã½ããã追å ãããã
ã¡ã½ãã | æ©è½ |
---|---|
naturalOrder() | ï¼èªç¶é åºä»ãï¼ã³ã³ãã¬ã¼ã¿ãçæ |
reverseOrder() | ï¼èªç¶é åºä»ãï¼éé ã³ã³ãã¬ã¼ã¿ãçæ |
comparing(e) | ã½ã¼ããã¼(Uå)æ½åºé¢æ°eãæå®ããã³ã³ãã¬ã¼ã¿ãçæ |
comparing(e,u) | ã½ã¼ããã¼(Uå)æ½åºé¢æ°eã¨(Uå)ã³ã³ãã¬ã¼ã¿uãæå®ããã³ã³ãã¬ã¼ã¿ãçæ |
comparingInt(e) | ã½ã¼ããã¼(intå)æ½åºé¢æ°eãæå®ããã³ã³ãã¬ã¼ã¿ãçæ |
comparingLong(e) | ã½ã¼ããã¼(longå)æ½åºé¢æ°eãæå®ããã³ã³ãã¬ã¼ã¿ãçæ |
comparingDouble(e) | ã½ã¼ããã¼(doubleå)æ½åºé¢æ°eãæå®ããã³ã³ãã¬ã¼ã¿ãçæ |
c.thenComparing(t) | (Tå)ã³ã³ãã¬ã¼ã¿tã追å æ¥ç¶ããè¾æ¸å¼é åºä»ãã³ã³ãã¬ã¼ã¿ãè¿ã |
c.thenComparing(e) | ã½ã¼ããã¼(Uå)æ½åºeãæå®ããè¾æ¸å¼é åºä»ãã³ã³ãã¬ã¼ã¿ãè¿ã |
c.thenComparing(e,u) | ã½ã¼ããã¼(Uå)æ½åºé¢æ°eã¨(Uå)ã³ã³ãã¬ã¼ã¿uãæå®ããè¾æ¸å¼é åºä»ãã³ã³ãã¬ã¼ã¿ãè¿ã |
c.thenComparingInt(e) | ã½ã¼ããã¼(intå)æ½åºé¢æ°eãæå®ããè¾æ¸å¼é åºä»ãã³ã³ãã¬ã¼ã¿ãè¿ã |
c.thenComparingLong(e) | ã½ã¼ããã¼(longå)æ½åºé¢æ°eãæå®ããè¾æ¸å¼é åºä»ãã³ã³ãã¬ã¼ã¿ãè¿ã |
c.thenComparingDouble(e) | ã½ã¼ããã¼(doubleå)æ½åºé¢æ°eãæå®ããè¾æ¸å¼é åºä»ãã³ã³ãã¬ã¼ã¿ãè¿ã |
nullsFirst(c) | nullãæå°å¤ã¨ã¿ãªãã³ã³ãã¬ã¼ã¿ãè¿ã |
nullsLast(c) | nullãæ大å¤ã¨ã¿ãªãã³ã³ãã¬ã¼ã¿ãè¿ã |
c.reversed() | é åºå転ãããã³ã³ãã¬ã¼ã¿ãè¿ã |
ãã¼ãï¼èªç¶é åº(natural order)ï¼java.lang.Comparable
ã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããã¯ã©ã¹ãè¾æ¸å¼é åº(lexicographic order)ï¼wikipedia:ja:è¾æ¸å¼é åº
ä¾1ï¼éé ã½ã¼ã
// Java8 String[] a = /*...*/; Arrays.sort(a, Comparator.reverseOrder());
// Java7以å Arrays.sort(a, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } });
ä¾2ï¼è¾æ¸å¼é åºä»ã
// Java8 class Person { //... public String getName(); public String getGender(); public int getAge(); }; Person[] people = /*...*/; // Age->Name->Gender ã®è¾æ¸å¼é åºã§ã½ã¼ã Arrays.sort(people, Comparator.comparingInt(Person::getAge) .thenComparing(Person::getName) .thenComparing(Person::getGender));
// Java7以å Arrays.sort(people, new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { int ret; // NOTE: Integer#compare(int,int)ã¯Java7以é ret = Integer.compare(o1.getAge(), o2.getAge()); if (ret != 0) return ret; ret = o1.getName().compareTo(o2.getName()); if (ret != 0) return ret; return o1.getGender().compareTo(o2.getGender()); } });
ä¾3ï¼null許容
// Java8 class Item { //... public String getCode(); // énullä¿è¨¼ public String getSubCode(); // null許容 }; Item[] items = /*...*/; // Code->SubCode(nullã¯æ大å¤) ã®è¾æ¸å¼é åºã§ã½ã¼ã Arrays.sort(items, Comparator.comparing(Item::getCode) .thenComparing(Item::getSubCode, Comparator.nullsLast(Comparator.naturalOrder())));
// Java7以å Arrays.sort(items, new Comparator<Item>() { @Override public int compare(Item o1, Item o2) { int ret; ret = o1.getCode().compareTo(o2.getCode()); if (ret != 0) return ret; String subCode1 = o1.getSubCode(); String subCode2 = o2.getSubCode(); if (subCode1 == null) { if (subCode2 == null) ret = 0; // null == null else ret = 1; // null > o2 } else { if (subCode2 == null) ret = -1; // o1 < null else ret = subCode1.compareTo(subCode2); } return ret; } });
é¢é£URL