我有一个场景,其中2个列表将与列表中作为唯一ID的第1列进行比较,并且需要获取相应ID的其余列中的差异。
E.g
清单1
Apple 12 1
Windows 12 3 5清单2
Apple 12 1 4
Windows 12 3 5输出: Apple 4
答案 0 :(得分:0)
前者
ArrayList l1=new ArrayList();
l1.add("a");
l1.add("b");
l1.add("c");
l1.add("d");
l1.add("e");
l1.add("f");
ArrayList l12=new ArrayList();
l12.add("z");
l12.add("b");
l12.add("c");
l12.add("d");
l12.add("e");
l12.add("g");
ArrayList outList = new ArrayList();
for(int i=1 ;i<l12.size(); i++)
{
if(!(l1.contains(l12.get(i))))
{
outList.add(l12.get(i));
}
}
这里outList包含2个列表之间的差异但不包括列表l1和l12的第1个值的元素。
答案 1 :(得分:0)
我添加了评论。检查以下代码:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;
public class AddTwoArray {
public static void main(String[] args) {
List<String> list1 = new ArrayList<String>();
list1.add("Windows 12 3 5");
list1.add("Apple 12 1");
list1.add("Orange");
List<String> list2 = new ArrayList<String>();
list2.add("Apple 4 12 1");
list2.add("Orange 5");
list2.add("Windows");
TreeSet s1 = new TreeSet<String>(list1);
TreeSet s2 = new TreeSet<String>(list2);
TreeSet s3 = new TreeSet<String>(list1);
TreeSet s4 = new TreeSet<String>(list2);
s1.removeAll(s2);
s4.removeAll(s3);
List<String> diff = new ArrayList<String>(s1);
List<String> diff1 = new ArrayList<String>(s4);
List<String> diffArray = null;
for (int tmp = 0 ; tmp < diff.size(); tmp++) {
diffArray = findDiff(diff.get(tmp).split(" "), diff1.get(tmp).split(" ")); // call method findDiff
if(diffArray != null){
for(String tmp1: diffArray){
String[] diffSplit = tmp1.split(" ");
for(int i =0; i < diffSplit.length; i++){
System.out.print(diffSplit[i] + " ");
}
}
}
System.out.println();
}
}
public static List<String> findDiff(String[] diff, String[] diff1) { // find the different between two arrays
HashSet<String> map = new HashSet<String>();
List<String> diffArray = new ArrayList<String>();
diffArray.add(diff[0]); // add unique key
for (String i : diff){
map.add(i);
}
for (String i : diff1) {
if (!map.contains(i)){ //find different element between diff and diff1
diffArray.add(i);
}
}
map = new HashSet<String>();
for (String i : diff1)
map.add(i);
for (String i : diff) {
if (!map.contains(i)){ //find different element between diff1 and diff
diffArray.add(i);
}
}
return diffArray; //diff array
}
}
如果你不清楚评论。