0

Hi I have Two lists of objects :

public class TimeTableForBus  {

    String bsName;
    int bsType;
    Time ttTime;
    int lon;
    int lat;
    int ttID;
    int bus_stop_status;
}

And I generated two list just like this :

private static ArrayList  getList( QueryRunner qRunner,  Connection conn){
    try {
        beans = (List) qRunner.query(conn, "call mpklocal.LCD_GetDispInfoAllTimeTable()",
                  new BeanListHandler(TimeTableForBus.class));
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

      for (int i = 0; i < beans.size(); i++) {
          TimeTableForBus bean = (TimeTableForBus) beans.get(i);
//           bean.print();
      }
      ArrayList<TimeTableForBus> bus = new ArrayList<>();
      for (int i = 0; i < beans.size(); i++) {
          bus.add((TimeTableForBus) beans.get(i));
      }

    return bus;
  }

When I check if are equals I see false when I do this I see false . The list have this same objects:

  public static  boolean equalLists(List<TimeTableForBus> one, List<TimeTableForBus> two){     
        if (one == null && two == null){
            return true;
        }

        if((one == null && two != null) 
          || one != null && two == null
          || one.size() != two.size()){
            return false;
        }

        //to avoid messing the order of the lists we will use a copy
        //as noted in comments by A. R. S.
//      one = new ArrayList<String>(one); 
//      two = new ArrayList<String>(two);   
//
//      Collections.sort(one);
//      Collections.sort(two);      
        return one.equals(two);
    }

  public static  boolean listsAreEquivelent(List<? extends Object> a, List<? extends Object> b) {
        if(a==null) {
            if(b==null) {
                //Here 2 null lists are equivelent. You may want to change this.
                return true;
            } else {
                return false;
            }
        }
        if(b==null) {
            return false;
        }
        Map<Object, Integer> tempMap = new HashMap<>();
        for(Object element : a) {
            Integer currentCount = tempMap.get(element);
            if(currentCount == null) {
                tempMap.put(element, 1);
            } else {
                tempMap.put(element, currentCount+1);
            }
        }
        for(Object element : b) {
            Integer currentCount = tempMap.get(element);
            if(currentCount == null) {
                return false;
            } else {
                tempMap.put(element, currentCount-1);
            }
        }
        for(Integer count : tempMap.values()) {
            if(count != 0) {
                return false;
            }
        }
        return true;
    }

And I don't know why I have this result

  • 1
    Does `TimeTableForBus` override `equals`? – Eran Oct 06 '16 at 06:17
  • @Eran no , it have to override equals ? how it have to look like ? –  Oct 06 '16 at 06:18
  • @Eran and how I can use this ? –  Oct 06 '16 at 06:20
  • If you implement `public boolean equals (Object other)` in `TimeTableForBus` in a way that it would return true if you consider the two objects to be equal to each other, you'll be able to compare the List simply be calling `list1.equals(list2)` (assuming the elements appear in the same order in both lists). – Eran Oct 06 '16 at 06:22
  • @Eran I override equals but when I do list1.equals(list2) I see result false –  Oct 06 '16 at 06:29
  • @Eran Thank you for help . Now it work –  Oct 06 '16 at 06:49

1 Answers1

0

Try to override public boolean equals(Object o) and public int hashCode() like this:

public class TimeTableForBus  {

        String bsName;
        int bsType;
        Time ttTime;
        int lon;
        int lat;
        int ttID;
        int bus_stop_status;

        @Override
        public int hashCode() {
            int result = 31;

            result = 37 * result + generateHash(bsName);
            result = 37 * result + generateHash(bsType);
            result = 37 * result + generateHash(ttTime);
            result = 37 * result + generateHash(lon);
            result = 37 * result + generateHash(lat);
            result = 37 * result + generateHash(ttID);
            result = 37 * result + generateHash(bus_stop_status);

            return result;
        }

        @Override
        public boolean equals(Object o) {
            if (o == this)
                return true;
            if (!(o instanceof TimeTableForBus))
                return false;
            TimeTableForBus model = (TimeTableForBus)o;
            return Objects.equals(bsName, model.bsName)
                    && bsType == model.bsType
                    && Objects.equals(ttTime, model.ttTime)
                    && lon == model.lon
                    && lat == model.lat
                    && ttID == model.ttID
                    && bus_stop_status == model.bus_stop_status;
        }

        private int generateHash(long value) {
            return (int)(value ^ (value >>> 32));
        }

        private int generateHash(Object value) {
            return value == null ? 0 : value.hashCode();
        }

    }
MistaMek
  • 76
  • 2