Intersection and union of ArrayLists in Java

后端 未结 24 2206
离开以前
离开以前 2020-11-22 06:05

Are there any methods to do so? I was looking but couldn\'t find any.

Another question: I need these methods so I can filter files. Some are AND filter

相关标签:
24条回答
  • 2020-11-22 06:37

    Intersection of two list of different object based on common key - Java 8

     private List<User> intersection(List<User> users, List<OtherUser> list) {
    
            return list.stream()
                    .flatMap(OtherUser -> users.stream()
                            .filter(user -> user.getId()
                                    .equalsIgnoreCase(OtherUser.getId())))
                    .collect(Collectors.toList());
        }
    
    0 讨论(0)
  • 2020-11-22 06:38

    Unions and intersections defined only for sets, not lists. As you mentioned.

    Check guava library for filters. Also guava provides real intersections and unions

     static <E> Sets.SetView<E >union(Set<? extends E> set1, Set<? extends E> set2)
     static <E> Sets.SetView<E> intersection(Set<E> set1, Set<?> set2)
    
    0 讨论(0)
  • 2020-11-22 06:41

    The solution marked is not efficient. It has a O(n^2) time complexity. What we can do is to sort both lists, and the execute an intersection algorithm as the one below.

    private  static ArrayList<Integer> interesect(ArrayList<Integer> f, ArrayList<Integer> s) { 
        ArrayList<Integer> res = new ArrayList<Integer>();
    
        int i = 0, j = 0; 
        while (i != f.size() && j != s.size()) { 
    
            if (f.get(i) < s.get(j)) {
                i ++;
            } else if (f.get(i) > s.get(j)) { 
                j ++;
            } else { 
                res.add(f.get(i)); 
                i ++;  j ++;
            }
        }
    
    
        return res; 
    }
    

    This one has a complexity of O(n log n + n) which is in O(n log n). The union is done in a similar manner. Just make sure you make the suitable modifications on the if-elseif-else statements.

    You can also use iterators if you want (I know they are more efficient in C++, I dont know if this is true in Java as well).

    0 讨论(0)
  • 2020-11-22 06:41

    After testing, here is my best intersection approach.

    Faster speed compared to pure HashSet Approach. HashSet and HashMap below has similar performance for arrays with more than 1 million records.

    As for Java 8 Stream approach, speed is quite slow for array size larger then 10k.

    Hope this can help.

    public static List<String> hashMapIntersection(List<String> target, List<String> support) {
        List<String> r = new ArrayList<String>();
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (String s : support) {
            map.put(s, 0);
        }
        for (String s : target) {
            if (map.containsKey(s)) {
                r.add(s);
            }
        }
        return r;
    }
    public static List<String> hashSetIntersection(List<String> a, List<String> b) {
        Long start = System.currentTimeMillis();
    
        List<String> r = new ArrayList<String>();
        Set<String> set = new HashSet<String>(b);
    
        for (String s : a) {
            if (set.contains(s)) {
                r.add(s);
            }
        }
        print("intersection:" + r.size() + "-" + String.valueOf(System.currentTimeMillis() - start));
        return r;
    }
    
    public static void union(List<String> a, List<String> b) {
        Long start = System.currentTimeMillis();
        Set<String> r= new HashSet<String>(a);
        r.addAll(b);
        print("union:" + r.size() + "-" + String.valueOf(System.currentTimeMillis() - start));
    }
    
    0 讨论(0)
  • 2020-11-22 06:41

    retainAll() method use for finding common element..i.e;intersection list1.retainAll(list2)

    0 讨论(0)
  • 2020-11-22 06:45

    First, I am copying all values of arrays into a single array then I am removing duplicates values into the array. Line 12, explaining if same number occur more than time then put some extra garbage value into "j" position. At the end, traverse from start-end and check if same garbage value occur then discard.

    public class Union {
    public static void main(String[] args){
    
        int arr1[]={1,3,3,2,4,2,3,3,5,2,1,99};
        int arr2[]={1,3,2,1,3,2,4,6,3,4};
        int arr3[]=new int[arr1.length+arr2.length];
    
        for(int i=0;i<arr1.length;i++)
            arr3[i]=arr1[i];
    
        for(int i=0;i<arr2.length;i++)
            arr3[arr1.length+i]=arr2[i];
        System.out.println(Arrays.toString(arr3));
    
        for(int i=0;i<arr3.length;i++)
        {
            for(int j=i+1;j<arr3.length;j++)
            {
                if(arr3[i]==arr3[j])
                    arr3[j]=99999999;          //line  12
            }
        }
        for(int i=0;i<arr3.length;i++)
        {
            if(arr3[i]!=99999999)
                System.out.print(arr3[i]+" ");
        }
    }   
    }
    
    0 讨论(0)
提交回复
热议问题