[英]Java: How do I compare a List<String> with List<List<String>>?
There is an ArrayList1 and a List2 containing several arraylists in it. 有一个ArrayList1和一个List2,其中包含多个arraylist。
I want to compare the contents in arraylist1 with all the arraylists(in List2) and throw out the records from List2 which are not equal to arraylist1. 我想将arraylist1中的内容与所有arraylists(在List2中)进行比较,并从List2中抛出不等于arraylist1的记录。
There are couple of questions similar to this one, but I could't understand it clearly. 有几个与此问题类似的问题,但我不清楚。 Please help.
请帮忙。
Thanks, Mike 谢谢,迈克
Depending on how you want to implement, iterate: 根据您想要的实现方式,迭代:
List<String> yourSingleList = new ArrayList<>();
for (List<String> list : yourListOfLists) {
//remove entries
list.retainAll(yourSingleList);
//or remove matching entities
list.removeAll(yourSingleList);
}
Or use an iterator to compare entirely: 或使用迭代器进行整体比较:
List<String> yourSingleList = new ArrayList<>();
Iterator<List<String>> lists = yourListOfLists.iterator();
while (lists.hasNext()) {
if (!lists.next().equals(yourSingleList) {
lists.remove();
}
}
I guess you want something like that: 我想你想要这样的东西:
List<List<String>> listlist = new ArrayList<List<String>>();
List<String> list = new ArrayList<String>();
// some example input here
list.add("A");
list.add("B");
list.add("C");
listlist.add(list);
for (List<String> l : listlist) {
for(String s : l) {
if (list.contains(s)) {
System.out.println(s + " is at position " + list.indexOf(s));
}
}
}
查看如何比较两个ArrayList <List <String >>像一个二维数组一样考虑它,并将列表A的每个元素与列表B.get(1),列表B.get(2)等中的每个元素进行比较。
Sounds like a homework question so here is the logic 听起来像是一个作业问题,所以这是逻辑
input: List of Arrays, Array
output: List
List Compiar getList(L, A):
| newL <- new List
| for each Array a in L do
| | for int i <- 0 to A.size()
| | | Array n = new Array
| | | for int j <- 0 to a.size()
| | | | if compairitor(A.get(i), a.get(j)) = 0 // compairitor is used to compair values
| | | | | n.add(a.get(j)
| | | | end-if
| | | end-for
| | | newL.add(n)
| | end-for
| end-for-each
| return newL
end
Use this method to replase the old list ex List old = getList(L, Comparison array) 使用此方法替换旧列表,例如List old = getList(L,比较数组)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.