fre*_*est 105 java collections
我有一个Integer类型的List,例如:
[1, 1, 2, 3, 3, 3]
Run Code Online (Sandbox Code Playgroud)
我想要一个方法来返回所有重复项,例如:
[1, 3]
Run Code Online (Sandbox Code Playgroud)
做这个的最好方式是什么?
lei*_*ifg 172
返回布尔值的方法add,Set无论值是否已存在(如果不存在,则返回true;如果已存在,则返回false,请参阅设置文档).
所以只需遍历所有值:
public Set<Integer> findDuplicates(List<Integer> listContainingDuplicates)
{
final Set<Integer> setToReturn = new HashSet<>();
final Set<Integer> set1 = new HashSet<>();
for (Integer yourInt : listContainingDuplicates)
{
if (!set1.add(yourInt))
{
setToReturn.add(yourInt);
}
}
return setToReturn;
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ler 46
我也需要一个解决方案.我使用了leifg的解决方案并使其成为通用的.
private <T> Set<T> findDuplicates(Collection<T> collection) {
Set<T> duplicates = new LinkedHashSet<>();
Set<T> uniques = new HashSet<>();
for(T t : collection) {
if(!uniques.add(t)) {
duplicates.add(t);
}
}
return duplicates;
}
Run Code Online (Sandbox Code Playgroud)
Seb*_*ian 34
我采用了John Strickler的解决方案并重新构建它以使用JDK8中引入的流API:
private <T> Set<T> findDuplicates(Collection<T> collection) {
Set<T> uniques = new HashSet<>();
return collection.stream()
.filter(e -> !uniques.add(e))
.collect(Collectors.toSet());
}
Run Code Online (Sandbox Code Playgroud)
sno*_*man 13
这是在Java 8中使用Streams的解决方案
// lets assume the original list is filled with {1,1,2,3,6,3,8,7}
List<String> original = new ArrayList<>();
List<String> result = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
您只需查看列表中此对象的出现次数是否超过一次即可。然后调用.distinct()以在结果中仅包含唯一元素
result = original.stream()
.filter(e -> Collections.frequency(original, e) > 1)
.distinct()
.collect(Collectors.toList());
// returns {1,3}
// returns only numbers which occur more than once
result = original.stream()
.filter(e -> Collections.frequency(original, e) == 1)
.collect(Collectors.toList());
// returns {2,6,8,7}
// returns numbers which occur only once
result = original.stream()
.distinct()
.collect(Collectors.toList());
// returns {1,2,3,6,8,7}
// returns the list without duplicates
Run Code Online (Sandbox Code Playgroud)
Ash*_*yan 12
int[] nums = new int[] {1, 1, 2, 3, 3, 3};
Arrays.sort(nums);
for (int i = 0; i < nums.length-1; i++) {
if (nums[i] == nums[i+1]) {
System.out.println("duplicate item "+nums[i+1]+" at Location"+(i+1) );
}
}
Run Code Online (Sandbox Code Playgroud)
显然你可以用它们做任何你想做的事情(即放入一个Set来获得一个重复值的唯一列表)而不是打印...这也有记录重复项目的位置的好处.
Java 8基本解决方案:
List duplicates =
list.stream().collect(Collectors.groupingBy(Function.identity()))
.entrySet()
.stream()
.filter(e -> e.getValue().size() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这也有效:
public static Set<Integer> findDuplicates(List<Integer> input) {
List<Integer> copy = new ArrayList<Integer>(input);
for (Integer value : new HashSet<Integer>(input)) {
copy.remove(value);
}
return new HashSet<Integer>(copy);
}
Run Code Online (Sandbox Code Playgroud)
在Java 8上使用Guava
private Set<Integer> findDuplicates(List<Integer> input) {
// Linked* preserves insertion order so the returned Sets iteration order is somewhat like the original list
LinkedHashMultiset<Integer> duplicates = LinkedHashMultiset.create(input);
// Remove all entries with a count of 1
duplicates.entrySet().removeIf(entry -> entry.getCount() == 1);
return duplicates.elementSet();
}
Run Code Online (Sandbox Code Playgroud)
你可以使用这样的东西:
List<Integer> newList = new ArrayList<Integer>();
for(int i : yourOldList)
{
yourOldList.remove(i);
if(yourOldList.contains(i) && !newList.contains(i)) newList.add(i);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
Lambas 可能是一个解决方案
Integer[] nums = new Integer[] {1, 1, 2, 3, 3, 3};
List<Integer> list = Arrays.asList(nums);
List<Integer> dps = list.stream().distinct().filter(entry -> Collections.frequency(list, entry) > 1).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
155668 次 |
| 最近记录: |