Skip to content

Commit 38eb6b2

Browse files
committed
added treemap and hashmap
1 parent ffc0192 commit 38eb6b2

File tree

4 files changed

+129
-6
lines changed

4 files changed

+129
-6
lines changed

.idea/workspace.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ It is used to develop rich internet applications. It uses a light-weight user in
137137
|[Priority Thread](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Multithreading/Priority.md)|
138138
|[Daemon Thread](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Multithreading/DemonThread.md)|
139139
|[Swings](https://github.com/connectaman/Java_Notes_and_Programs/tree/master/src/swings)|
140-
|[Collections]()|
141-
|[ArrayList]()|
142-
|[LinkedList]()|
143-
|[HashSet]()|
144-
|[TreeSet]()|
140+
|[Collections](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Collections/Collection.md)|
141+
|[ArrayList](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Collections/ArrayList.md)|
142+
|[LinkedList](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Collections/LinkedList.md)|
143+
|[HashSet](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Collections/HashSet.md)|
144+
|[TreeSet](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Collections/TreeSet.md)|
145145
|[HashMap]()|
146146
|[TreeMap]()|
147147
|[]()|

src/Collections/HashMap.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
### Java HashMap class
2+
3+
4+
##### Points to remember
5+
6+
- Java HashMap class contains values based on the key.
7+
- Java HashMap class contains only unique keys.
8+
- Java HashMap class may have one null key and multiple null values.
9+
- Java HashMap class is non synchronized.
10+
- Java HashMap class maintains no order.
11+
- The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.
12+
13+
14+
15+
---------
16+
17+
```java
18+
import java.util.*;
19+
public class HashMap2 {
20+
public static void main(String args[]) {
21+
HashMap<Integer,String> map=new HashMap<Integer,String>();
22+
map.put(100,"Amit");
23+
map.put(101,"Vijay");
24+
map.put(102,"Rahul");
25+
map.put(103, "Gaurav");
26+
System.out.println("Initial list of elements: "+map);
27+
//key-based removal
28+
map.remove(100);
29+
System.out.println("Updated list of elements: "+map);
30+
//value-based removal
31+
map.remove(101);
32+
System.out.println("Updated list of elements: "+map);
33+
//key-value pair based removal
34+
map.remove(102, "Rahul");
35+
System.out.println("Updated list of elements: "+map);
36+
}
37+
}
38+
```
39+
Output
40+
```
41+
Initial list of elements: {100=Amit, 101=Vijay, 102=Rahul, 103=Gaurav}
42+
Updated list of elements: {101=Vijay, 102=Rahul, 103=Gaurav}
43+
Updated list of elements: {102=Rahul, 103=Gaurav}
44+
Updated list of elements: {103=Gaurav}
45+
```
46+
47+
---------
48+

src/Collections/TreeMap.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
### Java TreeMap class
2+
3+
4+
Java TreeMap class is a red-black tree based implementation. It provides an efficient means of storing key-value pairs in sorted order.
5+
6+
##### The important points about Java TreeMap class are:
7+
8+
- Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
9+
- Java TreeMap contains only unique elements.
10+
- Java TreeMap cannot have a null key but can have multiple null values.
11+
- Java TreeMap is non synchronized.
12+
- Java TreeMap maintains ascending order.
13+
14+
15+
--------
16+
17+
##### Java TreeMap Example
18+
19+
```java
20+
import java.util.*;
21+
class TreeMap1{
22+
public static void main(String args[]){
23+
TreeMap<Integer,String> map=new TreeMap<Integer,String>();
24+
map.put(100,"Amit");
25+
map.put(102,"Ravi");
26+
map.put(101,"Vijay");
27+
map.put(103,"Rahul");
28+
29+
for(Map.Entry m:map.entrySet()){
30+
System.out.println(m.getKey()+" "+m.getValue());
31+
}
32+
}
33+
}
34+
```
35+
Output
36+
```
37+
100 Amit
38+
101 Vijay
39+
102 Ravi
40+
103 Rahul
41+
```
42+
43+
---------
44+
45+
```java
46+
import java.util.*;
47+
class TreeMap3{
48+
public static void main(String args[]){
49+
NavigableMap<Integer,String> map=new TreeMap<Integer,String>();
50+
map.put(100,"Amit");
51+
map.put(102,"Ravi");
52+
map.put(101,"Vijay");
53+
map.put(103,"Rahul");
54+
//Maintains descending order
55+
System.out.println("descendingMap: "+map.descendingMap());
56+
//Returns key-value pairs whose keys are less than or equal to the specified key.
57+
System.out.println("headMap: "+map.headMap(102,true));
58+
//Returns key-value pairs whose keys are greater than or equal to the specified key.
59+
System.out.println("tailMap: "+map.tailMap(102,true));
60+
//Returns key-value pairs exists in between the specified key.
61+
System.out.println("subMap: "+map.subMap(100, false, 102, true));
62+
}
63+
}
64+
```
65+
66+
Output
67+
```
68+
descendingMap: {103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
69+
headMap: {100=Amit, 101=Vijay, 102=Ravi}
70+
tailMap: {102=Ravi, 103=Rahul}
71+
subMap: {101=Vijay, 102=Ravi}
72+
```

0 commit comments

Comments
 (0)