オブジェクトの順序付けについて ref: http://qiita.com/zakki/items/374cf659ba266da41bfb
ArrayListのインデックス順での表示
20 aya
10 kouta
50 kazuyuki
RankRuleで定義したidの昇順での表示
10 kouta
20 aya
50 kazuyuki
class Employee {
private String name;
private Integer id;
public Employee(String name, Integer id) {
this.name = name;
this.id = id;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
}
class RankRule implements Comparator<Employee> {
public int compare(Employee obj1, Employee obj2) {
return obj1.getId().compareTo(obj2.getId());
}
}
class Main {
public static void main(String[] args) {
Employee e1 = new Employee("aya", 20);
Employee e2 = new Employee("kouta", 10);
Employee e3 = new Employee("kazuyuki", 50);
ArrayList<Employee> personnelDepartment = new ArrayList<>();
personnelDepartment.add(e1);
personnelDepartment.add(e2);
personnelDepartment.add(e3);
System.out.println("ArrayListのインデックス順での表示");
print(personnelDepartment);
System.out.println("RankRuleで定義したidの昇順での表示");
Collections.sort(personnelDepartment, new RankRule());
print(personnelDepartment);
}
public static void print(ArrayList<Employee> ary) {
for (Employee obj : ary) {
System.out.println(obj.getId() + " " + obj.getName());
}
}
}
public interface Comparator<T> {
int compare(T o1, T o2);
}
数値としては等しいのでcompareTo()はtrue
数値としては等しいのでcompareTo()はtrue
スケールや精度が違うのでequals()はfalse
true
public class BigDecimalSample {
public static void main(String[] args) {
BigDecimal firstValue = new BigDecimal("1.0");
BigDecimal secondValue = new BigDecimal("1.00");
BigDecimal thirdValue = new BigDecimal("1.0");
// 数値としては等しいのでcompareTo()はtrueになる
if (firstValue.compareTo(secondValue) == 0) {
System.out.println("数値としては等しいのでcompareTo()はtrue");
}
if (firstValue.compareTo(thirdValue) == 0) {
System.out.println("数値としては等しいのでcompareTo()はtrue");
}
// スケールや精度が違うのでequals()はfalseになる
if (firstValue.equals(secondValue)) {
} else {
System.out.println("スケールや精度が違うのでequals()はfalse");
}
if (firstValue.equals(thirdValue)) {
System.out.println("true");
}
}
}
public class ComparableTest {
public static void main(String[] args) {
Set<String> stringTest = new HashSet<>();
String[] sample = {"b", "a"};
Collections.addAll(stringTest, sample);
for(String s : stringTest) {
System.out.println(s);
}
Set<Integer> integerTest = new HashSet<>();
Integer[] integer = {3, 1};
Collections.addAll(integerTest, integer);
for(Integer i : integerTest) {
System.out.println(i);
}
}
}
public int compareTo(Integer anotherInteger) {
return compare(this.value, anotherInteger.value);
}
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
class CaseSample{
public static void main(String[] args) {
List<String> list = Arrays.asList("B", "a", "123", "c");
// Collections.sort(list); // [123, B, a, c]
Collections.sort(list, String.CASE_INSENSITIVE_ORDER); // [123, a, B, c]
// Collections.sort(list, (s1, s2) -> s1.compareToIgnoreCase(s2)); // [123, a, B, c]
System.out.println(list);
}
}
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
public static final Comparator<String> CASE_INSENSITIVE_ORDER
= new CaseInsensitiveComparator();
private static class CaseInsensitiveComparator
implements Comparator<String>, java.io.Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 8575799808933029326L;
public int compare(String s1, String s2) {
int n1 = s1.length();
int n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2) {
// No overflow because of numeric promotion
return c1 - c2;
}
}
}
}
return n1 - n2;
}
/** Replaces the de-serialized object. */
private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
}
}
Collections.sort(personnelDepartment, new RankRule());
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
public interface Comparable<T> {
public int compareTo(T o);
}