在不删除重复项的情况下对树集进行排序
Sorting tree sets without removing duplicates
myData{
itemId,
location,
ExpDtTm,
userId
}
我试图像这样对 TreeSet 进行排序:
TreeSet<myData> sorted = new TreeSet<>(Comparator.comparing(myData -> myData.ExpDtTm));
但是如果任何日期相同,它们将不会被添加到树集中,这就会出现问题,这被证明是一个问题。如果有人知道该怎么做,我们将不胜感激。
我也试过了
TreeSet<myData> sorted = new TreeSet<>(Comparator.comparing(myData -> myData.ExpDtTm).thencomparing(myData -> myData.itemId));
但是除了两个 lambda 中的 myData 之外,它都停止了。
A TreeSet
是 Set
, which explicitly forbids duplicate entries by design. If you want a sorted collection that includes duplicates, use an implementation of List
(e.g. ArrayList
), and sort it at the end by calling Collections.sort(myList, Comparator.comparing(myData -> myData.ExpDtTm));
的实现。
发生这种情况的原因是因为比较器不仅用于确定顺序,还用于确定两个对象是否相等(而不是 hashCode
,HashSet
将使用):
a TreeSet instance performs all element comparisons using its
compareTo
(or compare
) method, so two elements that are deemed equal
by this method are, from the standpoint of the set, equal.
如果您有两个对象,实际上并不相同,只是碰巧具有相同的日期,您可以使用对象的 identity hash code(实际上是内存地址)作为次要排序条件:
TreeSet<myData> sorted =
new TreeSet<>(Comparator.comparing(myData -> myData.ExpDtTm)
.thenComparingInt(System::identityHashCode));
这就是我最后做的事情:
final SortedSet<myData> sorted = new TreeSet<>(new Comparator<myData>() {
@Override
public int compare(myData o1, myData o2) {
int comparedValue = o1.expDtTm.compareTo(o2.expDtTm);
if(comparedValue == 0){
comparedValue = 1;
}
}
});
在该 compareValue if
语句中,您可以添加任何其他您可能希望用于排序的变量。
Employee.Java
private String name;
public Employee(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public String toString() {
return "Employee[ id=" + name + "]";
}
Driver Class
TreeSetTest.Java
TreeSet<Employee> treeSet = new TreeSet<>(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
int index = o1.getName().compareTo(o2.getName());
return index==0?1:index;
}
});
treeSet.add(new Employee("Harvansh"));
treeSet.add(new Employee("Harvansh1"));
treeSet.add(new Employee("Harvansh"));
treeSet.add(new Employee("Harvansh2"));
treeSet.add(new Employee("Harvansh"));
treeSet.add(new Employee("Harvansh3"));
treeSet.add(new Employee("Harvansh"));
treeSet.forEach(System.out::println);
输出
员工[id=Harvansh]
员工[id=Harvansh]
员工[id=Harvansh]
员工[id=Harvansh]
员工[id=Harvansh1]
员工[id=Harvansh2]
员工[id=Harvansh3]
myData{
itemId,
location,
ExpDtTm,
userId
}
我试图像这样对 TreeSet 进行排序:
TreeSet<myData> sorted = new TreeSet<>(Comparator.comparing(myData -> myData.ExpDtTm));
但是如果任何日期相同,它们将不会被添加到树集中,这就会出现问题,这被证明是一个问题。如果有人知道该怎么做,我们将不胜感激。
我也试过了
TreeSet<myData> sorted = new TreeSet<>(Comparator.comparing(myData -> myData.ExpDtTm).thencomparing(myData -> myData.itemId));
但是除了两个 lambda 中的 myData 之外,它都停止了。
A TreeSet
是 Set
, which explicitly forbids duplicate entries by design. If you want a sorted collection that includes duplicates, use an implementation of List
(e.g. ArrayList
), and sort it at the end by calling Collections.sort(myList, Comparator.comparing(myData -> myData.ExpDtTm));
的实现。
发生这种情况的原因是因为比较器不仅用于确定顺序,还用于确定两个对象是否相等(而不是 hashCode
,HashSet
将使用):
a TreeSet instance performs all element comparisons using its
compareTo
(orcompare
) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal.
如果您有两个对象,实际上并不相同,只是碰巧具有相同的日期,您可以使用对象的 identity hash code(实际上是内存地址)作为次要排序条件:
TreeSet<myData> sorted =
new TreeSet<>(Comparator.comparing(myData -> myData.ExpDtTm)
.thenComparingInt(System::identityHashCode));
这就是我最后做的事情:
final SortedSet<myData> sorted = new TreeSet<>(new Comparator<myData>() {
@Override
public int compare(myData o1, myData o2) {
int comparedValue = o1.expDtTm.compareTo(o2.expDtTm);
if(comparedValue == 0){
comparedValue = 1;
}
}
});
在该 compareValue if
语句中,您可以添加任何其他您可能希望用于排序的变量。
Employee.Java
private String name;
public Employee(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public String toString() {
return "Employee[ id=" + name + "]";
}
Driver Class
TreeSetTest.Java
TreeSet<Employee> treeSet = new TreeSet<>(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
int index = o1.getName().compareTo(o2.getName());
return index==0?1:index;
}
});
treeSet.add(new Employee("Harvansh"));
treeSet.add(new Employee("Harvansh1"));
treeSet.add(new Employee("Harvansh"));
treeSet.add(new Employee("Harvansh2"));
treeSet.add(new Employee("Harvansh"));
treeSet.add(new Employee("Harvansh3"));
treeSet.add(new Employee("Harvansh"));
treeSet.forEach(System.out::println);
输出
员工[id=Harvansh]
员工[id=Harvansh]
员工[id=Harvansh]
员工[id=Harvansh]
员工[id=Harvansh1]
员工[id=Harvansh2]
员工[id=Harvansh3]