java - Why remove element of Map? -


i filtering list ahve same lat,long in 1 list , put same list , put list map code as:-

private collection<list<applicationdataset>> groupthelist(arraylist<applicationdataset> arraylist)   {     map<key, list<applicationdataset>> map = new hashmap<key, list<applicationdataset>>();     for(applicationdataset appset: arraylist)        {         key key = new key(appset.getlatitude(), appset.getlongitude());         list<applicationdataset> list = map.get(key);         if(list == null){             list = new arraylist<applicationdataset>();          }         list.add(appset);              map.put(key, list);     }     return map.values(); }   public class key {     string _lat;     string _lon;      key(string lat, string lon) {         _lat = lat;         _lon = lon;     }      @override     public boolean equals(object o) {         if (this == o) return true;         if (o == null || getclass() != o.getclass()) return false;          key key = (key) o;          if (!_lat.equals(key._lat)) return false;         if (!_lon.equals(key._lon)) return false;          return true;     }      @override     public int hashcode() {         int result = _lat.hashcode();         result = 31 * result + _lon.hashcode();         return result;     } } 

but when debuging code according xml come web-service there 2 list have same lat long , saving in same list in amp @ time of debuging when go next step of debug element of map have 2 item list decrease , showing size 1 unable rectify issue.

your code looks ok: you've overridden equals() , hashcode() consistently.

check whitespace in lat/lng values cause of problems, perhaps trim() in constructor:

key(string lat, string lon) {     _lat = lat.trim();     _lon = lon.trim(); } 

also, can simplify code this:

@override public boolean equals(object o) {     return o instanceof key         && _lat.equals(((key)o)._lat))         && _lon.equals(((key)o)._lon)); }  @override public int hashcode() {     // string.hashcode() sufficiently addition acceptable     return _lat.hashcode() + _lon.hashcode(); } 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -