8000 Convert List<V> into Map<K, List<V>> · allwaysoft/Java-Coding-Problems@bc72f7a · GitHub
[go: up one dir, main page]

Skip to content

Commit bc72f7a

Browse files
committed
Convert List<V> into Map<K, List<V>>
1 parent 3f8c2e9 commit bc72f7a

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**[How To Efficiently Chunk A Java List](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/ChunkList)**
2+
3+
<b><a href="https://persistencelayer.wixsite.com/springboot-hibernate/post/how-to-efficiently-chunk-a-java-list">If you prefer to read it as a blog-post containing the relevant snippets of code then check this post</a></b>
4+
5+
**Description:** Is a common scenario to have a big `List` and to need to chunk it in multiple smaller `List` of a given size. For example, if we want to employ a concurrent batch implementation we need to give each thread a sublist of items. Chunking a list can be done via Google Guava, `Lists.partition(List list, int size)` [method](https://guava.dev/releases/22.0/api/docs/com/google/common/collect/Lists.html#partition-java.util.List-int-) or Apache Commons Collections, `ListUtils.partition(List list, int size)` [method](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/ListUtils.html#partition(java.util.List,%20int)). But, it can be implemented in plain Java as well. This application exposes 6 ways to do it. The trade-off is between the speed of implementation and speed of execution. For example, while the implementation relying on grouping collectors is not performing very well, it is quite simple and fast to write it.
6+
7+
**Key points:**
8+
- the fastest execution is provided by `Chunk.java` class which relies on the built-in `List.subList()` method
9+
10+
**Time-performance trend graphic for chunking 500, 1_000_000, 10_000_000 and 20_000_000 items in lists of 5 items:**\
11+
![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/ChunkList/head-to-head.png)
12+
13+
-----------------------------------------------------------------------------------------------------------------------
14+
<table>
15+
<tr><td><b>If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"</b></td><td><b>If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.</b></td></tr>
16+
<tr><td>
17+
<a href="https://www.apress.com/us/book/9781484256251"><p align="left"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Spring%20Boot%20Persistence%20Best%20Practices.jpg" height="500" width="450"/></p></a>
18+
</td><td>
19+
<a href="https://leanpub.com/java-persistence-performance-illustrated-guide"><p align="right"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Java%20Persistence%20Performance%20Illustrated%20Guide.jpg" height="500" width="450"/></p></a>
20+
</td></tr></table>
21+
22+
-----------------------------------------------------------------------------------------------------------------------
23+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.app</groupId>
5+
<artifactId>BONUS_3_ConvertListVtoMapKListV</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>13</maven.compiler.source>
11+
<maven.compiler.target>13</maven.compiler.target>
12+
</properties>
13+
<name>BONUS_3_ConvertListVtoMapKListV</name>
14+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package modern.challenge;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.function.Function;
10+
import java.util.function.Supplier;
11+
import java.util.stream.Collectors;
12+
13+
public class Converters {
14+
15+
private Converters() {
16+
throw new AssertionError("Cannot be instantiatied");
17+
}
18+
19+
public static Map<Integer, List<String>> toMap(List<String> list) {
20+
21+
if (list == null || list.isEmpty()) {
22+
return Collections.emptyMap();
23+
}
24+
25+
return list.stream().collect(
26+
Collectors.groupingBy(String::length,
27+
HashMap::new, Collectors.toCollection(ArrayList::new))
28+
);
29+
}
30+
31+
@SuppressWarnings("unchecked")
32+
public static <K, V, C extends Collection<V>, M extends Map<K, C>> M toMap(
33+
List<V> list, Function<? super V, ? extends K> c, Supplier<M> ms, Supplier<C> cs) {
34+
35+
if (list == null || c == null || ms == null || cs == null
36+
|| list.isEmpty()) {
37+
38+
throw new IllegalArgumentException("Non of the arguments can be null or empty");
39+
}
40+
41+
return list.stream().collect(
42+
Collectors.groupingBy(c, ms, Collectors.toCollection(cs))
43+
);
44+
}
45+
46+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package modern.challenge;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.LinkedHashMap;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
9+
public class MainApplication {
10+
11+
public static void main(String[] args) {
12+
13+
/* Convert List<V> into Map<K, List<V>> */
14+
// consider this list
15+
List<String> names
16+
= List.of("joana", "mark", "adela", "leo", "stan", "marius", "kely");
17+
18+
HashMap<Integer, List<String>> result1
19+
= Converters.toMap(names, String::length, HashMap::new, ArrayList::new);
20+
System.out.println("HashMap<Integer, List<String>>: " + result1);
21+
22+
LinkedHashMap<Integer, List<String>> result2
23+
= Converters.toMap(names, String::length, LinkedHashMap::new, LinkedList::new);
24+
System.out.println("LinkedHashMap<Integer, List<String>>: " + result2);
25+
}
26+
}

0 commit comments

Comments
 (0)
0