Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvariyath committed Feb 2, 2025
2 parents e9ed7b5 + 9d6f243 commit b1bb2e2
Show file tree
Hide file tree
Showing 107 changed files with 1,619 additions and 637 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ public void whenShufflingListWithStream_thenListIsShuffled() {
List<String> originalList = Arrays.asList("Foo", "Bar", "Baz", "Qux");
List<String> shuffledList = ShufflingCollections.shuffleList(originalList);

// Ensure the shuffled list is different from the original list
assertNotEquals(originalList, shuffledList, "The shuffled list should not be the same as the original list");

// Ensure the shuffled list contains the same elements as the original list
assertTrue(shuffledList.containsAll(originalList), "The shuffled list should contain all elements of the original list");
assertTrue(originalList.containsAll(shuffledList), "The original list should contain all elements of the shuffled list");
Expand Down
8 changes: 4 additions & 4 deletions core-java-modules/core-java-collections-maps-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
This module contains articles about Map data structures in Java.

### Relevant Articles:
- [Map of Primitives in Java](https://www.baeldung.com/java-map-primitives)
- [Copying a HashMap in Java](https://www.baeldung.com/java-copy-hashmap)
- [A Guide to Java HashMap](https://www.baeldung.com/java-hashmap)
- [Guide to WeakHashMap in Java](https://www.baeldung.com/java-weakhashmap)
- [Map to String Conversion in Java](https://www.baeldung.com/java-map-to-string-conversion)
- [Iterate Over a Map in Java](https://www.baeldung.com/java-iterate-map)
- [Merging Two Maps with Java](https://www.baeldung.com/java-merge-maps)
- [Sort a HashMap in Java](https://www.baeldung.com/java-hashmap-sort)
- [Finding the Highest Value in a Java Map](https://www.baeldung.com/java-find-map-max)
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
- [Immutable Map Implementations in Java](https://www.baeldung.com/java-immutable-maps)
- [A Guide to TreeMap in Java](https://www.baeldung.com/java-treemap)
- [A Guide to LinkedHashMap in Java](https://www.baeldung.com/java-linked-hashmap)
- [Get the Key for a Value from a Java Map](https://www.baeldung.com/java-map-key-from-value)
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps) [[next -->]](/core-java-modules/core-java-collections-maps-3)
18 changes: 0 additions & 18 deletions core-java-modules/core-java-collections-maps-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@
</parent>

<dependencies>
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId>
<version>${eclipse-collections.version}</version>
</dependency>
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>hppc</artifactId>
<version>${hppc.version}</version>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>${fastutil.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down Expand Up @@ -65,9 +50,6 @@
<properties>
<streamex.version>0.8.1</streamex.version>
<avaitility.version>1.7.0</avaitility.version>
<eclipse-collections.version>8.2.0</eclipse-collections.version>
<hppc.version>0.7.2</hppc.version>
<fastutil.version>8.1.0</fastutil.version>
<jmh.version>1.37</jmh.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static <K, V> Stream<K> keys(Map<K, V> map, V value) {
return map.entrySet()
.stream()
.filter(entry -> value.equals(entry.getValue()))
.map(Map.Entry::getKey);
.map(Entry::getKey);
}

public static <K, V> Set<K> getKeys(Map<K, V> map, V value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.map;
package com.baeldung.map.linkedhashmap;

import java.util.LinkedHashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
*/
package com.baeldung.map;

import com.google.common.collect.HashBiMap;
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.junit.Test;

import com.google.common.collect.HashBiMap;

/**
* @author swpraman
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.map;
package com.baeldung.map.immutable;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down
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());
}

}
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());
}

}
6 changes: 3 additions & 3 deletions core-java-modules/core-java-collections-maps-3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ This module contains articles about Map data structures in Java.

### Relevant Articles:
- [Java TreeMap vs HashMap](https://www.baeldung.com/java-treemap-vs-hashmap)
- [Comparing Two HashMaps in Java](https://www.baeldung.com/java-compare-hashmaps)
- [The Map.computeIfAbsent() Method](https://www.baeldung.com/java-map-computeifabsent)
- [Collections.synchronizedMap vs. ConcurrentHashMap](https://www.baeldung.com/java-synchronizedmap-vs-concurrenthashmap)
- [Java HashMap Load Factor](https://www.baeldung.com/java-hashmap-load-factor)
- [Converting Java Properties to HashMap](https://www.baeldung.com/java-convert-properties-to-hashmap)
- [Get Values and Keys as ArrayList From a HashMap](https://www.baeldung.com/java-values-keys-arraylists-hashmap)
- [Remove Duplicate Values From HashMap in Java](https://www.baeldung.com/java-hashmap-delete-duplicates)
- [Create an Empty Map in Java](https://www.baeldung.com/java-create-empty-map)
- [How to Check If a Key Exists in a Map](https://www.baeldung.com/java-map-key-exists)
- [The Java HashMap Under the Hood](https://www.baeldung.com/java-hashmap-advanced)
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2)[[next -->]](/core-java-modules/core-java-collections-maps-4)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.map;
package com.baeldung.map.advanced;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.map;
package com.baeldung.map.emptymap;

import java.util.Collections;
import java.util.HashMap;
Expand Down
Loading

0 comments on commit b1bb2e2

Please sign in to comment.