Friday, 30 January 2015

Java : Collection Framework : TreeMap (Key and value)

TreeMapExample.java
```import java.util.Set;
import java.util.TreeMap;

/*
* Example of keySet() method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

treeMap.put(1, "Cat");
treeMap.put(2, "Dog");
treeMap.put(4, "Apple");
treeMap.put(3, "Ball");

System.out.println("treeMap : " + treeMap + "\n");

/*
* Returns a Set view of the keys contained in this map. The set is
* backed by the map, so changes to the map are reflected in the set,
* and vice-versa.
*
* If the map is modified while an iteration over the set is in progress
* (except through the iterator's own remove operation), the results of
* the iteration are undefined. The set supports element removal, which
* removes the corresponding mapping from the map, via the
* Iterator.remove, Set.remove, removeAll, retainAll, and clear
*/
Set<Integer> set = treeMap.keySet();

System.out.println("set : " + set + "\n");

System.out.println("-----------------------");
System.out.println("Key" + " | " + "value");
System.out.println("-----------------------");

for( Integer key : set )
{
String value = treeMap.get(key);
System.out.println(key + "   |  " + value);

}

}
}
```
Output
```treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

set : [1, 2, 3, 4]

-----------------------
Key | value
-----------------------
1   |  Cat
2   |  Dog
3   |  Ball
4   |  Apple
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (KeySet)

TreeMapExample.java
```import java.util.Set;
import java.util.TreeMap;

/*
* Example of keySet() method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

treeMap.put(1, "Cat");
treeMap.put(2, "Dog");
treeMap.put(4, "Apple");
treeMap.put(3, "Ball");

System.out.println("treeMap : " + treeMap +"\n");

/*
* Returns a Set view of the keys contained in this map. The set is
* backed by the map, so changes to the map are reflected in the set,
* and vice-versa.
*
* If the map is modified while an iteration over the
* set is in progress (except through the iterator's own remove
* operation), the results of the iteration are undefined. The set
* supports element removal, which removes the corresponding mapping
* from the map, via the Iterator.remove, Set.remove, removeAll,
* retainAll, and clear operations. It does not support the add or
*/
Set<Integer> set = treeMap.keySet();

System.out.println("set : " + set + "\n");

for( Integer key : set )
{
System.out.println(key);
}

}
}
```
Output
```treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

set : [1, 2, 3, 4]

1
2
3
4
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial

Java : Collection Framework : TreeMap (Remove)

TreeMapExample.java
```import java.util.TreeMap;

/*
* Example of remove(Object key) method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

treeMap.put(1, "Cat");
treeMap.put(2, "Dog");
treeMap.put(4, "Apple");
treeMap.put(3, "Ball");

System.out.println("treeMap : " + treeMap + "\n");

/*
* Removes the mapping for this key from this TreeMap if present.
*
* Returns: the previous value associated with key, or null if there was
* no mapping for key. (A null return can also indicate that the map
* previously associated null with key.)
*/

String value = treeMap.remove(3);

System.out.println("value : " + value + "\n");
System.out.println("treeMap : " + treeMap + "\n");

}
}
```
Output
```treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

value : Ball

treeMap : {1=Cat, 2=Dog, 4=Apple}
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (PutReturn)

TreeMapExample.java
```import java.util.TreeMap;

/*
* Example of put(K key,V value) method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

/*
* Associates the specified value with the specified key in this map. If
* the map previously contained a mapping for the key, the old value is
* replaced.
*
* Returns: the previous value associated with key, or null if there was
* no mapping for key. (A null return can also indicate that the map
* previously associated null with key.)
*/
String value = treeMap.put(1, "Apple");

System.out.println("Previous value associated with key '1' : " + value);
System.out.println("treeMap : " + treeMap + "\n");

value = treeMap.put(1, "Ball");

System.out.println("Previous value associated with key '1' : " + value);
System.out.println("treeMap : " + treeMap);

}
}
```
Output
```Previous value associated with key '1' : null
treeMap : {1=Apple}

Previous value associated with key '1' : Apple
treeMap : {1=Ball}
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (PutAll)

