Skip to content

Commit beae807

Browse files
committed
Map,HashMap,TreeMap,LinkedHashMap
1 parent 997f748 commit beae807

6 files changed

Lines changed: 186 additions & 0 deletions

File tree

section_20/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.eazybytes.map;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class HashMapDemo {
7+
8+
public static void main(String[] args) {
9+
var countryMap = new HashMap<String,String>();
10+
countryMap.put("India", "New Delhi");
11+
countryMap.put("USA", "Washington, DC");
12+
countryMap.put("France", "Paris");
13+
countryMap.put(null, null);
14+
15+
System.out.println(countryMap.get("India"));
16+
countryMap.remove(null);
17+
System.out.println(countryMap.size());
18+
19+
}
20+
21+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.eazybytes.map;
2+
3+
import java.util.*;
4+
5+
public class HashMapIterationDemo {
6+
7+
public static void main(String[] args) {
8+
var countryMap = new HashMap<String,String>();
9+
countryMap.put("India", "New Delhi");
10+
countryMap.put("USA", "Washington, DC");
11+
countryMap.put("France", "Paris");
12+
approach3(countryMap);
13+
}
14+
15+
public static void approach1(HashMap<String, String> countryMap) {
16+
Set<String> keys = countryMap.keySet();
17+
Iterator<String> iterator = keys.iterator();
18+
while (iterator.hasNext()) {
19+
String key = iterator.next();
20+
String capital = countryMap.get(key);
21+
System.out.println(key + " : "+capital);
22+
}
23+
/*for(String key:keys) {
24+
String capital = countryMap.get(key);
25+
System.out.println(key + " : "+capital);
26+
}*/
27+
}
28+
29+
public static void approach2(HashMap<String, String> countryMap) {
30+
Set<Map.Entry<String, String>> entries = countryMap.entrySet();
31+
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
32+
/*while (iterator.hasNext()) {
33+
Map.Entry<String,String> entry = iterator.next();
34+
String key = entry.getKey();
35+
String capital = entry.getValue();
36+
System.out.println(key + " : "+capital);
37+
}*/
38+
for(Map.Entry<String, String> entry:entries) {
39+
String key = entry.getKey();
40+
String capital = entry.getValue();
41+
System.out.println(key + " : "+capital);
42+
}
43+
}
44+
45+
public static void approach3(HashMap<String, String> countryMap) {
46+
Collection<String> values = countryMap.values();
47+
for(String value:values) {
48+
System.out.println(value);
49+
}
50+
}
51+
52+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.eazybytes.map;
2+
3+
import java.util.Map;
4+
import static java.util.Map.entry;
5+
6+
public class ImmutableMapDemo {
7+
8+
public static void main(String[] args) {
9+
Map<String,Integer> stringIntegerMap = Map.of("a",1,"b",2,"c",3);
10+
processMap(stringIntegerMap);
11+
12+
Map<Integer,String> integerStringMap = Map.ofEntries(entry(1, "Tom"),
13+
entry(2, "Dick"),
14+
entry(3, "Harry"));
15+
processMap1(integerStringMap);
16+
17+
}
18+
19+
public static void processMap(Map<String,Integer> stringIntegerMap) {
20+
for(Map.Entry<String,Integer> entry : stringIntegerMap.entrySet()){
21+
System.out.println(entry.getKey()+":"+entry.getValue());
22+
}
23+
}
24+
25+
public static void processMap1(Map<Integer,String> integerStringMap) {
26+
for(Map.Entry<Integer,String> entry : integerStringMap.entrySet()){
27+
System.out.println(entry.getKey()+":"+entry.getValue());
28+
}
29+
}
30+
31+
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.eazybytes.map;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
import java.util.SequencedMap;
6+
7+
public class LinkedHashMapDemo {
8+
9+
public static void main(String[] args) {
10+
LinkedHashMap<Integer,String> linkedHashMap = new LinkedHashMap<>();
11+
12+
linkedHashMap.put(4, "Four");
13+
linkedHashMap.put(1, "One");
14+
linkedHashMap.put(7, "Seven");
15+
linkedHashMap.put(2, "Two");
16+
17+
for(Map.Entry<Integer,String> entry : linkedHashMap.entrySet()) {
18+
System.out.println(entry.getKey()+":"+entry.getValue());
19+
}
20+
SequencedMap<Integer,String> reversedLinkedHashMap = linkedHashMap.reversed();
21+
22+
for(Map.Entry<Integer,String> entry : reversedLinkedHashMap.entrySet()) {
23+
System.out.println(entry.getKey()+":"+entry.getValue());
24+
}
25+
26+
}
27+
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.eazybytes.map;
2+
3+
import java.util.Map;
4+
import java.util.TreeMap;
5+
6+
public class TreeMapDemo {
7+
8+
public static void main(String[] args) {
9+
Map<Integer,String> numbers = new TreeMap<>();
10+
numbers.put(23, "Twenty three");
11+
numbers.put(12, "Twelve");
12+
numbers.put(42, "Forty two");
13+
numbers.put(3, "Three");
14+
numbers.put(19, "Nineteen");
15+
numbers.put(48, "Forty eight");
16+
numbers.put(76, "Seventy six");
17+
18+
for(Map.Entry<Integer,String> entry : numbers.entrySet()){
19+
System.out.println(entry.getKey()+"-"+entry.getValue());
20+
}
21+
22+
}
23+
24+
}

0 commit comments

Comments
 (0)