10000 Arrays, collections and data structures · 2714rohit/Java-Coding-Problems@ddd3fbe · GitHub
[go: up one dir, main page]

Skip to content

Commit ddd3fbe

Browse files
committed
Arrays, collections and data structures
1 parent a4e3fe1 commit ddd3fbe

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-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>
10000 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_1_ChunkList</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_1_ChunkList</name>
14+
</project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package modern.challenge;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
import java.util.Spliterator;
8+
import java.util.Spliterators;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.StreamSupport;
11+
12+
public class Converters {
13+
14+
private Converters() {
15+
throw new AssertionError("Cannot be instantiatied");
16+
}
17+
18+
public static <T> List<T> iterableToList1(Iterable<T> iterable) {
19+
20+
if (iterable == null) {
21+
return Collections.emptyList();
22+
}
23+
24+
List<T> result = new ArrayList<>();
25+
iterable.forEach(result::add);
26+
27+
return result;
28+
}
29+
30+
public static <T> List<T> iterableToList2(Iterable<T> iterable) {
31+
32+
if (iterable == null) {
33+
return Collections.emptyList();
34+
}
35+
36+
List<T> result = StreamSupport.stream(iterable.spliterator(), false)
37+
.collect(Collectors.toList());
38+
39+
return result;
40+
}
41+
42+
public static <T> List<T> iterableToList3(Iterable<T> iterable) {
43+
44+
if (iterable == null) {
45+
return Collections.emptyList();
46+
}
47+
48+
List<T> result = new ArrayList<>();
49+
iterable.iterator().forEachRemaining(result::add);
50+
51+
return result;
52+
}
53+
54+
public static <T> List<T> iterableToList4(Iterable<T> iterable) {
55+
56+
if (iterable == null) {
57+
return Collections.emptyList();
58+
}
59+
60+
List<T> result
61+
= StreamSupport.stream(Spliterators.
62+
spliteratorUnknownSize(iterable.iterator(), Spliterator.ORDERED), false)
63+
.collect(Collectors.toList());
64+
65+
return result;
66+
}
67+
68+
public static <T> List<T> iterableToList5(Iterable<T> iterable) {
< F438 code>69+
70+
if (iterable == null) {
71+
return Collections.emptyList();
72+
}
73+
74+
List<T> result = new ArrayList<>();
75+
for (T elem : iterable) {
76+
result.add(elem);
77+
}
78+
79+
return result;
80+
}
81+
82+
public static <T> List<T> iterableToList6(Iterable<T> iterable) {
83+
84+
if (iterable == null) {
85+
return Collections.emptyList();
86+
}
87+
88+
List<T> result = new ArrayList<>();
89+
Iterator<T> iterator = iterable.iterator();
90+
while (iterator.hasNext()) {
91+
result.add(iterator.next());
92+
}
93+
94+
return result;
95+
}
96+
97+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package modern.challenge;
2+
3+
import java.util.Arrays;
4+
5+
public class MainApplication {
6+
7+
public static void main(String[] args) {
8+
9+
// let's consider the next Iterable
10+
Iterable<String> iterable = Arrays.asList("ana", "george", "mark");
11+
12+
System.out.println("iterableToList1(): " + Converters.iterableToList1(iterable));
13+
System.out.println("iterableToList2(): " + Converters.iterableToList2(iterable));
14+
System.out.println("iterableToList3(): " + Converters.iterableToList3(iterable));
15+
System.out.println("iterableToList4(): " + Converters.iterableToList4(iterable));
16+
System.out.println("iterableToList5(): " + Converters.iterableToList5(iterable));
17+
System.out.println("iterableToList6(): " + Converters.iterableToList6(iterable));
18+
}
19+
}

0 commit comments

Comments
 (0)
0