[英]Comparison of two List in Java and displaying the difference between two List in tabular format in JSP using JSTL
我有两个Arraylist-
List<java_test> one=new ArrayList<java_test>();
List<java_test> two=new ArrayList<java_test>();
我有两种方法可以分别返回定义的ArrayList值。 与第二个数组列表(即两个)相比,第一个数组列表(即一个)具有更多的值。
现在我想
比较两个数组列表的值并找出差异
此外,由于第一个Arrayls的值要比两个 数组多,因此,当只有一个Arraylsit时,应该显示0 ,因为两者之间存在差异。
编辑-1
我试图实现计算差异为-
public LinkedHashMap < String, List < Double >> diff() { for (int i = 0; i < one.size() && i < two.size(); i++) { Comaprision refObj = one.get(i); Comaprision stObj = two.get(i); Double x = refObj.getBeam_current(); Double y = stObj.getBeam_current(); x = refObj.getBeam_energy(); y = stObj.getBeam_energy(); comparing(x, y); x = refObj.getSt2_vs2_bag1_rb(); y = stObj.getSt2_vs2_bag1_rb(); comparing(x, y); x = refObj.getSt2_vs2_bag2_rb(); y = stObj.getSt2_vs2_bag2_rb(); comparing(x, y); x = refObj.getSt2_vs2_bag3_rb(); y = stObj.getSt2_vs2_bag3_rb(); comparing(x, y); x = refObj.getSt2_vs2_bag4_rb(); y = stObj.getSt2_vs2_bag4_rb(); comparing(x, y); dif.put("", z); } return dif; } public List < Double > comparing(Double x1, Double y1) { if ((x1 > y1)) { z.add(x1 - y1); System.out.println("value of z is" + z); } else { z.add(y1 - x1); } return z; }
我收到的输出是- 但这并没有给出正确的区别。我犯了什么错误?
如果我正确理解了您的问题。 您想要做的是,如果两个数组列表都具有相同索引的值,则打印差异;如果只有一个数组列表具有相同索引的值,则打印零。
为此,有两种可能性。
如果您的数组列表仅包含非零值。 您可以如下更改for循环的条件
for (int i = 0; i < one.size(); i++) // since you have already specified first array list is larger than the second.
然后按如下所示更改比较功能:
public List < Double > comparing(Double x1, Double y1) {
if(y1 == 0)
{
z.add(0);
}
else
if ((x1 > y1)) {
z.add(x1 - y1);
System.out.println("value of z is" + z);
} else {
z.add(y1 - x1);
}
return z;
}
如果您的arraylist包含零值。 我建议做这样的事情
public LinkedHashMap < String, List < Double >> diff() { for (int i = 0; i < one.size(); i++) { if(i < two.size()){ Comaprision refObj = one.get(i); Comaprision stObj = two.get(i); Double x = refObj.getBeam_current(); Double y = stObj.getBeam_current(); x = refObj.getBeam_energy(); y = stObj.getBeam_energy(); comparing(x, y); x = refObj.getSt2_vs2_bag1_rb(); y = stObj.getSt2_vs2_bag1_rb(); comparing(x, y); x = refObj.getSt2_vs2_bag2_rb(); y = stObj.getSt2_vs2_bag2_rb(); comparing(x, y); x = refObj.getSt2_vs2_bag3_rb(); y = stObj.getSt2_vs2_bag3_rb(); comparing(x, y); x = refObj.getSt2_vs2_bag4_rb(); y = stObj.getSt2_vs2_bag4_rb(); comparing(x, y); } else{ for(i=0; i<(the number of variables your comparing in the if condition above); i++) { z.add(0.0); } } } dif.put("", z); return dif; } public void comparing(Double x1, Double y1) { if ((x1 > y1)) { z.add(x1 - y1); System.out.println("value of z is" + z); } else { z.add(y1 - x1); } }
这可以解决该问题。 但是我还没有使用自写类在对象上尝试过。
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
/*Put values to both lists*/
list1.add("John");
list1.add("Doe");
list2.add("John");
list2.add("Doe");
/*Add other objects to list2. This will be the differences*/
list2.add("Jane");
list2.add("Roe");
System.out.println("Printing two lists before retaining differences");
System.out.println("list1: "+list1);
System.out.println("list2: "+list2+"\n");
System.out.println("Similarities removed. Differences stored to list2");
list2.removeAll(list1);
System.out.println("differences: "+list2);
}
}
我不知道您进行比较的算法是否正确。 例如,如果x1 = -1和x2 = 5: x2 - x1 = 5 - (-1)
= 5 + 1
= 6
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Double> list1 = new ArrayList<Double>();
List<Double> list2 = new ArrayList<Double>();
List<Double> result = new ArrayList<Double>(); //where result will be stored
//Load with values
list1.addAll(Arrays.asList(new Double[]{3.0,-5.0,7.0,-9.0,11.0,-13.0}));
list2.addAll(Arrays.asList(new Double[]{1.0,1.0,1.0,1.0,1.0,1.0}));
System.out.println("list1: "+list1);
System.out.println("list2: "+list2);
for(int i=0; i<list1.size() && i<list2.size(); i++) {
double d1 = list1.get(i);
double d2 = list2.get(i);
//If d1 is greater than d2, do d1 minus d2 otherwise do d2 minus d1
//I don't know if that is really the purpose of doing the difference in your code
//Store result to result list
result.add( d1>d2 ? d1-d2 : d2-d1);
}
//Print the result
System.out.println("result: "+result);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.