* create
```
import java.util.*;
class Main{
public static void main(String args[]){
TreeMap<Integer,String> map=new TreeMap<>();
map.put(100,"A");
System.out.println(map);
}
}
```
* iterator
```
import java.util.*;
class Main{
public static void main(String args[]){
TreeMap<Integer,String> map = new TreeMap<>();
map.put(100,"A");
map.put(200,"B");
Iterator<Map.Entry<Integer, String> > iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
System.out.println(entry.getKey() + "->" + entry.getValue());
}
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "->" + entry.getValue());
}
map.entrySet().forEach(entry -> {
System.out.println(entry.getKey() + "->" + entry.getValue());
});
}
}
```
* add
```
import java.util.*;
class Main{
public static void main(String args[]){
TreeMap<Integer,String> map = new TreeMap<>();
map.put(100,"A");
map.put(200,"B");
Set<Map.Entry<Integer, String> > entries = map.entrySet();
entries.forEach(entry -> {
System.out.println(entry.getKey() + "->" + entry.getValue());
});
}
}
```
* change
```
import java.util.*;
class Main{
public static void main(String args[]){
TreeMap<Integer,String> map = new TreeMap<>();
map.put(100,"A");
// put() replace the value if the key is present;
// but if the key is not present it creates a new record.
// If an existing key is passed then the previous value gets returned.
// If a new pair is passed, then NULL is returned.
map.put(100,"AA");
map.put(200,"B");
// replace() replaces the value of the given key
// but if the key is not present it does not create a new record
// It returns the previous value associated with the key.
// If there is no such key mapped, then it returns null.
map.replace(200, "BB");
map.replace(300, "C");
// computeIfPresent() changes value if key is present
map.computeIfPresent(200,(k,v)->"BBB");
map.computeIfPresent(300,(k,v)->"CC");
Set<Map.Entry<Integer, String> > entries = map.entrySet();
entries.forEach(entry -> {
System.out.println(entry.getKey() + "->" + entry.getValue());
});
}
}
```
* query
```
import java.util.*;
class Main{
public static void main(String args[]){
TreeMap<Integer,String> map = new TreeMap<>();
map.put(100,"A");
map.put(200,"B");
System.out.println(map.containsValue("A"));
System.out.println(map.containsKey(200));
System.out.println(map.isEmpty());
System.out.println(map.size());
}
}
```
* delete
```
import java.util.*;
class Main{
public static void main(String args[]){
TreeMap<Integer,String> map = new TreeMap<>();
map.put(100,"A");
map.put(200,"B");
map.remove(200);
map.clear();
System.out.println(map);
}
}
```