我有一組串列,無論每個串列中元素的順序如何,我都想從中洗掉重復項,如下所示:
我有這個作為輸入 [[-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]]
那么我如何才能設法改進重復項并保持我得到的三元組完好無損?
先感謝您。
uj5u.com熱心網友回復:
我認為您可以使用排序來使使用 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]]
uj5u.com熱心網友回復:
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());
uj5u.com熱心網友回復:
您可以創建一個類來表示您的集合元素并賦予它們您想要的行為。也就是說,如果兩個元素包含相同的整數而不管順序,則它們是相等的。
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]]
The way you do this really depends on the larger context of your problem domain. I think it's unlikely that you really want a public class called IntList but you might contain it inside your own implementation of Set, or somewhere else in your model.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/340157.html
上一篇:pandas:根據串列和另一列條件替換逗號分隔列中的相應值
下一篇:如何使用Zip<TFirst,TSecond>(IEnumerable<TFirst>,IEnumerable<TSecond>)?
