sort a map based on keys and values using java 8
Java
Map< Integer, String> map=new HashMap<Integer, String>();
map.put(101, "Hemendra");
map.put(99, "Andrew");
map.put(103, "Anish");
map.put(18, "Mohan");
map.put(11, "Christine");
map.put(109, "Rebeca");
map.put(111, "David");
map.put(19, "Rahim");
map.put(10, "Krishna");
Required to sort the map:
1. On the basis of keys:
[10=Krishna, 11=Christine, 18=Mohan, 19=Rahim, 99=Andrew, 101=Hemendra, 103=Anish, 109=Rebeca, 111=David]
2. On the basis of values:
[99=Andrew, 103=Anish, 11=Christine, 111=David, 101=Hemendra, 10=Krishna, 18=Mohan, 19=Rahim, 109=Rebeca]
Solution:
System.out.println(map.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList()));
System.out.println(map.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toList()));
Also in Java:
- Title
- how to add to a file in java
- Category
- Java
- Title
- remove last character from string java
- Category
- Java
- Title
- error message pushes button down
- Category
- Java
- Title
- how to echo java_home in windows cmd
- Category
- Java
- Title
- method resolve file in java
- Category
- Java
- Title
- java setroundingmode
- Category
- Java
- Title
- sum of arraylist java 8
- Category
- Java
- Title
- spring framework iterate
- Category
- Java
- Title
- get drawable with string android java
- Category
- Java
- Title
- hide elements android
- Category
- Java
- Title
- java do while
- Category
- Java
- Title
- crit chance in java
- Category
- Java
- Title
- java 8 stream group by example
- Category
- Java
- Title
- java 8 random string generator
- Category
- Java
- Title
- java if statement string equals scanner
- Category
- Java
- Title
- maximum arrays size in java
- Category
- Java
- Title
- java identifiers
- Category
- Java
- Title
- javadoc reference general toString
- Category
- Java
- Title
- loop java
- Category
- Java
- Title
- java 8 string to localdate
- Category
- Java
- Title
- get first 5 characters of string java
- Category
- Java
- Title
- get method of a class which I only have string to
- Category
- Java
- Title
- how to create a linked list in java
- Category
- Java
- Title
- how to initialize an array in java
- Category
- Java
- Title
- full shutdown windows 10 cmd
- Category
- Java
- Title
- what is a method example in java
- Category
- Java
- Title
- are strings modifiable
- Category
- Java
- Title
- java foreach map
- Category
- Java
- Title
- Matlab java.lang.IllegalArgumentException: Invalid range
- Category
- Java
- Title
- Java If statemtn
- Category
- Java
- Title
- java string to double
- Category
- Java
- Title
- how to get the width and height of a string in java
- Category
- Java
- Title
- insertion sort doubly linked list java
- Category
- Java
- Title
- test function that call a function javascrip
- Category
- Java
- Title
- how to add an image to a gui in java windowbuilder
- Category
- Java
- Title
- compareto method java
- Category
- Java
- Title
- bootstrap center text vertically
- Category
- Java
- Title
- html redirect
- Category
- Java
- Title
- how to create array of linkedlist in java
- Category
- Java
- Title
- code to include layout from java in android
- Category
- Java
- Title
- java foreach
- Category
- Java
- Title
- libgdx move camera with touch
- Category
- Java
- Title
- java jpa criteriabuilder in xample
- Category
- Java
- Title
- java how to show an array
- Category
- Java
- Title
- java string contains char
- Category
- Java
- Title
- sending a excel in an attachment in email java
- Category
- Java
- Title
- java create file
- Category
- Java
- Title
- log10 in java
- Category
- Java
- Title
- java check if element exists in array
- Category
- Java
- Title
- spigot custom join message
- Category
- Java
- Title
- logger in java
- Category
- Java
- Title
- how to make javafx full screen default
- Category
- Java
- Title
- java thread class sleep
- Category
- Java
- Title
- how to get individual words from a string in java
- Category
- Java
- Title
- java int to hex fixed length
- Category
- Java
- Title
- how to select a element in an arraylist java
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- boolean java.lang.String.equals(java.lang.Object)' on a null object reference
- Category
- Java
- Title
- how to create a java jframe
- Category
- Java
- Title
- Error executing Maven. java.io.FileNotFoundException: The specified user settings file does not exist: /etc/java-8-openjdk
- Category
- Java
- Title
- basic java coding
- Category
- Java
- Title
- how to make a variable unchangeable in java
- Category
- Java
- Title
- how to make a list java
- Category
- Java
- Title
- take string from keyboard java
- Category
- Java
- Title
- java string to integer
- Category
- Java
- Title
- android studio remove notification bar
- Category
- Java
- Title
- number of matches regex java
- Category
- Java
- Title
- how to get token oauth2.0 java example response
- Category
- Java
- Title
- displaying an arraylist in java
- Category
- Java
- Title
- icon label java
- Category
- Java
- Title
- java script using navigator.useragent to detect the browse
- Category
- Java
- Title
- how to iterate list in java selenium
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- kotlin android intent pass data
- Category
- Java
- Title
- how to replace in java
- Category
- Java
- Title
- leap year program in java
- Category
- Java
- Title
- java how to serialize a file into a local server
- Category
- Java
- Title
- string to double java
- Category
- Java
- Title
- get value textfield java
- Category
- Java
- Title
- how to check internet is working or not in java
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- java get excectuon time
- Category
- Java
- Title
- java implement interface
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- how to print in java
- Category
- Java
- Title
- java append to array
- Category
- Java
- Title
- import java.util.*;
- Category
- Java
- Title
- how to create a draw Rectangle in java
- Category
- Java
- Title
- string equality in java
- Category
- Java
- Title
- java switch case
- Category
- Java
- Title
- javafx polygon
- Category
- Java
- Title
- java mysql springboot jpa stackoverflow
- Category
- Java
- Title
- Java create array of array
- Category
- Java
- Title
- dataframe to dict without index
- Category
- Java
- Title
- ARE THERE POINTER IN JAVA
- Category
- Java
- Title
- how do I change the opacity of a JButton
- Category
- Java
- Title
- serialversionuid java
- Category
- Java
- Title
- what is a float java
- Category
- Java
- Title
- java newinstance alternative
- Category
- Java
- Title
- java read integer from text file into array scanner
- Category
- Java