Split or move java-collections-maps module (#7802)
This commit is contained in:
committed by
Josh Cummings
parent
ee9f90ab2f
commit
f4b4a9eea5
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.map.convert;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MapToString {
|
||||
|
||||
public static String convertWithIteration(Map<Integer, ?> map) {
|
||||
StringBuilder mapAsString = new StringBuilder("{");
|
||||
for (Integer key : map.keySet()) {
|
||||
mapAsString.append(key + "=" + map.get(key) + ", ");
|
||||
}
|
||||
mapAsString.delete(mapAsString.length()-2, mapAsString.length()).append("}");
|
||||
return mapAsString.toString();
|
||||
}
|
||||
|
||||
public static String convertWithStream(Map<Integer, ?> map) {
|
||||
String mapAsString = map.keySet().stream()
|
||||
.map(key -> key + "=" + map.get(key))
|
||||
.collect(Collectors.joining(", ", "{", "}"));
|
||||
return mapAsString;
|
||||
}
|
||||
|
||||
public static String convertWithGuava(Map<Integer, ?> map) {
|
||||
return Joiner.on(",").withKeyValueSeparator("=").join(map);
|
||||
}
|
||||
|
||||
public static String convertWithApache(Map map) {
|
||||
return StringUtils.join(map);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.map.convert;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StringToMap {
|
||||
|
||||
public static Map<String, String> convertWithStream(String mapAsString) {
|
||||
Map<String, String> map = Arrays.stream(mapAsString.split(","))
|
||||
.map(entry -> entry.split("="))
|
||||
.collect(Collectors.toMap(entry -> entry[0], entry -> entry[1]));
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Map<String, String> convertWithGuava(String mapAsString) {
|
||||
return Splitter.on(',').withKeyValueSeparator('=').split(mapAsString);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
package com.baeldung.map;
|
||||
package com.baeldung.map.copyhashmap;
|
||||
|
||||
import org.apache.commons.lang3.SerializationUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -6,8 +8,6 @@ import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.SerializationUtils;
|
||||
|
||||
public class CopyHashMap {
|
||||
|
||||
public static <String, Employee> HashMap<String, Employee> copyUsingConstructor(HashMap<String, Employee> originalMap) {
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.map.initialize;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class MapInitializer {
|
||||
|
||||
public static Map<String, String> articleMapOne;
|
||||
static {
|
||||
articleMapOne = new HashMap<>();
|
||||
articleMapOne.put("ar01", "Intro to Map");
|
||||
articleMapOne.put("ar02", "Some article");
|
||||
}
|
||||
|
||||
public static Map<String, String> createSingletonMap() {
|
||||
Map<String, String> passwordMap = Collections.singletonMap("username1", "password1");
|
||||
return passwordMap;
|
||||
|
||||
}
|
||||
|
||||
public Map<String, String> createEmptyMap() {
|
||||
Map<String, String> emptyMap = Collections.emptyMap();
|
||||
return emptyMap;
|
||||
}
|
||||
|
||||
public Map<String, String> createUsingDoubleBrace() {
|
||||
Map<String, String> doubleBraceMap = new HashMap<String, String>() {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
{
|
||||
put("key1", "value1");
|
||||
put("key2", "value2");
|
||||
}
|
||||
};
|
||||
return doubleBraceMap;
|
||||
}
|
||||
|
||||
public Map<String, String> createMapUsingStreamStringArray() {
|
||||
Map<String, String> map = Stream.of(new String[][] { { "Hello", "World" }, { "John", "Doe" }, })
|
||||
.collect(Collectors.toMap(data -> data[0], data -> data[1]));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String, Integer> createMapUsingStreamObjectArray() {
|
||||
Map<String, Integer> map = Stream.of(new Object[][] { { "data1", 1 }, { "data2", 2 }, })
|
||||
.collect(Collectors.toMap(data -> (String) data[0], data -> (Integer) data[1]));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String, Integer> createMapUsingStreamSimpleEntry() {
|
||||
Map<String, Integer> map = Stream.of(new AbstractMap.SimpleEntry<>("idea", 1), new AbstractMap.SimpleEntry<>("mobile", 2))
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String, Integer> createMapUsingStreamSimpleImmutableEntry() {
|
||||
Map<String, Integer> map = Stream.of(new AbstractMap.SimpleImmutableEntry<>("idea", 1), new AbstractMap.SimpleImmutableEntry<>("mobile", 2))
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String, String> createImmutableMapWithStreams() {
|
||||
Map<String, String> map = Stream.of(new String[][] { { "Hello", "World" }, { "John", "Doe" }, })
|
||||
.collect(Collectors.collectingAndThen(Collectors.toMap(data -> data[0], data -> data[1]), Collections::<String, String> unmodifiableMap));
|
||||
return map;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.map.iteration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapIteration {
|
||||
|
||||
public static void main(String[] args) {
|
||||
MapIteration mapIteration = new MapIteration();
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
|
||||
map.put("One", 1);
|
||||
map.put("Three", 3);
|
||||
map.put("Two", 2);
|
||||
|
||||
System.out.println("Iterating Keys of Map Using KeySet");
|
||||
mapIteration.iterateKeys(map);
|
||||
|
||||
System.out.println("Iterating Map Using Entry Set");
|
||||
mapIteration.iterateUsingEntrySet(map);
|
||||
|
||||
System.out.println("Iterating Using Iterator and Map Entry");
|
||||
mapIteration.iterateUsingIteratorAndEntry(map);
|
||||
|
||||
System.out.println("Iterating Using KeySet and For Each");
|
||||
mapIteration.iterateUsingKeySetAndForeach(map);
|
||||
|
||||
System.out.println("Iterating Map Using Lambda Expression");
|
||||
mapIteration.iterateUsingLambda(map);
|
||||
|
||||
System.out.println("Iterating Using Stream API");
|
||||
mapIteration.iterateUsingStreamAPI(map);
|
||||
}
|
||||
|
||||
public void iterateUsingEntrySet(Map<String, Integer> map) {
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
System.out.println(entry.getKey() + ":" + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateUsingLambda(Map<String, Integer> map) {
|
||||
map.forEach((k, v) -> System.out.println((k + ":" + v)));
|
||||
}
|
||||
|
||||
public void iterateUsingIteratorAndEntry(Map<String, Integer> map) {
|
||||
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet()
|
||||
.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<String, Integer> pair = iterator.next();
|
||||
System.out.println(pair.getKey() + ":" + pair.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateUsingKeySetAndForeach(Map<String, Integer> map) {
|
||||
for (String key : map.keySet()) {
|
||||
System.out.println(key + ":" + map.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateUsingStreamAPI(Map<String, Integer> map) {
|
||||
map.entrySet()
|
||||
.stream()
|
||||
.forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));
|
||||
}
|
||||
|
||||
public void iterateKeys(Map<String, Integer> map) {
|
||||
for (String key : map.keySet()) {
|
||||
System.out.println(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.map.mapmax;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class MapMax {
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingIteration(Map<K, V> map) {
|
||||
|
||||
Map.Entry<K, V> maxEntry = null;
|
||||
|
||||
for (Map.Entry<K, V> entry : map.entrySet()) {
|
||||
|
||||
if (maxEntry == null || entry.getValue()
|
||||
.compareTo(maxEntry.getValue()) > 0) {
|
||||
maxEntry = entry;
|
||||
}
|
||||
}
|
||||
|
||||
return maxEntry.getValue();
|
||||
}
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingCollectionsMax(Map<K, V> map) {
|
||||
|
||||
Entry<K, V> maxEntry = Collections.max(map.entrySet(), new Comparator<Entry<K, V>>() {
|
||||
public int compare(Entry<K, V> e1, Entry<K, V> e2) {
|
||||
return e1.getValue()
|
||||
.compareTo(e2.getValue());
|
||||
}
|
||||
});
|
||||
|
||||
return maxEntry.getValue();
|
||||
}
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingCollectionsMaxAndLambda(Map<K, V> map) {
|
||||
|
||||
Entry<K, V> maxEntry = Collections.max(map.entrySet(), (Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue()
|
||||
.compareTo(e2.getValue()));
|
||||
|
||||
return maxEntry.getValue();
|
||||
}
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingCollectionsMaxAndMethodReference(Map<K, V> map) {
|
||||
|
||||
Entry<K, V> maxEntry = Collections.max(map.entrySet(), Comparator.comparing(Map.Entry::getValue));
|
||||
|
||||
return maxEntry.getValue();
|
||||
}
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingStreamAndLambda(Map<K, V> map) {
|
||||
|
||||
Optional<Entry<K, V>> maxEntry = map.entrySet()
|
||||
.stream()
|
||||
.max((Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue()
|
||||
.compareTo(e2.getValue()));
|
||||
|
||||
return maxEntry.get()
|
||||
.getValue();
|
||||
}
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingStreamAndMethodReference(Map<K, V> map) {
|
||||
|
||||
Optional<Entry<K, V>> maxEntry = map.entrySet()
|
||||
.stream()
|
||||
.max(Comparator.comparing(Map.Entry::getValue));
|
||||
|
||||
return maxEntry.get()
|
||||
.getValue();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
|
||||
|
||||
map.put(1, 3);
|
||||
map.put(2, 4);
|
||||
map.put(3, 5);
|
||||
map.put(4, 6);
|
||||
map.put(5, 7);
|
||||
|
||||
MapMax mapMax = new MapMax();
|
||||
|
||||
System.out.println(mapMax.maxUsingIteration(map));
|
||||
System.out.println(mapMax.maxUsingCollectionsMax(map));
|
||||
System.out.println(mapMax.maxUsingCollectionsMaxAndLambda(map));
|
||||
System.out.println(mapMax.maxUsingCollectionsMaxAndMethodReference(map));
|
||||
System.out.println(mapMax.maxUsingStreamAndLambda(map));
|
||||
System.out.println(mapMax.maxUsingStreamAndMethodReference(map));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.map.mergemaps;
|
||||
|
||||
public class Employee implements Comparable<Employee> {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Employee(Long id, String name) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Employee employee = (Employee) o;
|
||||
|
||||
if (!id.equals(employee.id)) return false;
|
||||
return name.equals(employee.name);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = id.hashCode();
|
||||
result = 31 * result + name.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Employee employee) {
|
||||
return (int)(this.id - employee.getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.baeldung.map.mergemaps;
|
||||
|
||||
import one.util.streamex.EntryStream;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class MergeMaps {
|
||||
|
||||
private static Map<String, Employee> map1 = new HashMap<>();
|
||||
private static Map<String, Employee> map2 = new HashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
initialize();
|
||||
|
||||
mergeFunction();
|
||||
|
||||
streamConcat();
|
||||
|
||||
streamOf();
|
||||
|
||||
streamEx();
|
||||
|
||||
streamMerge();
|
||||
}
|
||||
|
||||
private static void streamMerge() {
|
||||
|
||||
Map<String, Employee> map3 = map2.entrySet()
|
||||
.stream()
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(v1, v2) -> new Employee(v1.getId(), v2.getName()),
|
||||
() -> new HashMap<>(map1)
|
||||
)
|
||||
);
|
||||
|
||||
System.out.println(map3);
|
||||
}
|
||||
|
||||
private static void streamEx() {
|
||||
Map<String, Employee> map3 = EntryStream.of(map1)
|
||||
.append(EntryStream.of(map2))
|
||||
.toMap((e1, e2) -> e1);
|
||||
|
||||
System.out.println(map3);
|
||||
|
||||
}
|
||||
|
||||
private static void streamOf() {
|
||||
Map<String, Employee> map3 = Stream.of(map1, map2)
|
||||
.flatMap(map -> map.entrySet().stream())
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(v1, v2) -> new Employee(v1.getId(), v2.getName())
|
||||
)
|
||||
);
|
||||
|
||||
map3.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static void streamConcat() {
|
||||
Map<String, Employee> result = Stream.concat(map1.entrySet().stream(), map2.entrySet().stream()).collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(value1, value2) -> new Employee(value2.getId(), value1.getName())
|
||||
));
|
||||
|
||||
result.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static void mergeFunction() {
|
||||
Map<String, Employee> map3 = new HashMap<>(map1);
|
||||
|
||||
map2.forEach(
|
||||
(key, value) -> map3.merge(key, value, (v1, v2) ->
|
||||
new Employee(v1.getId(), v2.getName()))
|
||||
);
|
||||
|
||||
map3.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
|
||||
private static void initialize() {
|
||||
Employee employee1 = new Employee(1L, "Henry");
|
||||
map1.put(employee1.getName(), employee1);
|
||||
Employee employee2 = new Employee(22L, "Annie");
|
||||
map1.put(employee2.getName(), employee2);
|
||||
Employee employee3 = new Employee(8L, "John");
|
||||
map1.put(employee3.getName(), employee3);
|
||||
|
||||
Employee employee4 = new Employee(2L, "George");
|
||||
map2.put(employee4.getName(), employee4);
|
||||
Employee employee5 = new Employee(3L, "Henry");
|
||||
map2.put(employee5.getName(), employee5);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.map;
|
||||
package com.baeldung.map.primitives;
|
||||
|
||||
import cern.colt.map.AbstractIntDoubleMap;
|
||||
import cern.colt.map.OpenIntDoubleHashMap;
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.baeldung.map.sort;
|
||||
|
||||
import com.baeldung.map.mergemaps.Employee;
|
||||
import com.google.common.base.Functions;
|
||||
import com.google.common.collect.ImmutableSortedMap;
|
||||
import com.google.common.collect.Ordering;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SortHashMap {
|
||||
|
||||
private static Map<String, Employee> map = new HashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
initialize();
|
||||
|
||||
treeMapSortByKey();
|
||||
|
||||
arrayListSortByValue();
|
||||
arrayListSortByKey();
|
||||
|
||||
sortStream();
|
||||
|
||||
sortGuava();
|
||||
|
||||
addDuplicates();
|
||||
|
||||
treeSetByKey();
|
||||
treeSetByValue();
|
||||
|
||||
}
|
||||
|
||||
private static void sortGuava() {
|
||||
final Ordering naturalOrdering =
|
||||
Ordering.natural().onResultOf(Functions.forMap(map, null));
|
||||
|
||||
System.out.println(ImmutableSortedMap.copyOf(map, naturalOrdering));
|
||||
}
|
||||
|
||||
private static void sortStream() {
|
||||
map.entrySet().stream()
|
||||
.sorted(Map.Entry.<String, Employee>comparingByKey().reversed())
|
||||
.forEach(System.out::println);
|
||||
|
||||
Map<String, Employee> result = map.entrySet().stream()
|
||||
.sorted(Map.Entry.comparingByValue())
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
|
||||
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
|
||||
|
||||
result.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static void treeSetByValue() {
|
||||
SortedSet<Employee> values = new TreeSet<>(map.values());
|
||||
System.out.println(values);
|
||||
}
|
||||
|
||||
private static void treeSetByKey() {
|
||||
SortedSet<String> keysSet = new TreeSet<>(map.keySet());
|
||||
System.out.println(keysSet);
|
||||
}
|
||||
|
||||
private static void treeMapSortByKey() {
|
||||
TreeMap<String, Employee> sorted = new TreeMap<>(map);
|
||||
sorted.putAll(map);
|
||||
|
||||
sorted.entrySet().forEach(System.out::println);
|
||||
|
||||
}
|
||||
|
||||
private static void arrayListSortByValue() {
|
||||
List<Employee> employeeById = new ArrayList<>(map.values());
|
||||
|
||||
Collections.sort(employeeById);
|
||||
|
||||
System.out.println(employeeById);
|
||||
}
|
||||
|
||||
private static void arrayListSortByKey() {
|
||||
List<String> employeeByKey = new ArrayList<>(map.keySet());
|
||||
Collections.sort(employeeByKey);
|
||||
System.out.println(employeeByKey);
|
||||
}
|
||||
|
||||
private static void initialize() {
|
||||
Employee employee1 = new Employee(1L, "Mher");
|
||||
map.put(employee1.getName(), employee1);
|
||||
Employee employee2 = new Employee(22L, "Annie");
|
||||
map.put(employee2.getName(), employee2);
|
||||
Employee employee3 = new Employee(8L, "John");
|
||||
map.put(employee3.getName(), employee3);
|
||||
Employee employee4 = new Employee(2L, "George");
|
||||
map.put(employee4.getName(), employee4);
|
||||
}
|
||||
|
||||
private static void addDuplicates() {
|
||||
Employee employee5 = new Employee(1L, "Mher");
|
||||
map.put(employee5.getName(), employee5);
|
||||
Employee employee6 = new Employee(22L, "Annie");
|
||||
map.put(employee6.getName(), employee6);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user