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

前端 未结 7 1727
余生分开走
余生分开走 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 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 allUids = Sets.newHashSet("Joe", "Tom", "Dan",
                                          "Bill", "Hector", "Ron");
    Set endUids = Sets.newHashSet("Dan", "Hector", "Ron");
    Set activeUids = Sets.difference(allUids, endUids);
    

    That has a more functional feel to it.

提交回复
热议问题