TreeMap Example Java Program
Definition
Maps are defined by the java.util.Map interface in Java. Maps are simple data structures that associate a key with a value. The element is the value. This lets the map be very flexible. If the key is the hash code of the element, the map is essentially a set. If it's just an increasing number, it becomes a list. Methods can be called that find the key or map entry that's closest to the given key in either direction. The map can also be reversed, and an iterator in reverse order can be generated from it. It's implemented by java.util.TreeMap.Syntax
TreeMapmarks = new TreeMap ();
TreeMap Example Program
import java.util.TreeMap; public class TreeMapDemo { public static void main(String[] args) { TreeMapmarks = new TreeMap (); marks.put("Student1", 120); marks.put("Student2", 190); marks.put("Student3", 89); marks.put("Student4", 142); for(String key: marks.keySet()){ System.out.println(key +" : "+ marks.get(key)); } } }
Sample Output
Output is: Student1 : 120 Student2 : 190 Student3 : 89 Student4 : 142
Java Collection Programs
- Linked List Example Java Program
- ArrayList Example Java Program
- HashSet Example Java Program
- Clear an Arraylist Example Java Program
- Reversing an ArrayList Example Java Program
- Linked HashSet Example Java Program
- HashMap Example Java Program
- Set Value in ArrayList Example Java Program
- Linked HashMap Example Java Program
- Remove Element from ArrayList Example Java Program
- Identity HashMap Example Java Program
- TreeMap Example Java Program
- TreeSet Example Java Program
- Finding Duplicates in Array Using TreeSet Java Example Program
- HashTable Example Java Program
- EnumSet Example Java Program
- Enum Map Example Java Program
- Wrapper Example Java Program
- Un Modifiable Wrapper Example Java Program
- Iterator Interface Example Java Program
- Sorted Map Interface Example Java Program
- Sorted Set Interface Example Java Program
Read More Articles
- Multiple Inheritance Using Interface Example Java Program
- Single Inheritance Example Java Program
- Multilevel Inheritance Example Java Program
- Hierarchical Inheritance Example Java Program
- Find all Substrings of a given string Example Java Program
- Create Matrix Example Java Program
- Sum Of Three Numbers Example Java Program
- Heap Sort Example Java Program
- Twin Prime Example Java Program
- Compile Time Polymorphism Example Java Program