Java: Comparing two string arrays and removing elements that exist in both arrays

前端 未结 7 1713
余生分开走
余生分开走 2020-12-14 21:38

This is mainly a performance questions. I have a master list of all users existing in a String array AllUids. I also have a list of all end dated users existing in a String

相关标签:
7条回答
  • 2020-12-14 21:57

    The easiest solution is probably to put all of the elements into a Set and then use removeAll. You can convert to a Set from an array like this:

    Set<String> activeUids = new HashSet<String>(Arrays.asList(activeUidsArray));
    

    though you should really try to avoid using arrays and favor collections.

    0 讨论(0)
  • 2020-12-14 22:03

    Don't use arrays for this, use Collection and the removeAll() method. As for performance: unless you do something idiotic that leads to O(n^2) runtime, just forget about it. It's premature optimization, the useless/harmful kind. "thousands of users" is nothing, unless you're doing it thousands of times each second.

    BTW, PHP "arrays" are in fact hash maps.

    0 讨论(0)
  • 2020-12-14 22:05

    Commons Collections has a class called CollectionUtils and a static method called removeAll which takes an initial list and a list of thing to remove from that list:

    Collection removeAll(Collection collection,
                         Collection remove)
    

    That should do what you want provided you use lists of users rather than arrays. You can convert your array into a list very easily with Arrays.asList() so...

    Collection ActiveUids = CollectionUtils.removeAll(Arrays.asList(AllUids), 
                                                      Arrays.asList(EndUids))
    

    EDIT: I also did a bit of digging with this into Commons Collections and found the following solution with ListUtils in Commons Collections as well:

    List diff = ListUtils.subtract(Arrays.asList(AllUids), Arrays.asList(EndUids));
    

    Pretty neat...

    0 讨论(0)
  • 2020-12-14 22:18
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author Bireswhar
     */
    import java.util.Collection;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Repeated {
    
        public static void main(String[] args) {
    //        Collection listOne = new ArrayList(Arrays.asList("milan","dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta"));
    //        Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga", "binga", "mike", "dingo"));
    //
    //        listOne.retainAll( listTwo );
    //        System.out.println( listOne );
    
            String[] s1 = {"ram", "raju", "seetha"};
            String[] s2 = {"ram"};
            List<String> s1List = new ArrayList(Arrays.asList(s1));
            for (String s : s2) {
                if (s1List.contains(s)) {
                    s1List.remove(s);
                } else {
                    s1List.add(s);
                }
                 System.out.println("intersect on " + s1List);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-14 22:19
        String s1 = "a,b,c,d";
        String s2 = "x,y,z,a,b,c";
        Set<String> set1 = new HashSet<String>();
        Set<String> set2 = new HashSet<String>();
    
        Set<String> set11 = new HashSet<String>();
    
        String[] splitS1 = s1.split(",");
        String[] splitS2 = s2.split(",");
    
        for(String s3:splitS1){
            set1.add(s3);
            set11.add(s3);
        }
    
        for(String s4:splitS2){
            set2.add(s4);
        }
        set1.removeAll(set2);
        set2.removeAll(set11);
        set1.addAll(set2);
        System.out.println(set1);
    
    0 讨论(0)
  • 2020-12-14 22:20

    You can't "remove" elements from arrays. You can set them to null, but arrays are of fixed size.

    You could use java.util.Set and removeAll to take one set away from another, but I'd prefer to use the Google Collections Library:

    Set<String> allUids = Sets.newHashSet("Joe", "Tom", "Dan",
                                          "Bill", "Hector", "Ron");
    Set<String> endUids = Sets.newHashSet("Dan", "Hector", "Ron");
    Set<String> activeUids = Sets.difference(allUids, endUids);
    

    That has a more functional feel to it.

    0 讨论(0)
提交回复
热议问题