gpt4 book ai didi

Java 设置奇怪的行为

转载 作者:行者123 更新时间:2023-12-01 05:05:16 25 4
gpt4 key购买 nike

我遇到了一个问题,我需要根据某些条件更改排序集的可比较值。

做这样的事情:

SortedSet<T> groups;

for(T t: groups){
t.setOrdinal(max);
}

不会自动重新排列 SortedSet。

在线阅读我发现我需要从集合中删除该对象,然后再次添加它。显然,我无法在迭代集合时做到这一点。所以我决定制作一个集合的Arraylist。将集合设为空,然后再次添加所有元素,以便它们遵循排序顺序。我做了这样的事情:

SortedSet 组;

List<T> groupList = new ArrayList<T>(groups);
groups = null;
for(T t: groupList){
t.setOrdinal(max);
}

groups = new TreeSet<T>(groupList);

但是当我检查组集时,它没有遵循基于比较对象 T 序数的比较器的排序顺序

但是当我尝试这样的事情时:

SortedSet 组;

List<T> groupList = new ArrayList<T>(groups);
groups.clear();
for(T t: groupList){
t.setOrdinal(max);
}

groups.addAll(groupList);

我得到了预期的结果。有人可以解释一下发生了什么事吗?

这就是我实现类 T 的方式

public class T implements Serializable, Comparable<T> {
//
int ordinal;
//getter
//setter

@Override
public int compareTo(T that) {
if (that == null) {
return 1;
}

return this.ordinal - that.ordinal;
}

}

对于那些想查看完整程序的人:

List<SmartUser> groupsList = new ArrayList<SmartUser>(groups);
groups = null;
for (SmartUser smartUser : groupsList) {
if (smartUser.isExpired()) {
smartUser.setOrdinal(Long.MAX_VALUE);
}
SmartUserQuery smartUserQuery = smartUser.getSmartUserQuery();
if (smartUserQuery != null) {
//Do parallel processing: of each samrtUser
}
}

groups = new TreeSet<SmartUser>groupsList;

正确的结果给出方法:

List<SmartUser> groupsList = new ArrayList<SmartUser>(groups);
groups.clear();
for (SmartUser smartUser : groupsList) {
if (smartUser.isExpired()) {
smartUser.setOrdinal(Long.MAX_VALUE);
}
SmartUserQuery smartUserQuery = smartUser.getSmartUserQuery();
if (smartUserQuery != null) {
//Do parallel processing: of each samrtUser
}
}

groups.addAll(groupsList);

谢谢。

最佳答案

将compareTo方法更改为以下

@Override
public int compareTo(CustomObject o) {
return Integer.valueOf(o.ordinal).compareTo(ordinal );
}

此外,根据 TreeSet 合约,您应该提供 equals 方法,该方法将与compareTo 方法同步

 @Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof CustomObject))
return false;
CustomObject o = (CustomObject) obj;

return this.ordinal == o.ordinal;
}

示例实现

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class CustomObject implements Comparable<CustomObject> {

private int ordinal = 0;

public CustomObject(int priority) {
this.ordinal = priority;
}

/**
* @param args
*/
public static void main(String[] args) {

List<CustomObject> list = new ArrayList<CustomObject>();
list.add(new CustomObject(1));
list.add(new CustomObject(2));
list.add(new CustomObject(3));
list.add(new CustomObject(6));
list.add(new CustomObject(4));
System.out.println("Before: " + list);
for (CustomObject object : list) {
if (object.ordinal == 4) {
object.ordinal = 10;
}
}
Set<CustomObject> set = new TreeSet<CustomObject>();
set.addAll(list);
System.out.println("After: " + set);

}

@Override
public int compareTo(CustomObject o) {
return Integer.valueOf(o.ordinal).compareTo(ordinal);
}

@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof CustomObject))
return false;
CustomObject o = (CustomObject) obj;

return this.ordinal == o.ordinal;
}

@Override
public String toString() {
return " Ordinal Value is :" + ordinal;
}

}

关于Java 设置奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com