Java 8 compare map by value bean's field -


how can create sorted map compares value's bean's field. map key integer , value car. i'd compare car's name it's can contains no letters.

something this: (it's not work, bad example)

comparator<car> carnamecomparator = new comparator<car>() {     @override      public int compare(car c1, car c2) {         return c1.getname().comparetoignorecase(c2.getname());     }      }; sortedmap<integer,car> carmap = new treemap<>(carnamecomparator); 

second try:

map<integer, car> carmap = new hashmap<>(); carmap.put(1, car1); carmap.put(2, car2);  map<integer, car> sortedbyvalue = carmap.entryset().stream()         .sorted(map.entry.<integer, car> comparingbyvalue(carnamecomparator))         .collect(collectors.tomap(map.entry::getkey, map.entry::getvalue, (e1, e2) -> e1, linkedhashmap::new));   sortedbyvalue.foreach((k,c)->system.out.println("key : " + k + " value : " + c.getname())); 

maybe second can right solution, know better solution, don't need create other map?


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -