Java中ArrayList的交并

Java中ArrayList的交并

是否有办法这样做?我在找,但找不到。

另一个问题:我需要这些方法来过滤文件。有些是AND过滤器和一些是OR过滤器(就像集合论中的那样),所以我需要根据所有文件和保存这些文件的UnitedArrayList/Intersects ArrayList进行过滤。

我应该使用不同的数据结构来保存文件吗?还有什么能提供更好的运行时的吗?


回首忆惘然
浏览 698回答 3
3回答

MM们

这里是一个没有使用任何第三方库的简单实现。主要优势retainAll,&nbsp;removeAll和addAll这些方法不修改输入到方法的原始列表。public&nbsp;class&nbsp;Test&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String...&nbsp;args)&nbsp;throws&nbsp;Exception&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List<String>&nbsp;list1&nbsp;=&nbsp;new&nbsp;ArrayList<String>(Arrays.asList("A",&nbsp;"B",&nbsp;"C")); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List<String>&nbsp;list2&nbsp;=&nbsp;new&nbsp;ArrayList<String>(Arrays.asList("B",&nbsp;"C",&nbsp;"D",&nbsp;"E",&nbsp;"F")); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(new&nbsp;Test().intersection(list1,&nbsp;list2)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(new&nbsp;Test().union(list1,&nbsp;list2)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;<T>&nbsp;List<T>&nbsp;union(List<T>&nbsp;list1,&nbsp;List<T>&nbsp;list2)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set<T>&nbsp;set&nbsp;=&nbsp;new&nbsp;HashSet<T>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set.addAll(list1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set.addAll(list2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;ArrayList<T>(set); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;<T>&nbsp;List<T>&nbsp;intersection(List<T>&nbsp;list1,&nbsp;List<T>&nbsp;list2)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List<T>&nbsp;list&nbsp;=&nbsp;new&nbsp;ArrayList<T>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(T&nbsp;t&nbsp;:&nbsp;list1)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(list2.contains(t))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add(t); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;list; &nbsp;&nbsp;&nbsp;&nbsp;}}

凤凰求蛊

收藏(ArrayList也是如此)有:col.retainAll(otherCol)&nbsp;//&nbsp;for&nbsp;intersectioncol.addAll(otherCol)&nbsp;//&nbsp;for&nbsp;union如果接受重复,则使用列表实现;如果不接受重复,则使用SET实现:Collection<String> col1 = new ArrayList<String>(); // {a, b, c}// Collection<String> col1 = new TreeSet<String>();col1.add("a");col1.add("b");col1.add("c");Collection<String> col2 = new ArrayList<String>(); // {b, c, d, e}// Collection<String> col2 = new TreeSet<String>();col2.add("b");col2.add("c");col2.add("d");col2.add("e");col1.addAll(col2);System.out.println(col1);&nbsp;//output for ArrayList: [a, b, c, b, c, d, e]//output for TreeSet: [a, b, c, d, e]

达令说

这篇文章相当古老,但它是谷歌在寻找这个话题时第一次出现。我想使用Java 8流(基本上)在一行中进行相同的更新:List<T>&nbsp;intersect&nbsp;=&nbsp;list1.stream() &nbsp;&nbsp;&nbsp;&nbsp;.filter(list2::contains) &nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());List<T>&nbsp;union&nbsp;=&nbsp;Stream.concat(list1.stream(),&nbsp;list2.stream()) &nbsp;&nbsp;&nbsp;&nbsp;.distinct() &nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());如果有人有更好/更快的解决方案,请告诉我,但是这个解决方案是一个很好的线性程序,可以很容易地包含在方法中,而无需添加不必要的助手类/方法,并且仍然保持可读性。
打开App,查看更多内容
随时随地看视频慕课网APP