我认为没有内置的方法可以完全做到您想要的,但使用
containsAll() 并不难实现:
使用您提供的值,这里有一个快速示例来展示如何识别子列表或相等列表:
public static void main(String[] args){
List<Integer> listOne = new ArrayList<>(Arrays.asList(1,2,3));
List<Integer> listTwo = new ArrayList<>(Arrays.asList(1,2,3,4,5,6));
List<Integer> listThree = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7));
List<Integer> listFour = new ArrayList<>(Arrays.asList(1,2,3,4,5,7,6));
List<List<Integer>> listOfLists = new ArrayList<>(Arrays.asList(listOne, listTwo, listThree, listFour));
for(int currentIndex = 0; currentIndex < listOfLists.size(); currentIndex++) {
List<Integer> currentList = listOfLists.get(currentIndex);
for (int comparisonIndex = 0; comparisonIndex < listOfLists.size(); comparisonIndex++) {
if(currentIndex == comparisonIndex) { continue; }
List<Integer> comparisonList = listOfLists.get(comparisonIndex);
if(comparisonList.containsAll(currentList)){
boolean isEqualSet = comparisonList.size() == currentList.size();
System.out.println(currentList + " is " + (isEqualSet ? "an equal set of: " : "a subset of: ") + comparisonList);
continue;
}
}
}
}
输出:
[1, 2, 3] is a subset of: [1, 2, 3, 4, 5, 6]
[1, 2, 3] is a subset of: [1, 2, 3, 4, 5, 6, 7]
[1, 2, 3] is a subset of: [1, 2, 3, 4, 5, 7, 6]
[1, 2, 3, 4, 5, 6] is a subset of: [1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6] is a subset of: [1, 2, 3, 4, 5, 7, 6]
[1, 2, 3, 4, 5, 6, 7] is an equal set of: [1, 2, 3, 4, 5, 7, 6]
[1, 2, 3, 4, 5, 7, 6] is an equal set of: [1, 2, 3, 4, 5, 6, 7]
您可以根据您的标准存储列表的索引,然后在之后删除它们。
List.class
。由于一个是类而另一个是原始结构,因此数组和列表的处理方式略有不同,其中一个具有方法,因为它是一个类,而另一个则通过类具有实用程序方法或者您必须手动创建函数。你甚至看过Arrays
类吗?Arrays Java API - Mr00Anderson