[英]How to remove duplicates from Set of list in Java
我有一组列表,无论每个列表中元素的顺序如何,我都想从中删除重复项,如下所示:
我有这个作为输入[[-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]]
这是逻辑,因为内部 Set 优化了[-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 实现中,或者您模型中的其他地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.