TreeMapExample.java
```import java.util.HashMap;
import java.util.TreeMap;

/*
* Example of putAll(Map<? extends K,? extends V> map) method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

hashMap.put(1, "Cat");
hashMap.put(2, "Dog");
hashMap.put(4, "Apple");
hashMap.put(3, "Ball");

System.out.println("hashMap : " + hashMap + "\n");

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

/*
* Copies all of the mappings from the specified map to this map. These
* mappings replace any mappings that this map had for any of the keys
* currently in the specified map.
*/
treeMap.putAll(hashMap);

System.out.println("treeMap : " + treeMap + "\n");

}
}
```
Output
```hashMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (Put)

TreeMapExample.java
```import java.util.TreeMap;

/*
* Example of put(K key,V value) method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

/*
* Associates the specified value with the specified key in this map. If
* the map previously contained a mapping for the key, the old value is
* replaced.
*/
treeMap.put(1, "Cat");
treeMap.put(2, "Dog");
treeMap.put(4, "Apple");
treeMap.put(3, "Ball");

System.out.println("treeMap : " + treeMap + "\n");

}
}
```
Output
```treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (Constructor Accepts Map)

TreeMapExample.java
```import java.util.HashMap;
import java.util.TreeMap;

/*
* Example of TreeMap(Map<? extends K,? extends V> m) Constructor.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

hashMap.put(1, "Cat");
hashMap.put(2, "Dog");
hashMap.put(4, "Apple");
hashMap.put(3, "Ball");

System.out.println("hashMap : " + hashMap + "\n");

/*
* Constructs a new tree map containing the same mappings as the given
* map, ordered according to the natural ordering of its keys. All keys
* inserted into the new map must implement the Comparable interface.
*/

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(hashMap);

System.out.println("treeMap : " + treeMap + "\n");

}
}
```
Output
```hashMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (ContainsValue)

TreeMapExample.java
```import java.util.TreeMap;

/*
* Example of containsValue(Object value) method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

treeMap.put(1, "Cat");
treeMap.put(2, "Dog");
treeMap.put(4, "Apple");
treeMap.put(3, "Ball");

System.out.println("treeMap : " + treeMap +"\n");

/*
* Returns true if this map maps one or more keys to the specified
* value.
*/
boolean isValueExist = treeMap.containsValue("Ball");

System.out.println("isValue 'Ball' Exist : " + isValueExist);

isValueExist = treeMap.containsValue("Eagle");

System.out.println("isValue 'Eagle' Exist : " + isValueExist);

}
}
```
Output
```treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

isValue 'Ball' Exist : true
isValue 'Eagle' Exist : false
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (ContainsKey)

TreeMapExample.java
```import java.util.TreeMap;

/*
* Example of containsKey(Object key) method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

treeMap.put(1, "Cat");
treeMap.put(2, "Dog");
treeMap.put(4, "Apple");
treeMap.put(3, "Ball");

System.out.println("treeMap : " + treeMap +"\n");

/*
* Returns true if this map contains a mapping for the specified key.
*/
boolean isKeyExist = treeMap.containsKey(2);

System.out.println("isKey '2' Exist : " + isKeyExist);

isKeyExist = treeMap.containsKey(8);

System.out.println("isKey '8' Exist : " + isKeyExist);

}
}
```
Output
```treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

isKey '2' Exist : true
isKey '8' Exist : false
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (Get)

TreeMapExample.java
```import java.util.TreeMap;

/*
* Example of get(Object key) method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

treeMap.put(1, "Cat");
treeMap.put(2, "Dog");
treeMap.put(4, "Apple");
treeMap.put(3, "Ball");

System.out.println("treeMap : " + treeMap +"\n");

/*
* Returns the value to which the specified key is mapped, or null if
* this map contains no mapping for the key.
*
* A return value of null does not necessarily indicate that the map
* contains no mapping for the key; it's also possible that the map
* explicitly maps the key to null. The containsKey operation may be
* used to distinguish these two cases.
*/
String value = treeMap.get(2);

System.out.println("value for the Key '2' : " + value);

value = treeMap.get(10);

