Search code examples
javalistobjectcastingsubtraction

How to cast from class type object to double type in java


I have a method to subtract two values from two different list which are of class type.The code for subtraction is

public LinkedHashMap<String, List<Double>> diff()
  { 
      List<Double> z=new ArrayList<Double>();
     for(int i=0;i<st_jsp.size()&&i<ref_jsp.size();i++){
         Double x=(double) ref_jsp.get(i);
         Double y=st_jsp.get(i);

          if(x>y){
              z.add(x-y);
          }
          else{
          z.add(y-x);
          }

          dif.put("", z);
       }

    return dif;

  }

Here st_jsp and ref_jsp are two list such as List<Ref_user_interface> st_jsp=new ArrayList<Ref_user_interface>(); and List<Ref_user_interface> st_jsp=new ArrayList<Ref_user_interface>(); Ref_user_interface is my class name. Thses two list holds value of in following way-

 ref.setSt1_vs1_bag6_rb(rs.getDouble(8));
 ref.setSt1_vs1_bag7_rb(rs.getDouble(9));
 ref.setSt1_vs1_bag8_rb(rs.getDouble(10));
 st_jsp.add(ref);

where ref is Ref_user_interface ref=new Ref_user_interface(); In my subtract method ,I'm getting an ** cannot convert from Ref_user_interface to Double** on line Double x= ref_jsp.get(i);Double y=st_jsp.get(i);. Is there any way through which I can subtract my values from two list whcih hold data of object type.?

EDIT-1 I have getter and setter methods.But for each value of list,Do I have to include getter methods??For my getter and setter method please visit the link setter method of mine

EDIT-2

I have changed my subtraction method by all your answers as-

public LinkedHashMap<String, List<Double>> diff()
  {  

     List<Double> z=new ArrayList<Double>();

     for(int i=0;i<st_jsp.size()&&i<ref_jsp.size();i++)
     {
            Comaprision refObj = ref_jsp.get(i);
            Comaprision stObj = st_jsp.get(i);
            Double x =refObj.getBeam_current();
            x+=refObj.getBeam_energy();
            x+=refObj.getSt2_vs2_bag1_rb();
            x+=refObj.getSt2_vs2_bag2_rb();
            x+=refObj.getSt2_vs2_bag3_rb();
            x+=refObj.getSt2_vs2_bag4_rb();
            x+=refObj.getSt2_vs2_bag5_rb();
            x+=refObj.getSt2_vs2_bag6_rb();
            x+=refObj.getSt2_vs2_bag7_rb();
            x+=refObj.getSt2_vs2_bag8_rb();
            x+=refObj.getSt2_vs2_bag9_rb();
             Double y = stObj.getBeam_current();
            y+=stObj.getBeam_energy();
            y+=stObj.getSt2_vs2_bag1_rb();
            y+=stObj.getSt2_vs2_bag2_rb();
            y+=stObj.getSt2_vs2_bag3_rb();
            y+=stObj.getSt2_vs2_bag4_rb();
            y+=stObj.getSt2_vs2_bag5_rb();
            y+=stObj.getSt2_vs2_bag6_rb();
            y+=stObj.getSt2_vs2_bag7_rb();
            y+=stObj.getSt2_vs2_bag8_rb();



          if((x>y)){

              z.add(x-y);
          }
          else{
          z.add(y-x);

          dif.put("", z);

       }
    return dif;

  }

but I can't understand that whether all the values at 1 index are being added and then comapred with y at index 1 and so on .Moreover this loop gives me 42 values.My ref_jsp has 21 rows and st_jsp has 78 rows but value of these rows are being repeated twice.I can't understand this for loop.Please help me to explain.I don't want to add up my values at a index,

My actual problem is to -->I just want to compare value at getter of each index of x with value at getter of each index of y and find the difference.


Solution

  • the thing is your list returns the object of Ref_user_interface so you can get value by calling the getter method like

    ref_jsp.get(i).getSt1_vs1_bag6_rb()
    

    and if you want all the getter methods the try this in your for loop

    for(int i=0;i<st_jsp.size()&&i<ref_jsp.size();i++){
        Ref_user_interface refObj = ref_jsp.get(i);
        Ref_user_interface stObj = st_jsp.get(i);
        Double x = refObj.getSt1_vs1_bag6_rb();
        Double y = stObj.getSt1_vs1_bag6_rb();
        comparing(x,y);
        x =refObj.getSt1_vs1_bag7_rb();
        y = stObj.getSt1_vs1_bag7_rb();
        comparing(x,y)
    
         // and so on for all the getters method you have for this obj
    
         public void comparing(Double x,Double y){
               // write your comparing logic here and add value in map
              if(x > 0 && y > 0){
                  if(x>y){
                     z.add(x-y);  
                  }else{
                     z.add(y-x); 
                  }  
              }else if(x>0){
                 z.add(x);
              }else{
                 z.add(y);
              }     
         }
    

    and so on you get all the values you want and then use it according to your logic.