开源企业cms建站系统,网页设计与制作第三版,wordpress博客 分类,徐州做汽车销售的公司网站本周#xff0c;我们将暂时中断较高级别的问题和技术文章#xff0c;以解决我们中许多人可能面临的一些代码问题。 没什么花哨的或太辛苦的#xff0c;但是有一天它可能会节省您15分钟的时间#xff0c;偶尔回到基础上也很不错。 因此#xff0c;让我们开始吧。 有时… 本周我们将暂时中断较高级别的问题和技术文章以解决我们中许多人可能面临的一些代码问题。 没什么花哨的或太辛苦的但是有一天它可能会节省您15分钟的时间偶尔回到基础上也很不错。 因此让我们开始吧。 有时您会发现需要确定一个集合中的哪些元素存在于另一个集合中哪些是常见的和/或哪些不存在于另一个集合中。 阿帕奇百科全书集合在CollectionUtils一些一些实用方法是有用的尤其是路口 但这个帖子去有点超出了到计算集合的集合独特的元素它总是很高兴坐下来的细节。 我们还将通过支持任意数量的集合而不是像CollectionUtils那样仅支持两个集合来使解决方案更加通用。 另外事实是并非所有人都选择或能够包含库只是为了获得几个有用的实用程序方法。 当仅处理两个集合时这不是一个难题但是并非所有开发人员都熟悉java.util.Collection定义的所有方法因此这里是一些示例代码。 他们的关键是一起使用keepAll和removeAll方法来构建三个集合-通用仅在集合A中存在仅在B中存在。 SetString a new HashSetString();
a.add(a);
a.add(a2);
a.add(common);SetString b new HashSetString();
b.add(b);
b.add(b2);
b.add(common);SetString inAOnly new HashSetString(a);
inAOnly.removeAll(b);
System.out.println(A Only: inAOnly );SetString inBOnly new HashSetString(b);
inBOnly .removeAll(a);
System.out.println(B Only: inBOnly );SetString common new HashSetString(a);
common.retainAll(b);
System.out.println(Common: common); 输出 A Only: [a, a2]
B Only: [b, b2]
Common: [common1] 处理三个或更多集合 处理两个以上的集合时这个问题有些棘手但是可以很简单地以一种通用的方式解决如下所示 计算通用元素 计算公共元素很容易即使有大量集合此代码也将始终如一地执行。 public static void main(String[] args) {ListString a Arrays.asList(a, b, c);ListString b Arrays.asList(a, b, c, d);ListString c Arrays.asList(d, e, f, g);ListListString lists new ArrayListListString();lists.add(a);System.out.println(Common in A: getCommonElements(lists));lists.add(b);System.out.println(Common in A B: getCommonElements(lists));lists.add(c);System.out.println(Common in A B C: getCommonElements(lists));lists.remove(a);System.out.println(Common in B C: getCommonElements(lists));
}public static T SetT getCommonElements(Collection? extends CollectionT collections) {SetT common new LinkedHashSetT();if (!collections.isEmpty()) {Iterator? extends CollectionT iterator collections.iterator();common.addAll(iterator.next());while (iterator.hasNext()) {common.retainAll(iterator.next());}}return common;
} 输出 Common in A: [a, b, c]
Common in A B: [a, b, c]
Common in A B C: []
Common in B C: [d] 计算独特元素 计算唯一元素与计算通用元素一样简单。 请注意此代码的性能将随着您添加大量集合而降低尽管在大多数实际情况下都没有关系。 我猜想有一些方法可以优化它但是由于我没有遇到问题所以我没有打扰tryin。 正如克努斯Knuth著名的说法 “我们应该忘记效率低下的问题大约有97的时间是这样过早的优化是万恶之源”。 public static void main(String[] args) {ListString a Arrays.asList(a, b, c);ListString b Arrays.asList(a, b, c, d);ListString c Arrays.asList(d, e, f, g);ListListString lists new ArrayListListString();lists.add(a);System.out.println(Unique in A: getUniqueElements(lists));lists.add(b);System.out.println(Unique in A B: getUniqueElements(lists));lists.add(c);System.out.println(Unique in A B C: getUniqueElements(lists));lists.remove(a);System.out.println(Unique in B C: getUniqueElements(lists));
}public static T ListSetT getUniqueElements(Collection? extends CollectionT collections) {ListSetT allUniqueSets new ArrayListSetT();for (CollectionT collection : collections) {SetT unique new LinkedHashSetT(collection);allUniqueSets.add(unique);for (CollectionT otherCollection : collections) {if (collection ! otherCollection) {unique.removeAll(otherCollection);}}}return allUniqueSets;
} 输出 Unique in A: [[a, b, c]]
Unique in A B: [[], [d]]
Unique in A B C: [[], [], [e, f, g]]
Unique in B C: [[a, b, c], [e, f, g]] 这里的所有都是它的。 随意使用此代码无论您喜欢什么如果有任何改进或建议请发表评论。 当我们分享知识和经验时开发人员都将从中受益。 参考 计算多个集合中的公共和唯一元素– Java和我们的JCG合作伙伴 Carfey软件博客博客上的Craig Flichel。 翻译自: https://www.javacodegeeks.com/2012/03/common-and-unique-elements-in-multiple.html