gpt4 book ai didi

java - TreeSet 到 List 转换中的 Controller 排序

转载 作者:行者123 更新时间:2023-12-04 05:31:55 30 4
gpt4 key购买 nike

我的方法中有 2 个列表按特定顺序排序(根据 ID 字段升序或降序)

我有一个自定义比较器实现,它允许我将两个列表都放入树集中并获得所需的结果。

我的问题是加载树集后,我需要从该方法返回一个列表。我的第一个实现并不关心排序,所以我这样做了(复合是我命名的 TreeSet):

    composite.addAll(custom);
composite.addAll(reference);

Iterator<MyObject> anIter = composite.iterator();
ArrayList<MyObject> returnVal = new ArrayList<MyObject>();
while(anIter.hasNext())
returnVal.add(anIter.next());

执行此操作后,“自定义”和“引用”两个列表将恢复为默认顺序。 Collection 的 iterator() 方法状态的 Javadocs 将按升序返回列表,这可能是我的麻烦所在。

那么......有没有办法在保护原始列表顺序的同时返回 TreeSet 的内容?我想到了一个树集,因为我想在两个集合上使用比较器接口(interface)的强大功能,将它们与 addAll() 联合起来,并抛出欺骗。

任何有关保护排序的建议将不胜感激。

编辑 *
Set<MyObject> composite = new TreeSet<MyObject>(new Comparator<MyObject>(){
@Override
public int compare(MyObject arg0, MyObject arg1) {
int arg0ID = arg0.getObjID();
int arg1ID = arg1.getObjID();
if(arg0ID < arg1ID) return -1;
else if(arg0ID> arg1ID) return 1;
else return 0;
}
});

最佳答案

is there no way to return the content of a TreeSet while protecting the original list ordering?



不,当然不。 TreeSet是这项工作的错误工具。使用 TreeSet 的要点是它应用了 Comparator - 基于集合包含的对象的排序。使用不同的数据结构—— LinkedHashSet 怎么样? ? — 如果您想在保留插入顺序的同时删除重复的对象。

I don't see how your statement refutes my decision. I intentionally CHOSE the TreeSet because I wanted to exercise the comparator interface. ... My comparator let me identify what components of the object to filter on. I don't want to lose that capability



TreeSet :“元素使用它们的自然顺序或在集合创建时提供的 Comparator 进行排序,具体取决于使用的构造函数。”您不能使用 TreeSet 选择任何订单排序(尤其是像插入排序)。 .如果不能使用 equals()hashCode()要选择代表身份的对象字段,请使用带有 LinkedHashSet 的 decorate-dedup-undecorate 模式。 ,其中装饰器包含 equals()hashCode()实现。 Comparator/Comparable用于指定排序,而不是标识或相等。

You're suggesting I need to abandon it and move to something like LinkedHashSet?



对,就是这样。要么制作 MyObject实现 .equals().hashCode()你的意思是:
class MyObject {
// snip

@Override
public boolean equals(Object o) {
// boilerplate junk here
if (!o instanceof MyObject) return false;
MyObject other = (MyObject) o;
return this.getObjID() == other.getObjID();
}

@Override
public int hashCode() {
return this.getObjID();
}

// snip
}

像这样重复数据删除:
List<MyObject> custom = /* snip */;
List<MyObject> reference = /* snip */;

Set<MyObject> uniques = new LinkedHashSet<>(custom.size() + reference.size());
uniques.addAll(custom);
uniques.addAll(reference);
List<MyObject> deduped = new ArrayList<>(uniques);

或者使用我提到的 decorate-dedup-undecorate 模式。装饰器类看起来像这样:
class Decorator {
private final MyObject value;

public Decorator(MyObject value) {
this.value = value;
}

public MyObject getValue() {
return value;
}

@Override
public boolean equals(Object o) {
// boilerplate junk here
if (!o instanceof Decorator) return false;
Decorator other = (Decorator) o;
return this.value.getObjID() == other.value.getObjID();
}

@Override
public int hashCode() {
return this.value.getObjID();
}
}

这大致是你如何使用它:
List<MyObject> custom = /* snip */;
List<MyObject> reference = /* snip */;

Set<Decorator> uniques = new LinkedHashSet<>(custom.size() + reference.size());
for (MyObject m : custom) uniques.add(new Decorator(m));
for (MyObject m : reference) uniques.add(new Decorator(m));

List<MyObject> deduped = new ArrayList<>(uniques.size());
for (Decorator d : uniques) deduped.add(d.getValue());

关于java - TreeSet 到 List 转换中的 Controller 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12466326/

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