forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
107 changed files
with
1,619 additions
and
637 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ava/com/baeldung/map/MyLinkedHashMap.java → ...ng/map/linkedhashmap/MyLinkedHashMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...om/baeldung/map/ImmutableMapUnitTest.java → ...g/map/immutable/ImmutableMapUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...ollections-maps-2/src/test/java/com/baeldung/map/linkedhashmap/LinkedHashMapUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.baeldung.map.linkedhashmap; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Set; | ||
|
||
import org.junit.Test; | ||
|
||
public class LinkedHashMapUnitTest { | ||
|
||
@Test | ||
public void givenLinkedHashMap_whenGetsOrderedKeyset_thenCorrect() { | ||
LinkedHashMap<Integer, String> map = new LinkedHashMap<>(); | ||
map.put(1, null); | ||
map.put(2, null); | ||
map.put(3, null); | ||
map.put(4, null); | ||
map.put(5, null); | ||
Set<Integer> keys = map.keySet(); | ||
Integer[] arr = keys.toArray(new Integer[0]); | ||
for (int i = 0; i < arr.length; i++) { | ||
assertEquals(Integer.valueOf(i + 1), arr[i]); | ||
} | ||
} | ||
|
||
@Test | ||
public void givenLinkedHashMap_whenAccessOrderWorks_thenCorrect() { | ||
LinkedHashMap<Integer, String> map = new LinkedHashMap<>(16, .75f, true); | ||
map.put(1, null); | ||
map.put(2, null); | ||
map.put(3, null); | ||
map.put(4, null); | ||
map.put(5, null); | ||
Set<Integer> keys = map.keySet(); | ||
assertEquals("[1, 2, 3, 4, 5]", keys.toString()); | ||
map.get(4); | ||
assertEquals("[1, 2, 3, 5, 4]", keys.toString()); | ||
map.get(1); | ||
assertEquals("[2, 3, 5, 4, 1]", keys.toString()); | ||
map.get(3); | ||
assertEquals("[2, 5, 4, 1, 3]", keys.toString()); | ||
} | ||
|
||
@Test | ||
public void givenLinkedHashMap_whenRemovesEldestEntry_thenCorrect() { | ||
LinkedHashMap<Integer, String> map = new MyLinkedHashMap<>(16, .75f, true); | ||
map.put(1, null); | ||
map.put(2, null); | ||
map.put(3, null); | ||
map.put(4, null); | ||
map.put(5, null); | ||
Set<Integer> keys = map.keySet(); | ||
assertEquals("[1, 2, 3, 4, 5]", keys.toString()); | ||
map.put(6, null); | ||
assertEquals("[2, 3, 4, 5, 6]", keys.toString()); | ||
map.put(7, null); | ||
assertEquals("[3, 4, 5, 6, 7]", keys.toString()); | ||
map.put(8, null); | ||
assertEquals("[4, 5, 6, 7, 8]", keys.toString()); | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
.../core-java-collections-maps-2/src/test/java/com/baeldung/map/treemap/TreeMapUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.baeldung.map.treemap; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Comparator; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
|
||
import org.junit.Test; | ||
|
||
public class TreeMapUnitTest { | ||
|
||
@Test | ||
public void givenTreeMap_whenOrdersEntriesNaturally_thenCorrect() { | ||
TreeMap<Integer, String> map = new TreeMap<>(); | ||
map.put(3, "val"); | ||
map.put(2, "val"); | ||
map.put(1, "val"); | ||
map.put(5, "val"); | ||
map.put(4, "val"); | ||
assertEquals("[1, 2, 3, 4, 5]", map.keySet().toString()); | ||
} | ||
|
||
@Test | ||
public void givenTreeMap_whenOrdersEntriesNaturally_thenCorrect2() { | ||
TreeMap<String, String> map = new TreeMap<>(); | ||
map.put("c", "val"); | ||
map.put("b", "val"); | ||
map.put("a", "val"); | ||
map.put("e", "val"); | ||
map.put("d", "val"); | ||
|
||
assertEquals("[a, b, c, d, e]", map.keySet().toString()); | ||
} | ||
|
||
@Test | ||
public void givenTreeMap_whenOrdersEntriesByComparator_thenCorrect() { | ||
TreeMap<Integer, String> map = new TreeMap<>(Comparator.reverseOrder()); | ||
map.put(3, "val"); | ||
map.put(2, "val"); | ||
map.put(1, "val"); | ||
map.put(5, "val"); | ||
map.put(4, "val"); | ||
|
||
assertEquals("[5, 4, 3, 2, 1]", map.keySet().toString()); | ||
} | ||
|
||
@Test | ||
public void givenTreeMap_whenPerformsQueries_thenCorrect() { | ||
TreeMap<Integer, String> map = new TreeMap<>(); | ||
map.put(3, "val"); | ||
map.put(2, "val"); | ||
map.put(1, "val"); | ||
map.put(5, "val"); | ||
map.put(4, "val"); | ||
|
||
Integer highestKey = map.lastKey(); | ||
Integer lowestKey = map.firstKey(); | ||
Set<Integer> keysLessThan3 = map.headMap(3).keySet(); | ||
Set<Integer> keysGreaterThanEqTo3 = map.tailMap(3).keySet(); | ||
|
||
assertEquals(Integer.valueOf(5), highestKey); | ||
assertEquals(Integer.valueOf(1), lowestKey); | ||
assertEquals("[1, 2]", keysLessThan3.toString()); | ||
assertEquals("[3, 4, 5]", keysGreaterThanEqTo3.toString()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...src/main/java/com/baeldung/map/MyKey.java → ...java/com/baeldung/map/advanced/MyKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...com/baeldung/map/EmptyMapInitializer.java → ...ung/map/emptymap/EmptyMapInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.