如何从Java的Set列表中删除重复项



我有一组列表,我想从中删除重复项,无论每个列表中的元素顺序如何,如:

输入是[[-1,-1,2],[0,-1,1],[1,-1,0],[2,-1,-1],[-1,2,-1],[-1,1,0],[0,1,-1],[-1,0,1],[1,0,-1]]

当我使用Set<Set>来细化我的元素时,它确实部分工作,但我得到[[1,-1,0],[-1,2]],这是逻辑的,因为内部集合细化了[-1,-1,2]的重复项。

当我尝试使用Set<List>时,我无法细化我的元素,这就得到了这个[[-1,-1,2],[0,-1,1],[1,-1,0],[2,-1,-1],[-1,2,-1],[-1,1,0],[0,1,-1],[-1,0,1],[1,0,-1]]

那么我如何才能改进重复项并保持生成的三元组完整呢?

提前谢谢你。

我认为你可以使用排序使使用Set和List工作如你所指定的:

import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
class Main {

public static void main(String[] args) {
int[][] arrayWithDuplicates = new int[][] { { -1, -1, 2 }, { 0, -1, 1 }, { 1, -1, 0 }, { 2, -1, -1 },
{ -1, 2, -1 }, { -1, 1, 0 }, { 0, 1, -1 }, { -1, 0, 1 }, { 1, 0, -1 } };
System.out.printf("arrayWithDuplicates = %s%n", Arrays.deepToString(arrayWithDuplicates));
int[][] arrayWithoutDuplicates = getArrayWithoutDuplicates(arrayWithDuplicates);
System.out.printf("arrayWithoutDuplicates = %s%n", Arrays.deepToString(arrayWithoutDuplicates));
}
public static int[][] getArrayWithoutDuplicates(int[][] array) {
List<int[]> listWithoutDuplicates = new ArrayList<>();
Set<List<Integer>> seenSubLists = new HashSet<>();
for (int[] ints : array) {
List<Integer> sortedInts = Arrays.stream(ints).boxed().sorted().collect(Collectors.toList());
if (!seenSubLists.contains(sortedInts)) {
listWithoutDuplicates.add(ints);
seenSubLists.add(sortedInts);
}
}
return listWithoutDuplicates.toArray(new int[listWithoutDuplicates.size()][]);
}
}

输出:

arrayWithDuplicates = [[-1, -1, 2], [0, -1, 1], [1, -1, 0], [2, -1, -1], [-1, 2, -1], [-1, 1, 0], [0, 1, -1], [-1, 0, 1], [1, 0, -1]]
arrayWithoutDuplicates = [[-1, -1, 2], [0, -1, 1]]
final Set<List<Integer>> sortedLists = new HashSet<>();
Set<List<Integer>> newLists = lists.stream()
.map(integers -> {
List<Integer> sorted = integers.stream().sorted().collect(Collectors.toList());
if (sortedLists.contains(sorted)) {
return null;
}
sortedLists.add(sorted);
return integers;
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());

您可以创建一个类来表示您的集合元素并赋予它们您想要的行为。也就是说,如果两个元素包含相同的整数,无论其顺序如何,它们都是相等的。

import java.util.Arrays;
public class IntList extends Object {
// I will keep the original array but you can just sort it in place if that makes sense
private int[] array; // The orignal array
private int[] sortedArray; // Sorted copy of the original array
public IntList( int[] array ) {
this.array = array;
this.sortedArray = array.clone();
Arrays.sort( this.sortedArray );
}
@Override
public boolean equals( Object o ) {
// This object is equal to another if they are:
//   the same instance or instances of this class with equal sorted arrays
boolean result;
if ( o == this ) {
result = true;
} else {
if ( ! ( o instanceof IntList ) ) {
result = false;
} else {
IntList other = ( IntList ) o;
result = Arrays.equals( this.sortedArray, other.sortedArray );
}
}
return result;
}
@Override
public int hashCode() {
// Used by HashSet
return Arrays.hashCode( this.sortedArray );
}
@Override
public String toString() {
return Arrays.toString( this.sortedArray );
}
}

则可以构造一个具有下列元素的Set:

import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
public class main {
public static void main(String[] args) {
int[][] input = new int[][] { { -1, -1, 2 }, { 0, -1, 1 }, { 1, -1, 0 }, { 2, -1, -1 },
{ -1, 2, -1 }, { -1, 1, 0 }, { 0, 1, -1 }, { -1, 0, 1 }, { 1, 0, -1 } };
System.out.printf("input = %s%n", Arrays.deepToString(input));
Set<IntList> set = new HashSet<IntList>();
for( int[] currIntArray: input ) {
IntList list = new IntList( currIntArray );
set.add( list );
}
System.out.printf( "output = %s%n", set.toString());
}
}

结果

input = [[-1, -1, 2], [0, -1, 1], [1, -1, 0], [2, -1, -1], [-1, 2, -1], [-1, 1, 0], [0, 1, -1], [-1, 0, 1], [1, 0, -1]]
output = [[-1, -1, 2], [0, -1, 1]]

您这样做的方式实际上取决于您的问题域的更大上下文。我认为你不太可能真的想要一个叫做IntList的公共类,但你可能会把它包含在你自己的Set实现中,或者在你模型的其他地方。

最新更新