【发布时间】:2021-02-01 09:32:33
【问题描述】:
我有这个面试任务。我认为这很简单,但是...
我有两个列表:
List<String> l1 = List.of("A", "B", "A", "A");
List<String> l2 = List.of("A", "C", "A");
我需要找出这两个列表的共同点。
当然,我试过这个解决方案:
List<String> l1 = List.of("A", "B", "A", "A");
List<String> l2 = List.of("A", "C", "A");
Set<String> l3 = new HashSet<>(l1);
l3.retainAll(l2);
System.out.println(l3); // outputs A
还有这个:
List<String> l1 = List.of("A", "B", "A", "A");
List<String> l2 = List.of("A", "C", "A");
List<String> l3 = new ArrayList<>(l1);
l3.retainAll(l2);
System.out.println(l3); // outputs A, A, A
但我只需要输出A, A
我能做什么?
【问题讨论】:
标签: java list collections set intersection