Skip to content

Commit ab0b2df

Browse files
authored
BAEL-4719 - Using the Map.Entry Java Class (eugenp#10428)
* BAEL-4719 Using the Map.Entry Java Class * BAEL-4719 Using the Map.Entry Java Class * BAEL-4719 Change description * BAEL-4719 Feedback from first draft
1 parent a478360 commit ab0b2df

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.map.entry;
2+
3+
public class Book {
4+
private String title;
5+
private String author;
6+
7+
public Book(String title, String author) {
8+
this.title = title;
9+
this.author = author;
10+
}
11+
12+
public String getTitle() {
13+
return title;
14+
}
15+
16+
public void setTitle(String title) {
17+
this.title = title;
18+
}
19+
20+
public String getAuthor() {
21+
return author;
22+
}
23+
24+
public void setAuthor(String author) {
25+
this.author = author;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "Book{" +
31+
"title='" + title + '\'' +
32+
", author='" + author + '\'' +
33+
'}';
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.map.entry;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class MapEntryEfficiencyExample {
7+
8+
public static void main(String[] args) {
9+
MapEntryEfficiencyExample mapEntryEfficiencyExample = new MapEntryEfficiencyExample();
10+
Map<String, String> map = new HashMap<>();
11+
12+
map.put("Robert C. Martin", "Clean Code");
13+
map.put("Joshua Bloch", "Effective Java");
14+
15+
System.out.println("Iterating Using Map.KeySet - 2 operations");
16+
mapEntryEfficiencyExample.usingKeySet(map);
17+
18+
System.out.println("Iterating Using Map.Entry - 1 operation");
19+
mapEntryEfficiencyExample.usingEntrySet(map);
20+
21+
}
22+
23+
public void usingKeySet(Map<String, String> bookMap) {
24+
for (String key : bookMap.keySet()) {
25+
System.out.println("key: " + key + " value: " + bookMap.get(key));
26+
}
27+
}
28+
29+
public void usingEntrySet(Map<String, String> bookMap) {
30+
for (Map.Entry<String, String> book: bookMap.entrySet()) {
31+
System.out.println("key: " + book.getKey() + " value: " + book.getValue());
32+
}
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.map.entry;
2+
3+
import java.util.*;
4+
5+
public class MapEntryTupleExample {
6+
7+
public static void main(String[] args) {
8+
Map.Entry<String, Book> tuple1;
9+
Map.Entry<String, Book> tuple2;
10+
Map.Entry<String, Book> tuple3;
11+
12+
tuple1 = new AbstractMap.SimpleEntry<>("9780134685991", new Book("Effective Java 3d Edition", "Joshua Bloch"));
13+
tuple2 = new AbstractMap.SimpleEntry<>("9780132350884", new Book("Clean Code", "Robert C Martin"));
14+
tuple3 = new AbstractMap.SimpleEntry<>("9780132350884", new Book("Clean Code", "Robert C Martin"));
15+
16+
List<Map.Entry<String, Book>> orderedTuples = new ArrayList<>();
17+
orderedTuples.add(tuple1);
18+
orderedTuples.add(tuple2);
19+
orderedTuples.add(tuple3);
20+
21+
for (Map.Entry<String, Book> tuple : orderedTuples) {
22+
System.out.println("key: " + tuple.getKey() + " value: " + tuple.getValue());
23+
}
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.map.entry;
2+
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class MapEntryUnitTest {
10+
11+
@Test
12+
public void givenSimpleEntryList_whenAddDuplicateKey_thenDoesNotOverwriteExistingKey() {
13+
List<Map.Entry<String, Book>> orderedTuples = new ArrayList<>();
14+
orderedTuples.add(new AbstractMap.SimpleEntry<>("9780134685991", new Book("Effective Java 3d Edition", "Joshua Bloch")));
15+
orderedTuples.add(new AbstractMap.SimpleEntry<>("9780132350884", new Book("Clean Code", "Robert C Martin")));
16+
orderedTuples.add(new AbstractMap.SimpleEntry<>("9780132350884", new Book("Clean Code", "Robert C Martin")));
17+
18+
assertEquals(3, orderedTuples.size());
19+
assertEquals("9780134685991", orderedTuples.get(0).getKey());
20+
assertEquals("9780132350884", orderedTuples.get(1).getKey());
21+
assertEquals("9780132350884", orderedTuples.get(2).getKey());
22+
}
23+
24+
@Test
25+
public void givenRegularMap_whenAddDuplicateKey_thenOverwritesExistingKey() {
26+
Map<String, Book> entries = new HashMap<>();
27+
entries.put("9780134685991", new Book("Effective Java 3d Edition", "Joshua Bloch"));
28+
entries.put("9780132350884", new Book("Clean Code", "Robert C Martin"));
29+
entries.put("9780132350884", new Book("Clean Code", "Robert C Martin"));
30+
31+
assertEquals(2, entries.size());
32+
}
33+
}

0 commit comments

Comments
 (0)