Skip to content

Commit 23efa1f

Browse files
committed
Add TreeMap module (cont'd)
1 parent e48e117 commit 23efa1f

File tree

8 files changed

+167
-8
lines changed

8 files changed

+167
-8
lines changed
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
1-
package collections.treemap;public class FetchLargestKey {
1+
package collections.treemap;
2+
3+
import java.util.Map.Entry;
4+
import java.util.TreeMap;
5+
6+
public class FetchLargestKey {
7+
public static void main(String[] args) {
8+
TreeMap<String, Integer> map = new TreeMap<>();
9+
map.put("Oracle", 43);
10+
map.put("Microsoft", 56);
11+
map.put("Apple", 43);
12+
map.put("Novartis", 87);
13+
14+
// Fetching the first entry in the Map.
15+
Entry<String, Integer> firstEntry = map.firstEntry();
16+
17+
System.out.println("Smallest key: " + firstEntry.getKey() + ", Value: " + firstEntry.getValue());
18+
19+
// Fetching the last entry in the Map.
20+
Entry<String, Integer> lastEntry = map.lastEntry();
21+
System.out.println("Largest key: " + lastEntry.getKey() + ", Value: " + lastEntry.getValue());
22+
}
223
}

src/main/java/collections/treemap/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class TreeMapDemo {
7777
System.out.println("Elements are stored in random order: " + hashMap);
7878

7979
// Creating a TreeMap using existing HashMap. This will store the elements in ascending order.
80-
TreeMap<String, Integer> treeMap1 = new HashMap<>(hashMap);
80+
TreeMap<String, Integer> treeMap1 = new TreeMap<>(hashMap);
8181
System.out.println("Elements are stored in ascending order:" + treeMap1);
8282

8383
// Creating a TreeMap using existing TreeMap. This will store the elements in the same order as it was in the passed Map.
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
1-
package collections.treemap;public class RemoveElement {
1+
package collections.treemap;
2+
3+
import java.util.TreeMap;
4+
5+
public class RemoveElement {
6+
public static void main(String[] args) {
7+
TreeMap<String, Integer> map = new TreeMap<>();
8+
9+
map.put("Oracle", 43);
10+
map.put("Microsoft", 56);
11+
map.put("Apple", 43);
12+
map.put("Novartis", 87);
13+
14+
System.out.println("Removing Oracle from Map. This will return the value corresponding to Oracle: " + map.remove("Oracle"));
15+
System.out.println("Removing Google from Map. This will return null as Google is not present in the Map: " + map.remove("Google"));
16+
}
217
}
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1-
package collections.treemap;public class ReplaceAfterCheckingPreviousValue {
1+
package collections.treemap;
2+
3+
import java.util.TreeMap;
4+
5+
public class ReplaceAfterCheckingPreviousValue {
6+
public static void main(String[] args) {
7+
TreeMap<String, Integer> map = new TreeMap<>();
8+
map.put("Oracle", 43);
9+
map.put("Microsoft", 56);
10+
map.put("Apple", 43);
11+
map.put("Novartis", 87);
12+
13+
System.out.println("Replacing the value of Oracle : " + map.replace("Oracle", 67));
14+
System.out.println("Latest value of Oracle : " + map.get("Oracle"));
15+
16+
System.out.println("Replacing the value of Apple only if current value is 50 : " + map.replace("Apple", 50, 90));
17+
System.out.println("Latest value of Oracle : " + map.get("Apple"));
18+
19+
System.out.println("Replacing the value of Apple only if current value is 76 : " + map.replace("Apple", 76, 90));
20+
System.out.println("Latest value of Oracle : " + map.get("Apple"));
21+
}
222
}
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
1-
package collections.treemap;public class SortByValues {
1+
package collections.treemap;
2+
3+
import java.util.Comparator;
4+
import java.util.TreeMap;
5+
6+
public class SortByValues {
7+
public static TreeMap<String, Integer> sortByValues(TreeMap<String, Integer> map) {
8+
Comparator<String> valueComparator = (k1, k2) -> {
9+
int comp = map.get(k1).compareTo(map.get(k2));
10+
if (comp == 0) { return 1; }
11+
else { return comp; }
12+
};
13+
14+
TreeMap<String, Integer> mapSortedByValues = new TreeMap<>(valueComparator);
15+
16+
mapSortedByValues.putAll(map);
17+
return mapSortedByValues;
18+
}
19+
20+
public static void main(String[] args) {
21+
TreeMap<String, Integer> map = new TreeMap<>();
22+
map.put("Oracle", 43);
23+
map.put("Microsoft", 56);
24+
map.put("Apple", 76);
25+
map.put("Novartis", 87);
26+
map.put("Google", 23);
27+
map.put("Audi", 101);
28+
29+
System.out.println(sortByValues(map));
30+
}
231
}
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1-
package collections.treemap;public class SortTreeMap {
1+
package collections.treemap;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.TreeMap;
6+
7+
public class SortTreeMap {
8+
public static void main(String[] args) {
9+
Map<Integer, String> employeeMap = new HashMap<>();
10+
employeeMap.put(123, "Alex");
11+
employeeMap.put(342, "Ryan");
12+
employeeMap.put(143, "Joe");
13+
employeeMap.put(234, "Allen");
14+
employeeMap.put(432, "Roy");
15+
16+
System.out.println("Unsorted map " + employeeMap);
17+
18+
TreeMap<Integer, String> sortedMap = new TreeMap<>();
19+
sortedMap.putAll(employeeMap);
20+
System.out.println("Sorted map " + sortedMap);
21+
}
222
}
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
1-
package collections.treemap;public class SortedMapConstructorDemo {
1+
package collections.treemap;
2+
3+
import java.util.Comparator;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.TreeMap;
7+
8+
public class SortedMapConstructorDemo {
9+
public static void main(String[] args) {
10+
// Creating a TreeMap which will store all the elements in reverse order.
11+
TreeMap<String, Integer> reverseMap = new TreeMap<>(Comparator.reverseOrder());
12+
reverseMap.put("Oracle", 43);
13+
reverseMap.put("Microsoft", 56);
14+
reverseMap.put("Apple", 43);
15+
reverseMap.put("Novartis", 87);
16+
System.out.println("Elements are stored in reverse order: " + reverseMap);
17+
18+
// Creating a HashMap which will store all the elements in random order.
19+
Map<String, Integer> hashMap = new HashMap<>();
20+
hashMap.put("Oracle", 43);
21+
reverseMap.put("Microsoft", 56);
22+
reverseMap.put("Apple", 43);
23+
reverseMap.put("Novartis", 87);
24+
System.out.println("Elements are stored in random order: " + hashMap);
25+
26+
// Creating a TreeMap using existing HashMap. This will store the elements in ascending order.
27+
TreeMap<String, Integer> treeMap1 = new TreeMap<>(hashMap);
28+
System.out.println("Elements are stored in ascending order:" + treeMap1);
29+
30+
// Creating a TreeMap using existing TreeMap. This will store the elements in the same order as it was in the passed Map.
31+
TreeMap<String, Integer> treeMap2 = new TreeMap<>(reverseMap);
32+
System.out.println("Elements are stored in descending order: " + treeMap2);
33+
}
234
}
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,24 @@
1-
package collections.treemap;public class TreeMapInsertDemo {
1+
package collections.treemap;
2+
3+
import java.util.TreeMap;
4+
5+
public class TreeMapInsertDemo {
6+
public static void main(String[] args) {
7+
TreeMap<String, Integer> map = new TreeMap<>();
8+
9+
map.put("Oracle", 43);
10+
map.put("Microsoft", 56);
11+
map.put("Apple", 43);
12+
map.put("Novartis", 87);
13+
14+
System.out.println(map);
15+
16+
TreeMap<String, Integer> finalMap = new TreeMap<>();
17+
18+
map.put("Google", 65);
19+
map.put("Audi", 32);
20+
finalMap.putAll(map);
21+
22+
System.out.println(finalMap);
23+
}
224
}

0 commit comments

Comments
 (0)