2 ways:
- Use
TreeSet
with Comparable
pojo.
- Use
TreeSet
with customized Comparator
.
Code
Tmp.java
(Use TreeSet
with Comparable
pojo.)
import java.util.*;
public class Tmp {
static class StudentMaster implements Comparable<StudentMaster> {
private Integer id;
private String master;
public StudentMaster(Integer id, String master) {
this.id = id;
this.master = master;
}
@Override
public int compareTo(StudentMaster other) {
int masterFlag = master.compareTo(other.master);
return (masterFlag == 0) ? id.compareTo(other.id) : masterFlag;
}
@Override
public boolean equals(Object o) {
StudentMaster osm = (StudentMaster) o;
return id == osm.id && master.equals(osm.master);
}
@Override
public int hashCode() {
return Objects.hash(id, master);
}
public String toString() {
StringBuilder sb = new StringBuilder();
Formatter fm = new Formatter(sb);
fm.format("id = %d, master = %s\n", id, master);
fm.close();
return sb.toString();
}
}
public static void test() {
final Set<StudentMaster> smSet = new TreeSet<>();
smSet.add(new StudentMaster(146, "Sweden"));
smSet.add(new StudentMaster(148, "Sweden"));
smSet.add(new StudentMaster(110, "Orebro"));
smSet.add(new StudentMaster(6, "Malmo"));
smSet.add(new StudentMaster(14, "Orebro"));
smSet.add(new StudentMaster(26, "Malmo"));
for (StudentMaster sm : smSet) {
System.out.print(sm);
}
}
public static void main(String[] args) {
test();
}
}
TmpComparator.java
(Use TreeSet
with customized Comparator
.)
import java.util.*;
public class TmpComparator {
static Comparator<StudentMaster> smc = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
StudentMaster sm1 = (StudentMaster) o1, sm2 = (StudentMaster) o2;
int masterFlag = sm1.master.compareTo(sm2.master);
return (masterFlag == 0) ? sm1.id.compareTo(sm2.id) : masterFlag;
}
};
static class StudentMaster {
private Integer id;
private String master;
public StudentMaster(Integer id, String master) {
this.id = id;
this.master = master;
}
@Override
public boolean equals(Object o) {
StudentMaster osm = (StudentMaster) o;
return id == osm.id && master.equals(osm.master);
}
@Override
public int hashCode() {
return Objects.hash(id, master);
}
public String toString() {
StringBuilder sb = new StringBuilder();
Formatter fm = new Formatter(sb);
fm.format("id = %d, master = %s\n", id, master);
fm.close();
return sb.toString();
}
}
public static void test() {
final Set<StudentMaster> smSet = new TreeSet<>(smc);
smSet.add(new StudentMaster(146, "Sweden"));
smSet.add(new StudentMaster(148, "Sweden"));
smSet.add(new StudentMaster(110, "Orebro"));
smSet.add(new StudentMaster(6, "Malmo"));
smSet.add(new StudentMaster(14, "Orebro"));
smSet.add(new StudentMaster(26, "Malmo"));
for (StudentMaster sm : smSet) {
System.out.print(sm);
}
}
public static void main(String[] args) {
test();
}
}
Just run the main()
method.
Output of both are the same:
id = 6, master = Malmo
id = 26, master = Malmo
id = 14, master = Orebro
id = 110, master = Orebro
id = 146, master = Sweden
id = 148, master = Sweden
Tips
- In production code, the
equals()
need to be improved, this is a simplified version for test only.