Java TreeMap

TreeMap is a class which is available in java.util package. The TreeMap is a class of the Java collections framework provides the functionality of tree data structure. In Java TreeMap, elements are stored in key/value pairs. Keys are unique values associated with individual values. TreeMap cannot contains duplicate keys.

TreeMap Syntax:
                        
                            TreeMap<K, V> numbers = new TreeMap<>();
                            TreeMap<String, Integer> numbers = new TreeMap<>();
                        
                    
Basic Operations on Java LinkedHashMap
  1. 1. Insert Elements to TreeMap
  2. 2. Access TreeMap Elements
  3. 3. Remove TeeMap Elements
  4. 4. Replace TreeMap Elements
Methods of TreeMap
Method Name Description
put() inserts the specified key/value mapping to the map.
putAll() inserts all the entries from the specified map to this map.
putIfAbsent() inserts the specified key/value mapping to the map if the specified key is not present in the map.
entrySet() returns a set of all the key/values mapping (entry) of a treemap.
keySet() returns a set of all the keys of the map.
values() returns a set of all the values of the map.
get() Returns the value associated with the specified key. Returns null if the key is not found.
getOrDefault() Returns the value associated with the specified key. Returns the specified default value if the key is not found.
remove(key) returns and removes the entry associated with the specified key from the TreeMap.
remove(key, value) removes the entry from the map only if the specified key is associated with the specified value and returns a boolean value.
replace(key, value) replaces the value mapped by the specified key with the new value.
replace(key, old, new) replaces the old value with the new value only if the old value is already associated with the specified key.
replaceAll(function) replaces each value of the map with the result of the specified function.
clear() removes all the entries from the map.
containsKey() checks if the map contains the specified key and returns a boolean value.
containsValue() checks if the map contains the specified value and returns a boolean value.
size() returns the size of the map.
isEmpty() checks if the map is empty and returns a boolean value.
1. Insert Elements to TreeMap
                        
                            import java.util.TreeMap;

                            class Main {
                                public static void main(String[] args) {
                                    // Creating TreeMap of even numbers
                                    TreeMap<String, Integer> evenNumbers = new TreeMap<>();

                                    // Using put()
                                    evenNumbers.put("Two", 2);
                                    evenNumbers.put("Four", 4);

                                    // Using putIfAbsent()
                                    evenNumbers.putIfAbsent("Six", 6);
                                    System.out.println("TreeMap of even numbers: " + evenNumbers);

                                    //Creating TreeMap of numbers
                                    TreeMap<String, Integer> numbers = new TreeMap<>();
                                    numbers.put("One", 1);

                                    // Using putAll()
                                    numbers.putAll(evenNumbers);
                                    System.out.println("TreeMap of numbers: " + numbers);
                                }
                            }
                        
                    
2. Access TreeMap Elements
                        
                            import java.util.TreeMap;

                            class Main {
                                public static void main(String[] args) {
                                    TreeMap<String, Integer> numbers = new TreeMap<>();

                                    numbers.put("One", 1);
                                    numbers.put("Two", 2);
                                    numbers.put("Three", 3);
                                    System.out.println("TreeMap: " + numbers);

                                    // Using entrySet()
                                    System.out.println("Key/Value mappings: " + numbers.entrySet());

                                    // Using keySet()
                                    System.out.println("Keys: " + numbers.keySet());

                                    // Using values()
                                    System.out.println("Values: " + numbers.values());

                                    // Using get()
                                    int value1 = numbers.get("Three");
                                    System.out.println("Using get(): " + value1);

                                    // Using getOrDefault()
                                    int value2 = numbers.getOrDefault("Five", 5);
                                    System.out.println("Using getOrDefault(): " + value2);
                                }
                            }
                        
                    
3. Removed TeeMap Elements
                        
                            import java.util.TreeMap;

                            class Main {
                                public static void main(String[] args) {

                                    TreeMap<String, Integer> numbers = new TreeMap<>();
                                    numbers.put("One", 1);
                                    numbers.put("Two", 2);
                                    numbers.put("Three", 3);
                                    System.out.println("TreeMap: " + numbers);

                                    // remove method with single parameter
                                    int value = numbers.remove("Two");
                                    System.out.println("Removed value: " + value);

                                    // remove method with two parameters
                                    boolean result = numbers.remove("Three", 3);
                                    System.out.println("Is the entry {Three=3} removed? " + result);

                                    System.out.println("Updated TreeMap: " + numbers);
                                }
                            }
                        
                    
Replace TreeMap Elements
                        
                            import java.util.TreeMap;

                            class Main {
                                public static void main(String[] args) {

                                    TreeMap<String, Integer> numbers = new TreeMap<>();
                                    numbers.put("First", 1);
                                    numbers.put("Second", 2);
                                    numbers.put("Third", 3);
                                    System.out.println("Original TreeMap: " + numbers);

                                    // Using replace()
                                    numbers.replace("Second", 22);
                                    numbers.replace("Third", 3, 33);
                                    System.out.println("TreeMap using replace: " + numbers);

                                    // Using replaceAll()
                                    numbers.replaceAll((key, oldValue) -> oldValue + 2);
                                    System.out.println("TreeMap using replaceAll: " + numbers);
                                }
                            }