简体   繁体   English

Java中两个List的比较,并使用JSTL在JSP中以表格格式显示两个List之间的差异

[英]Comparison of two List in Java and displaying the difference between two List in tabular format in JSP using JSTL

I have two Arraylist namely- 我有两个Arraylist-

    List<java_test> one=new ArrayList<java_test>();
    List<java_test> two=new ArrayList<java_test>();

I have two methods which returns the defined ArrayList values individually. 有两种方法可以分别返回定义的ArrayList值。 the first arraylist ie one has more values as compared to second arraylist ie two. 与第二个数组列表(即两个)相比,第一个数组列表(即一个)具有更多的值。

Now I want to 现在我想

compare the values of both the array list and find out there difference 比较两个数组列表的值并找出差异

and display that in tabular format.Moreover as the fi rst Arraylsit one has more values as compared to two .So when there is only values of one arraylsit 0 should be displayed as there difference. 此外,由于第一个Arrayls的要比两个 数组,因此,当只有一个Arraylsit时,应该显示0 ,因为两者之间存在差异。

EDIT-1 编辑-1

I tried to achieve calculate difference as- 我试图实现计算差异为-

  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; } 

The output I receive is- 我收到的输出是- 在此处输入图片说明 But It does not give correct difference.Whats the mistake I have done.? 但这并没有给出正确的区别。我犯了什么错误?

If i have understood your question correctly. 如果我正确理解了您的问题。 What you want to do is print the difference if both the array list has values for same index or print zero when only one array list has value for the same index. 您想要做的是,如果两个数组列表都具有相同索引的值,则打印差异;如果只有一个数组列表具有相同索引的值,则打印零。

for this there are two possibilities. 为此,有两种可能性。

If your array list only contains non-zero value. 如果您的数组列表仅包含非零值。 You could change the condition of for loop as follows 您可以如下更改for循环的条件

for (int i = 0; i < one.size(); i++) // since you have already specified first array list is larger than the second.

Then change the comparing function as follows: 然后按如下所示更改比较功能:

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;
 }

If your arraylist contains zero values. 如果您的arraylist包含零值。 I would suggest doing something like this 我建议做这样的事情

  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); } } 

This may solve that problem. 这可以解决该问题。 But I haven't tried it yet on an object using a self-written class. 但是我还没有使用自写类在对象上尝试过。

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);
}

} }

I don't know if your algorithm in doing the comparison is right. 我不知道您进行比较的算法是否正确。 For example if x1 = -1 and x2 = 5: x2 - x1 = 5 - (-1) = 5 + 1 = 6 例如,如果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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM