BAEL-4719 - Using the Map.Entry Java Class (#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
This commit is contained in:
parent
dd2ecbb9ee
commit
d11f1163ab
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.map.entry;
|
||||
|
||||
public class Book {
|
||||
private String title;
|
||||
private String author;
|
||||
|
||||
public Book(String title, String author) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book{" +
|
||||
"title='" + title + '\'' +
|
||||
", author='" + author + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.map.entry;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapEntryEfficiencyExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
MapEntryEfficiencyExample mapEntryEfficiencyExample = new MapEntryEfficiencyExample();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
|
||||
map.put("Robert C. Martin", "Clean Code");
|
||||
map.put("Joshua Bloch", "Effective Java");
|
||||
|
||||
System.out.println("Iterating Using Map.KeySet - 2 operations");
|
||||
mapEntryEfficiencyExample.usingKeySet(map);
|
||||
|
||||
System.out.println("Iterating Using Map.Entry - 1 operation");
|
||||
mapEntryEfficiencyExample.usingEntrySet(map);
|
||||
|
||||
}
|
||||
|
||||
public void usingKeySet(Map<String, String> bookMap) {
|
||||
for (String key : bookMap.keySet()) {
|
||||
System.out.println("key: " + key + " value: " + bookMap.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
public void usingEntrySet(Map<String, String> bookMap) {
|
||||
for (Map.Entry<String, String> book: bookMap.entrySet()) {
|
||||
System.out.println("key: " + book.getKey() + " value: " + book.getValue());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.map.entry;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class MapEntryTupleExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map.Entry<String, Book> tuple1;
|
||||
Map.Entry<String, Book> tuple2;
|
||||
Map.Entry<String, Book> tuple3;
|
||||
|
||||
tuple1 = new AbstractMap.SimpleEntry<>("9780134685991", new Book("Effective Java 3d Edition", "Joshua Bloch"));
|
||||
tuple2 = new AbstractMap.SimpleEntry<>("9780132350884", new Book("Clean Code", "Robert C Martin"));
|
||||
tuple3 = new AbstractMap.SimpleEntry<>("9780132350884", new Book("Clean Code", "Robert C Martin"));
|
||||
|
||||
List<Map.Entry<String, Book>> orderedTuples = new ArrayList<>();
|
||||
orderedTuples.add(tuple1);
|
||||
orderedTuples.add(tuple2);
|
||||
orderedTuples.add(tuple3);
|
||||
|
||||
for (Map.Entry<String, Book> tuple : orderedTuples) {
|
||||
System.out.println("key: " + tuple.getKey() + " value: " + tuple.getValue());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.map.entry;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MapEntryUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSimpleEntryList_whenAddDuplicateKey_thenDoesNotOverwriteExistingKey() {
|
||||
List<Map.Entry<String, Book>> orderedTuples = new ArrayList<>();
|
||||
orderedTuples.add(new AbstractMap.SimpleEntry<>("9780134685991", new Book("Effective Java 3d Edition", "Joshua Bloch")));
|
||||
orderedTuples.add(new AbstractMap.SimpleEntry<>("9780132350884", new Book("Clean Code", "Robert C Martin")));
|
||||
orderedTuples.add(new AbstractMap.SimpleEntry<>("9780132350884", new Book("Clean Code", "Robert C Martin")));
|
||||
|
||||
assertEquals(3, orderedTuples.size());
|
||||
assertEquals("9780134685991", orderedTuples.get(0).getKey());
|
||||
assertEquals("9780132350884", orderedTuples.get(1).getKey());
|
||||
assertEquals("9780132350884", orderedTuples.get(2).getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegularMap_whenAddDuplicateKey_thenOverwritesExistingKey() {
|
||||
Map<String, Book> entries = new HashMap<>();
|
||||
entries.put("9780134685991", new Book("Effective Java 3d Edition", "Joshua Bloch"));
|
||||
entries.put("9780132350884", new Book("Clean Code", "Robert C Martin"));
|
||||
entries.put("9780132350884", new Book("Clean Code", "Robert C Martin"));
|
||||
|
||||
assertEquals(2, entries.size());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue