来自ArrayList的ArrayList的唯一集

时间:2018-12-31 09:50:58

标签: java arraylist set

嗨,我有一个arraylist的arraylist格式如下:

[[val1, val2],[val3,val4],[val1,val2],[val1,val5]]

,并希望获得一组唯一的数组列表:

[[val1, val2],[val3,val4],[val1,val5]]

我尝试了以下方法:

Set<String> uniques = new HashSet<>();
    for (ArrayList<String> sublist : mappedEntities) {
        uniques.addAll(sublist);
    }

但是这会将内部arraylist的所有值合并在一起

5 个答案:

答案 0 :(得分:2)

可以使用Java 8 Collection Stream Distinct
返回Set数据类型:

Set<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toSet());

如果您想返回List

List<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toList());

答案 1 :(得分:2)

为什么不像这样简单地将它们放在Set中?

Set<List<String>> uniques = new HashSet<>(mappedEntities);

您的错误是您将内部列表展平,并将它们的项目分别放在集合中。

答案 2 :(得分:1)

这里的问题是,您需要使用{em> ArrayList Set的{​​{1}},但是您使用的是 Strings Set<ArrayList<String>>改为Set

给出列表:

Set<String>

您需要做的就是声明集合并使用addAll()

List<List<String>> mappedEntities = Arrays.asList(Arrays.asList("val1", "val2"), 
                                                  Arrays.asList("val3", "val4"),
                                                  Arrays.asList("val1", "val2"), 
                                                  Arrays.asList("val1", "val5"));

由于集合只能包含唯一值,因此不会将所有重复项添加到集合中(无需明确检查此)。您现在可以将其打印出来:

Set<List<String>> mySet = new HashSet<>();
mySet.addAll(mappedEntities);

或更简单地,使用列表mySet.forEach(System.out::println); 初始化 HashSet

mappedEntities

答案 3 :(得分:0)

尝试此代码:

public class B {
    public static void main(String[] args) throws Exception {
       List<List<String>> list= Arrays.asList(
               Arrays.asList("a","b","c"),
               Arrays.asList("a","b","c"),
               Arrays.asList("a","b","c","d"));

       Set<List<String>> uniques = new HashSet<>();
       for (List<String> sublist : list) {
          if(!uniques.contains(sublist))
          uniques.add(sublist);
       }
       System.out.println(uniques);
    }
}

输出:

[[a, b, c], [a, b, c, d]]

答案 4 :(得分:0)

我是STACKOVERFLOW的初学者,但我尝试解决您的问题
我想你想这样..

import java.util.*; 
public class GFG { 
    public static void main(String[] args) 
    { 
        int n = 3; 

        // Here aList is an ArrayList of ArrayLists 
        ArrayList<ArrayList<String> > aList = 
                new ArrayList<ArrayList<String> >(n); 

        // Create n lists one by one and append to the 
        // master list (ArrayList of ArrayList) 
        ArrayList<String> a1 = new ArrayList<String>(); 
        a1.add("1"); 
        a1.add("2"); 
        aList.add(a1); 

        ArrayList<String> a2 = new ArrayList<String>(); 
        a2.add("11"); 
        a2.add("22"); 
        aList.add(a2); 

        ArrayList<String> a3 = new ArrayList<String>(); 
        a3.add("1"); 
        a3.add("2"); 
        aList.add(a3); 


        Set<ArrayList<String>> uniques = new HashSet<ArrayList<String>>();
        for (ArrayList<String> sublist : aList) {
            uniques.add(sublist);
        }
        System.out.println("Your Answer"); 
        for (ArrayList<String> x : uniques) 
            System.out.println(x); 
    }


    }