In this example we shall show you how to make use of Java Sorted Map. A SortedMap
is a Map
that sort its entries in ascending order according to the keys’ natural ordering, or according to a Comparator
provided at the time of the SortedMap
creation. All keys inserted into a SortedMap
must implement the Comparable
interface (or be accepted by the specified Comparator
). Furthermore, all such elements must be mutually comparable (i.e, Mutually Comparable simply means that two objects accept each other as the argument to their compareTo
method), If you try to sort keys which do not implement Comparable
or not has a specific Comparator
, a ClassCastException
will be thrown.
Tip 2
All SortedMap
implementation classes should provide four “standard” constructors as the following:
- A void (no arguments) constructor, which creates an empty
SortedMap
sorted according to the natural ordering of its keys.
1 |
SortedMap sortedMap= new TreeMap(); |
- A constructor with a single argument of type
Comparator
, which creates an empty SortedMap
sorted according to the specified Comparator
.
1 |
Comparator comparator = new MyComparator(); |
2 |
SortedMap sortedMap = new TreeMap(comparator); |
- A constructor with a single argument of type
Map
, which creates a new Map
with the same key-value mappings as its argument, sorted according to the keys’ natural ordering.
1 |
Map map = new HashMap(); |
2 |
SortedMap sortedMap = new TreeMap(map); |
- A constructor with a single argument of type
SortedMap
, which creates a new SortedMap
with the same key-value mappings and the same ordering as the input SortedMap
.
1 |
SortedMap sortedMap= new TreeMap(); |
2 |
SortedMap newSortedMap = new TreeMap(sortedMap); |
1. SortedMap Operations:
The SortedMap
interface provides operations for normal Map operations and for the following:
- Range view — performs arbitrary range operations on the
SortedMap
subMap(K fromKey, K toKey)
: Returns a view of the portion of this Map
whose keys range from fromKey, inclusive, to toKey, exclusive.
headMap(K toKey)
: Returns a view of the portion of this Map
whose keys are strictly less than toKey.
tailMap(K fromKey)
: Returns a view of the portion of this Map
whose keys are greater than or equal to fromKey.
- Endpoints — returns the first or the last key in the
SortedMap
firstKey()
: Returns the first (lowest) key currently in this Map
.
lastKey()
: Returns the last (highest) key currently in this Map
.
- Comparator access — returns the
Comparator
, if any, used to sort the map
comparator()
: Returns the Comparator
used to order the keys in this Map
, or null if this Map
uses the natural ordering of its keys.
2. Example:
2.1. SortMapExample.java
01 |
package com.jcg.util.map; |
03 |
import java.util.Comparator; |
04 |
import java.util.HashMap; |
06 |
import java.util.TreeMap; |
12 |
public class SortMapExample { |
17 |
* @param args the arguments |
19 |
public static void main(String[] args) { |
21 |
Map unsortMap = new HashMap(); |
22 |
unsortMap.put( 10 , "Ashraf" ); |
23 |
unsortMap.put( 5 , "Sara" ); |
24 |
unsortMap.put( 6 , "Mohamed" ); |
25 |
unsortMap.put( 20 , "Esraa" ); |
26 |
unsortMap.put( 1 , "Bahaa" ); |
27 |
unsortMap.put( 7 , "Dalia" ); |
28 |
unsortMap.put( 8 , "Amira" ); |
29 |
unsortMap.put( 99 , "Ahmed" ); |
30 |
unsortMap.put( 50 , "Sama" ); |
31 |
unsortMap.put( 2 , "Nada" ); |
32 |
unsortMap.put( 9 , "Osama" ); |
34 |
System.out.println( "Unsort Map......" ); |
38 |
System.out.println( "
Sorted Map in ascending order......" ); |
39 |
Map ascSortedMap = new TreeMap(); |
40 |
ascSortedMap.putAll(unsortMap); |
41 |
printMap(ascSortedMap); |
44 |
System.out.println( "
Sorted Map in descending order......" ); |
45 |
Map desSortedMap = new TreeMap( |
49 |
public int compare(Integer o1, Integer o2) { |
50 |
return o2.compareTo(o1); |
54 |
desSortedMap.putAll(unsortMap); |
55 |
printMap(desSortedMap); |
64 |
public static void printMap(Map map) { |
65 |
for (Map.Entry entry : map.entrySet()) { |
66 |
System.out.println( "Key : " + entry.getKey() + " Value : " |
2.2. Explanation:
Let’s suppose that we want to sort a Map
which contains a group of employees according to their ids where we use the employee id as a key and the employee name as a value. After we create a SortedMap
using this Map
and the key of thisMap
is an Integer
type which implements the Comparable
interface, the keys are ordered in their natural ordering. Also, we can apply the descending order by creating our own Comparator
then passing it to the SortedMap
at creation time.
2.3. Output:
05 |
Key : 99 Value : Ahmed |
06 |
Key : 20 Value : Esraa |
08 |
Key : 6 Value : Mohamed |
12 |
Key : 10 Value : Ashraf |
14 |
Sorted Map in ascending order...... |
18 |
Key : 6 Value : Mohamed |
22 |
Key : 10 Value : Ashraf |
23 |
Key : 20 Value : Esraa |
25 |
Key : 99 Value : Ahmed |
27 |
Sorted Map in descending order...... |
28 |
Key : 99 Value : Ahmed |
30 |
Key : 20 Value : Esraa |
31 |
Key : 10 Value : Ashraf |
35 |
Key : 6 Value : Mohamed |