Java的:比较两个字符串数组和删除元素存在于两个数组 [英] Java: Comparing two string arrays and removing elements that exist in both arrays

查看:326
本文介绍了Java的:比较两个字符串数组和删除元素存在于两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这主要是一个性能问题。我有存在于String数组AllUids所有用户的主列表。我也有存在于String数组EndUids凡是注日期的最终用户的列表。

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.

我在Java中工作,我的目标是消除存在从主列表AllUids结束日期为阵列中的任何用户。我知道PHP有一个叫做和array_diff功能。

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有什么事情是比较两个数组,并删除在这两个相似的元素。我的目标是高性能这里这就是为什么我问一个内置的功能。我不想添加任何特殊包。

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.

我想过写一个递归函数,但它只是看起来这将是低效的。有数以千计的两个列表中的用户。为了在结束日期列表存在,则必须存在于AllUids列表,也就是直到删除。

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.

例如:

String[] AllUids = {"Joe", "Tom", "Dan", "Bill", "Hector", "Ron"};

String[] EndUids = {"Dan", "Hector", "Ron"};

功能我要找:

String[] ActiveUids = AllUids.RemoveSimilar(EndUids);

ActiveUids是这样的:

ActiveUids would look like this:

{"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.

推荐答案

共享集合已经称为类<一href=\"http://commons.apache.org/collections/api-2.1.1/org/apache/commons/collections/CollectionUtils.html\">CollectionUtils和一个叫静的removeAll方法,需要一个初步名单和事情的清单,以从该列表中删除:

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)

这是应该做你想要什么只要你使用的用户,而不是数组列表。你可以用Arrays.asList(),所以你的数组转换成一个列表很容易...

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))

编辑:我也做了一些与此挖掘到共享集合,发现在下议院集合ListUtils以下解决方案还有:

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

pretty整齐...

Pretty neat...

这篇关于Java的:比较两个字符串数组和删除元素存在于两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