Java Comparable, Comparator 차이
algorithms
featured
java
comparable
comparator
ordering
Intro
작성중
import java.math.BigDecimal;
import java.util.Set;
import java.util.SortedSet;
import java.util.HashSet;
import java.util.TreeSet;
class Adequality implements Comparable<Adequality> {
final String name;
Adequality(final String name) {
this.name = name;
}
@Override
public int compareTo(Adequality o) {
return 1;
}
@Override
public boolean equals(Object obj) {
return true;
}
@Override
public String toString() {
return "a";
}
public static void main(String[] args) {
final Adequality adequality = new Adequality("a");
final BigDecimal fourPointZero = new BigDecimal("4.0");
final BigDecimal fourPointDoubleZero = new BigDecimal("4.00");
final Set<Adequality> adequalitySet = new HashSet<>();
final Set<BigDecimal> bigDecimalSet = new HashSet<>();
final SortedSet<Adequality> adequalitySortedSet = new TreeSet<>();
final SortedSet<BigDecimal> bigDecimalSortedSet = new TreeSet<>();
final SortedSet<Adequality> nullableAdequalitySortedSet = new TreeSet<>((a, b) -> 1);
final SortedSet<BigDecimal> nullableBigDecimalSortedSet = new TreeSet<>((a, b) -> 1);
.add(adequality);
adequalitySet.add(adequality);
adequalitySet.add(fourPointZero);
bigDecimalSet.add(fourPointDoubleZero);
bigDecimalSetSystem.out.println(adequalitySet + "\t\t\t" + bigDecimalSet);
.add(adequality);
adequalitySortedSet.add(adequality);
adequalitySortedSet.add(fourPointZero);
bigDecimalSortedSet.add(fourPointDoubleZero);
bigDecimalSortedSetSystem.out.println(adequalitySortedSet + "\t\t\t" + bigDecimalSortedSet);
.add(adequality);
nullableAdequalitySortedSet.add(adequality);
nullableAdequalitySortedSet.add(fourPointZero);
nullableBigDecimalSortedSet.add(fourPointDoubleZero);
nullableBigDecimalSortedSetSystem.out.println(nullableAdequalitySortedSet + "\t\t\t" + nullableBigDecimalSortedSet);
.add(null);
adequalitySet.add(null);
adequalitySet.add(null);
bigDecimalSet.add(null);
bigDecimalSetSystem.out.println(adequalitySet + "\t\t" + bigDecimalSet);
try {
.add(null);
adequalitySortedSet} catch (NullPointerException e) {
System.out.println(e);
}
try {
.add(null);
bigDecimalSortedSet} catch (NullPointerException e) {
System.out.println(e);
}
.add(null);
nullableAdequalitySortedSet.add(null);
nullableAdequalitySortedSet.add(null);
nullableBigDecimalSortedSet.add(null);
nullableBigDecimalSortedSetSystem.out.println(nullableAdequalitySortedSet + "\t" + nullableBigDecimalSortedSet);
}
}
Citation
BibTeX citation:
@online{kee2024,
author = {Kee, Yunho},
title = {Java Comparable, Comparator 차이},
date = {2024-06-09},
url = {https://yhkee0404.github.io/posts/algorithms/java-comparable-comparator/},
langid = {ko}
}
For attribution, please cite this work as:
Kee, Yunho. 2024. “Java Comparable, Comparator 차이.” June
9, 2024. https://yhkee0404.github.io/posts/algorithms/java-comparable-comparator/.