![]() ("Sort map by value in Descending Order => "+ sortByValueInReverse) ("Sort map by value in Ascending Order => "+ sortByValueInAscd) ![]() ("Sort map by key Alphabetically Descending order=> "+ sortAlphabatDesc) ("Sort map by key Alphabetically Ascending Order => "+ sortAlphabat) So we have cover all the scenerio for sort hash map so your complete class will look like: sorted(paring(Map.Entry::getValue)).map(Map.Entry::getKey).collect(Collectors.toList()) List sortByValueAndGetListOfKeys = ratingMap.entrySet().stream() Sort Map by values and Return List of keys So for that, we can return the List of movies instead of the hash map after sorting the map. What if you need only the movie’s name on the basis of rating ascending order. collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue,LinkedHashMap::new)) sorted((paringInt(ratinCustomSeq::indexOf))) HashMap sortB圜ustomSeq = ratingMap.entrySet().stream() So for example we have a custom list in which we define the rating sequence and we want to sort the map by this custom list. ![]() Sometimes we need to sort our map on the basis of some custom sequence. sorted((Comparator.reverseOrder())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)) HashMap sortByValueInReverse = ratingMap.entrySet().stream() ![]() So the movie that has the highest rating will come first and so on. Now we sort the ratings in the descending orders. Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)) sorted((Comparator.naturalOrder())).collect(Collectors.toMap( HashMap sortByValueInAscd = ratingMap.entrySet().stream() So after Sort, we will get the result in ascending order on the basis of ratings. In our case, we have ratings as hash map values. HashMap sortAlphabatDesc = ratingMap.entrySet().stream() Sort Map by Key Alphabetically In Descending order collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, sorted((Comparator.naturalOrder().toString().CASE_INSENSITIVE_ORDER)) HashMap sortAlphabat = ratingMap.entrySet().stream() So after sorting this map we will get all the movies in ascending order. In Our case we have movie’s name as keys. Now we will sort this map on the basis of above scenerios: Sort Map by key Alphabetically So take an example of a hash map that contains the movie name as the key.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |