【问题标题】:How to perform union of 2 lists?如何执行 2 个列表的联合?
【发布时间】:2013-03-20 06:36:22
【问题描述】:

我有两个 ArrayList ar1 和 ar2。

ArrayList ar1 包含一个对象列表,每个对象都具有属性 ID、NAME 和 STATUS。 ArrayList ar2 包含一个对象列表,每个对象都具有属性 ID、NAME 和 SUBJECT。 ar1 和 ar2 大小相同。

有什么方法可以将这两个列表合并成一个新列表 ar3,其中包含一个对象列表,每个对象都具有属性 ID、NAME、STATUS 和 SUBJECT?

更新:两个列表中的 ID 和 NAME 相同。

【问题讨论】:

  • 您必须编写代码来遍历这两个列表并创建第三个。
  • 1.为您的自定义对象添加一个好的 hashcode()equals。 2. 使用addAll() 将两者都放入HashSet 3. ???? 4 利润
  • 使用第三方库有什么限制吗?
  • commons.io 有一个工具。
  • @user2077648: commons.apache.org/proper/commons-collections/apidocs/org/…, java.util.List) 但是,在 cmets 中,您明确表示不允许重复,因此您必须将结果放入一个集合中删除重复项。

标签: java data-structures collections arraylist


【解决方案1】:

使用比较器 C 的搜索功能。

定义比较器如下

if(obja.id==objb.id & obja.name=objb.name) return (a==b);

因此,对于 listA 中的每个元素,找到 listB 中的元素,然后框住新对象并添加

listC 中的那个。

【讨论】:

    【解决方案2】:

    所以,如果我理解正确,您有一个 As 列表和一个相同大小的 Bs 列表,并且您想要一个 Cs 列表,其中第 n 个元素是列表 a 的第 n 个元素和列表 B 的第 n 个元素。

    嗯,首先你需要定义你的联合类:

    class C {
        Integer id;
        String name;
        String status;
        String subject;
    
        C(A a, B b) {
            this.id = a.id;
            this.name = a.name;
            this.status = a.status;
            this.subject = b.subject;
        }
    }
    

    然后,您可以使用迭代器:

    Iterator<A> aIterator = aList.iterator();
    Iterator<B> bIterator = bList.iterator();
    List<C> cList = new ArrayList(aList.size());
    while (aIterator.hasNext() && bIterator.hasNext()) {
        A a = aIterator.next();
        B b = bIterator.next();
        cList.add(new C(a, b));
    }
    

    【讨论】:

    • 不检查重复对象。
    • 除非我误解了问题,否则没有必要。
    【解决方案3】:
    public static void main(String[] args) throws Exception {
    
        List<String> list1 = new ArrayList<String>(Arrays.asList("A", "B", "C"));
        List<String> list2 = new ArrayList<String>(Arrays.asList("B", "C", "D",
                "E", "F"));
        List<String> result = new ArrayList<String>();
        result = union(list1, list2);
    
        System.out.println(result);
    }
    
    public static List<String> union(List<String> list1, List<String> list2) {
        HashSet<String> set = new HashSet<String>();
    
        set.addAll(list1);
        set.addAll(list2);
    
        return new ArrayList<String>(set);
    }
    

    输出:

    [D, E, F, A, B, C]
    

    【讨论】:

    • 列表中的对象是不同的类型。
    • @MikeRylander 是的,之前的问题只是关于如何合并两个列表。
    【解决方案4】:
    Map<String, Target> map = new HashMap<>();
    
    for (TypeWithStatus item : typesWithStatus) {
       map.put(item.getId()+item.getName(), createTypeWithStatusAndSubject(item));
    }
    
    for (TypeWithSubject item : typesWithSubject) {
       map.get(item.getId()+item.getName()).setSubject(item.getSubject());
    }
    

    这个想法是将第一个列表中的所有元素存储在地图中,并在第二次运行中更新地图值。这仅在两个列表都包含 id+name 的“相同”项目时才有效。如果没有,则必须添加空检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      相关资源
      最近更新 更多