Merge pull request #8969 from omerfinger/master
BAEL-3926 - Java Map with case-insensitive keys
This commit is contained in:
commit
f8d065ab17
|
@ -0,0 +1,8 @@
|
|||
## Java Collections Cookbooks and Examples
|
||||
|
||||
This module contains articles about Map data structures in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- More articles: [[<-- prev>]](/../java-collections-maps)
|
||||
- More articles: [[<-- prev>]](/../java-collections-maps-2)
|
|
@ -0,0 +1,43 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>java-collections-maps-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>java-collections-maps-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<spring.version>5.2.5.RELEASE</spring.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,94 @@
|
|||
package com.baeldung.map.caseinsensitivekeys;
|
||||
|
||||
import org.apache.commons.collections4.map.CaseInsensitiveMap;
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CaseInsensitiveMapUnitTest {
|
||||
@Test
|
||||
public void givenCaseInsensitiveTreeMap_whenTwoEntriesAdded_thenSizeIsOne(){
|
||||
Map<String, Integer> treeMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
treeMap.put("abc", 1);
|
||||
treeMap.put("ABC", 2);
|
||||
|
||||
assertEquals(1, treeMap.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsCaseInsensitiveMap_whenTwoEntriesAdded_thenSizeIsOne(){
|
||||
Map<String, Integer> commonsHashMap = new CaseInsensitiveMap<>();
|
||||
commonsHashMap.put("abc", 1);
|
||||
commonsHashMap.put("ABC", 2);
|
||||
|
||||
assertEquals(1, commonsHashMap.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedCaseInsensitiveMap_whenTwoEntriesAdded_thenSizeIsOne(){
|
||||
Map<String, Integer> linkedHashMap = new LinkedCaseInsensitiveMap<>();
|
||||
linkedHashMap.put("abc", 1);
|
||||
linkedHashMap.put("ABC", 2);
|
||||
|
||||
assertEquals(1, linkedHashMap.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCaseInsensitiveTreeMap_whenSameEntryAdded_thenValueUpdated(){
|
||||
Map<String, Integer> treeMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
treeMap.put("abc", 1);
|
||||
treeMap.put("ABC", 2);
|
||||
|
||||
assertEquals(2, treeMap.get("aBc").intValue());
|
||||
assertEquals(2, treeMap.get("ABc").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsCaseInsensitiveMap_whenSameEntryAdded_thenValueUpdated(){
|
||||
Map<String, Integer> commonsHashMap = new CaseInsensitiveMap<>();
|
||||
commonsHashMap.put("abc", 1);
|
||||
commonsHashMap.put("ABC", 2);
|
||||
|
||||
assertEquals(2, commonsHashMap.get("aBc").intValue());
|
||||
assertEquals(2, commonsHashMap.get("ABc").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedCaseInsensitiveMap_whenSameEntryAdded_thenValueUpdated(){
|
||||
Map<String, Integer> linkedHashMap = new LinkedCaseInsensitiveMap<>();
|
||||
linkedHashMap.put("abc", 1);
|
||||
linkedHashMap.put("ABC", 2);
|
||||
|
||||
assertEquals(2, linkedHashMap.get("aBc").intValue());
|
||||
assertEquals(2, linkedHashMap.get("ABc").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCaseInsensitiveTreeMap_whenEntryRemoved_thenSizeIsZero(){
|
||||
Map<String, Integer> treeMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
treeMap.put("abc", 3);
|
||||
treeMap.remove("aBC");
|
||||
|
||||
assertEquals(0, treeMap.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsCaseInsensitiveMap_whenEntryRemoved_thenSizeIsZero(){
|
||||
Map<String, Integer> commonsHashMap = new CaseInsensitiveMap<>();
|
||||
commonsHashMap.put("abc", 3);
|
||||
commonsHashMap.remove("aBC");
|
||||
|
||||
assertEquals(0, commonsHashMap.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedCaseInsensitiveMap_whenEntryRemoved_thenSizeIsZero(){
|
||||
Map<String, Integer> linkedHashMap = new LinkedCaseInsensitiveMap<>();
|
||||
linkedHashMap.put("abc", 3);
|
||||
linkedHashMap.remove("aBC");
|
||||
|
||||
assertEquals(0, linkedHashMap.size());
|
||||
}
|
||||
}
|
6
pom.xml
6
pom.xml
|
@ -453,6 +453,7 @@
|
|||
|
||||
<module>java-collections-conversions</module>
|
||||
<module>java-collections-conversions-2</module>
|
||||
<module>java-collections-maps-3</module>
|
||||
<!-- <module>java-ee-8-security-api</module> --> <!-- long running -->
|
||||
|
||||
<module>javafx</module>
|
||||
|
@ -564,7 +565,7 @@
|
|||
<module>rxjava-libraries</module>
|
||||
<module>rxjava-observables</module>
|
||||
<module>rxjava-operators</module>
|
||||
|
||||
|
||||
<module>atomikos</module>
|
||||
</modules>
|
||||
|
||||
|
@ -965,6 +966,7 @@
|
|||
|
||||
<module>java-collections-conversions</module>
|
||||
<module>java-collections-conversions-2</module>
|
||||
<module>java-collections-maps-3</module>
|
||||
<!-- <module>java-ee-8-security-api</module> --> <!-- long running -->
|
||||
|
||||
<module>javafx</module>
|
||||
|
@ -1075,7 +1077,7 @@
|
|||
<module>rxjava-libraries</module>
|
||||
<module>rxjava-observables</module>
|
||||
<module>rxjava-operators</module>
|
||||
|
||||
|
||||
<module>atomikos</module>
|
||||
</modules>
|
||||
|
||||
|
|
Loading…
Reference in New Issue