System.out.println("value for the Key '10' : " + value);

}
}
```
Output
```treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

value for the Key '2' : Dog
value for the Key '10' : null
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (isEmpty)

TreeMapExample.java
```import java.util.TreeMap;

/*
* Example of isEmpty() method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

System.out.println("treeMap : " + treeMap);

/*
* Returns true if this map contains no key-value mappings.
*/
boolean isEmpty = treeMap.isEmpty();

System.out.println("isEmpty : " + isEmpty + "\n");

treeMap.put(1, "Cat");
treeMap.put(2, "Dog");
treeMap.put(4, "Apple");
treeMap.put(3, "Ball");

System.out.println("treeMap : " + treeMap );

isEmpty = treeMap.isEmpty();

System.out.println("isEmpty : " + isEmpty);

}
}
```
Output
```treeMap : {}
isEmpty : true

treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}
isEmpty : false
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (Size)

TreeMapExample.java
```import java.util.TreeMap;

/*
* Example of size() method.
*/
public class TreeMapExample
{
public static void main( String[] args )
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

treeMap.put(1, "Cat");
treeMap.put(2, "Dog");
treeMap.put(4, "Apple");
treeMap.put(3, "Ball");

System.out.println("treeMap : " + treeMap + "\n");

/*
* Returns the number of key-value mappings in this map.
*/
int size = treeMap.size();

System.out.println("size : " + size);

}
}
```
Output
```treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

size : 4
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (Clear)

TreeMapExample.java
```import java.util.TreeMap;

/*
* Example of clear() method.
*/
public class TreeMapExample
{
public static void main(String[] args)
{

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

treeMap.put(1, "Cat");
treeMap.put(2, "Dog");
treeMap.put(4, "Apple");
treeMap.put(3, "Ball");

System.out.println("treeMap : " + treeMap + "\n");

/*
* Removes all of the mappings from this map. The map will be empty
* after this call returns.
*/
treeMap.clear();

System.out.println("treeMap : " + treeMap);

}
}
```
Output
```treeMap : {1=Cat, 2=Dog, 3=Ball, 4=Apple}

treeMap : {}
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : HashMap (Constructors Introduction)

Click the below Image to Enlarge
 Java : Collection Framework : HashMap (Constructors Introduction)

 Java : Collection Framework : HashMap (Constructors Introduction)

 Java : Collection Framework : HashMap (Constructors Introduction)

 Java : Collection Framework : HashMap (Constructors Introduction)
• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : TreeMap (Default Constructor)

TreeMapExample.java
```import java.util.TreeMap;

/*
* Example of TreeMap() Constructor.
*/
public class TreeMapExample
{
public static void main( String[] args )
{
/*
* Constructs a new, empty tree map, using the natural ordering of its
* keys. All keys inserted into the map must implement the Comparable
* interface.
*/
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

treeMap.put(1, "Cat");
treeMap.put(3, "Dog");
treeMap.put(4, "Apple");
treeMap.put(2, "Ball");

System.out.println("treeMap : " + treeMap + "\n");

}
}
```
Output
```treeMap : {1=Cat, 2=Ball, 3=Dog, 4=Apple}
```

• All JavaEE Viedos Playlist
• All JavaEE Viedos
• Servlets Tutorial
• JDBC Tutorial
• Java : Collection Framework : LinkedHashMap (EntrySet)

```import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/*
* Example of entrySet() method.
*/
{
public static void main(String[] args)
{

/*
* A map entry (key-value pair).
*
* Returns a Set view of the mappings contained in this map.
*/

System.out.println("entrySet : " + entrySet + "\n");

System.out.println("-----------------------");
System.out.println("Key" + " | " + "value");
System.out.println("-----------------------");

for (Map.Entry<Integer, String> entry : entrySet)
{
System.out.println(entry.getKey() + "   | " + entry.getValue());
}

}
}
```
Output
```linkedHashMap : {1=Apple, 3=Cat, 2=Ball}

entrySet : [1=Apple, 3=Cat, 2=Ball]

-----------------------
Key | value
-----------------------
1   | Apple
3   | Cat
2   | Ball
```