|
|
Program: Basic TreeMap Operations.
Description: |
Below example shows basic operations on TreeMap like creating an object, adding key-value pair objects,
gtting value by passing key object, checking whether the map has elements or not, deleting specific entry, and size of the
TreeMap.
|
Code: |
package com.java2novice.treemap;
import java.util.TreeMap;
public class MyBasicOperations {
public static void main(String a[]){
TreeMap<String, String> hm = new TreeMap<String, String>();
//add key-value pair to TreeMap
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third","THIRD INSERTED");
System.out.println(hm);
//getting value for the given key from TreeMap
System.out.println("Value of second: "+hm.get("second"));
System.out.println("Is TreeMap empty? "+hm.isEmpty());
hm.remove("third");
System.out.println(hm);
System.out.println("Size of the TreeMap: "+hm.size());
}
}
|
|
Output: |
{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED}
Value of second: SECOND INSERTED
Is TreeMap empty? false
{first=FIRST INSERTED, second=SECOND INSERTED}
Size of the TreeMap: 2
|
|
|
|
|
List Of All TreeMap Sample Programs:- Basic TreeMap Operations.
- How to iterate through TreeMap?
- How to copy Map content to another TreeMap?
- How to search a key in TreeMap?
- How to search a value in TreeMap?
- How to get all keys from TreeMap?
- How to get entry set from TreeMap?
- How to delete all elements from TreeMap?
- How to sort keys in TreeMap by using Comparator?
- How to sort keys in TreeMap by using Comparator with user define objects?
- How to get sorted sub-map from TreeMap?
- How to get first key element from TreeMap (Sorted Map)?
- How to get last key element from TreeMap (Sorted Map)?
- How to reverse sorted keys in a TreeMap?
|
|
|
Purpose of garbage collection
The garbage collection process is to identify the objects which are
no longer referenced or needed by a program so that their resources can be
reclaimed and reused. These identified objects will be discarded.
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- Albert Einstein
|