java tutorial - Java TreeMap example - java programming - learn java - java basics - java for beginners



 Simple Java TreeMap

Learn java - java tutorial - Simple Java TreeMap - java examples - java programs

Java TreeMap example:

  • TreeMap is Red-Black tree based NavigableMap implementation. It is sorted according to the natural ordering of its keys.
  • The TreeMap class implements Map interface same to HashMap class. The main difference between them is HashMap is an unordered collection while TreeMap is sorted in the ascending order of its keys. It is unsynchronized collection class that means it is not appropriate for thread-safe operations until unless synchronized clearly.
 Simple Java TreeMap

Learn java - java tutorial - Simple Java TreeMap - java examples - java programs

  • Java HashSet class is used to create a collection which uses a hash table for storage. It inherits the AbstractSet class as well as implements Set interface.
  • The main points concerning Java HashSet class are:
  • HashSet stores the elements by using a mechanism named hashing.
  • HashSet contains unique elements only.

Difference between List and Set :

  • List can contain duplicate elements whereas Set contains unique elements only.

Hierarchy of HashSet class:

  • The HashSet class extends AbstractSet class which implements Set interface. The Set interface inherits Collection and Iterable interfaces in hierarchical order.

HashSet class declaration :

  • Let's see the declaration for java.util.HashSet class.
  • public class HashSet<E> extends AbstractSet<E>implements Set<E>, Cloneable, Serializable

sample code:

import java.util.TreeMap;
 
public class JavaTreeMapExample {
 
    public static void main(String[] args) {
 
       
        TreeMap treeroute = new TreeMap();
        treeroute.put("One", new Integer(1));
        treeroute.put("Two", new Integer(2));
         Object objOne = treeroute.get("One");
        System.out.println(objOne);
        Object objTwo = treeroute.get("Two");
        System.out.println(objTwo);
 
    }
}

Output:

1
2

Related Searches to Java TreeMap example