Java:比较两个字符串数组并删除两个数组中存在的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1235033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Comparing two string arrays and removing elements that exist in both arrays
提问by user84786
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 array EndUids.
这主要是一个性能问题。我有一个字符串数组 AllUids 中存在的所有用户的主列表。我还有一个字符串数组 EndUids 中存在的所有结束日期用户的列表。
I am working in Java and my goal is to remove any users that exist in the end dated array from the master list AllUids. I know PHP has a function called array_diff.
我正在使用 Java,我的目标是从主列表 AllUids 中删除存在于结束日期数组中的任何用户。我知道 PHP 有一个名为 array_diff 的函数。
I was curious if Java has anything that will compare two arrays and remove elements that are similar in both. My objective is performance here which is why I asked about a built in function. I do not want to add any special packages.
我很好奇 Java 是否有任何东西可以比较两个数组并删除两者中相似的元素。我的目标是这里的性能,这就是我询问内置函数的原因。我不想添加任何特殊的包。
I thought about writing a recursive function but it just seems like it will be inefficient. There are thousands of users in both lists. In order to exist in the end dated list, you must exist in the AllUids list, that is until removed.
我想写一个递归函数,但它似乎效率低下。两个列表中都有成千上万的用户。为了存在于结束日期列表中,您必须存在于 AllUids 列表中,即直到被移除。
Example:
例子:
String[] AllUids = {"Joe", "Tom", "Dan", "Bill", "Hector", "Ron"};
String[] EndUids = {"Dan", "Hector", "Ron"};
Functionality I am looking for:
我正在寻找的功能:
String[] ActiveUids = AllUids.RemoveSimilar(EndUids);
ActiveUids would look like this:
ActiveUids 看起来像这样:
{"Joe", "Tom", "Bill"}
Thank you all, Obviously I can come up with loops and such but I am not confident that it will be efficient. This is something that will run on production machines everyday.
谢谢大家,显然我可以想出循环等,但我不相信它会有效。这是每天都会在生产机器上运行的东西。
采纳答案by Jon
Commons Collectionshas a class called CollectionUtilsand a static method called removeAll which takes an initial list and a list of thing to remove from that list:
Commons Collections有一个名为CollectionUtils的类和一个名为 removeAll 的静态方法,它接受一个初始列表和一个要从该列表中删除的事物列表:
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...
如果您使用用户列表而不是数组,那应该可以满足您的需求。您可以使用 Arrays.asList() 非常轻松地将数组转换为列表,因此...
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:
编辑:我还对 Commons Collections 进行了一些挖掘,并在 Commons Collections 中找到了 ListUtils 的以下解决方案:
List diff = ListUtils.subtract(Arrays.asList(AllUids), Arrays.asList(EndUids));
Pretty neat...
漂亮整齐...
回答by Jon Skeet
You can't "remove" elements from arrays. You can set them to null, but arrays are of fixed size.
您不能从数组中“删除”元素。您可以将它们设置为 null,但数组的大小是固定的。
You coulduse java.util.Set
and removeAll
to take one set away from another, but I'd prefer to use the Google Collections Library:
您可以使用java.util.Set
andremoveAll
将一组从另一组中取出,但我更喜欢使用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.
这有一种更实用的感觉。
回答by Samuel Carrijo
You could put those strings into a Collectioninstead, and then use removeAll method.
您可以将这些字符串放入一个集合中,然后使用 removeAll 方法。
回答by Laurence Gonsalves
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 中,然后使用 removeAll。您可以像这样从数组转换为 Set:
Set<String> activeUids = new HashSet<String>(Arrays.asList(activeUidsArray));
though you should really try to avoid using arrays and favor collections.
尽管您真的应该尽量避免使用数组并使用集合。
回答by Michael Borgwardt
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.
不要为此使用数组,使用 Collection 和removeAll()方法。至于性能:除非你做一些愚蠢的事情导致 O(n^2) 运行时,否则就忘记它。这是过早的优化,无用/有害的那种。“成千上万的用户”不算什么,除非您每秒执行数千次。
BTW, PHP "arrays" are in fact hash maps.
顺便说一句,PHP“数组”实际上是哈希映射。
回答by Bireshwar
/*
* 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);
}
}
}
回答by Aravindh
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);