forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedMapDemo.java
More file actions
30 lines (28 loc) · 865 Bytes
/
Copy pathSortedMapDemo.java
File metadata and controls
30 lines (28 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapDemo{
public static void main(String[] args){
SortedMap<String, Integer> smsi = new TreeMap<String, Integer>();
String[] officeSupplies = {
"Pen", "pencil", "legal pad", "CD", "paper"
};
int[] quantities = {
20, 30, 5, 10, 20
};
for(int i=0;i<officeSupplies.length; i++){
smsi.put(officeSupplies[i], quantities[i]);
}
System.out.println(smsi);
System.out.println(smsi.headMap("pencil"));
System.out.println(smsi.headMap("paper"));
SortedMap<String, Integer> smsiCopy = new TreeMap<String, Integer>(new Comparator<String>(){
@Override
public int compare(String key1, String key2){
return key2.compareTo(key1); // reverse as the natural order
}
});
smsiCopy.putAll(smsi);
System.out.println(smsiCopy);
}
}