diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/README.md b/README.md new file mode 100644 index 0000000..cf0194d --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +## Stream Utils ## + +A collection of useful steam utilities + + +## Slice ## + +Get a subset of a Stream + +```java +StreamUtils.getSlice(steam, 0, 5); +``` + +Get a subset of a List + +```java +StreamUtils.getSlice(list, 0, 5); +``` + +## Distinct by Key ## + +Predicate used to filter objects by a given property + +```java +List uniqueUsersByLastname = users.stream() + .filter(StreamUtils.distinctByKey(user -> user.getLastName())) + .collect(Collectors.toList()); +``` \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..aca1f0a --- /dev/null +++ b/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + + com.ljunggren + stream-utils + 0.0.1-SNAPSHOT + jar + + steam-utils + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + org.projectlombok + lombok + 1.18.12 + provided + + + junit + junit + 4.13 + test + + + diff --git a/src/main/java/com/ljunggren/streamUtils/StreamUtils.java b/src/main/java/com/ljunggren/streamUtils/StreamUtils.java new file mode 100644 index 0000000..a2ad3da --- /dev/null +++ b/src/main/java/com/ljunggren/streamUtils/StreamUtils.java @@ -0,0 +1,30 @@ +package com.ljunggren.streamUtils; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class StreamUtils { + + public static List getSlice(List list, int fromIndex, int toIndex) { + Stream stream = list.stream(); + return getSlice(stream, fromIndex, toIndex) + .collect(Collectors.toList()); + } + + public static Stream getSlice(Stream stream, int fromIndex, int toIndex) { + return stream + .skip(fromIndex) + .limit(toIndex - fromIndex + 1); + } + + public static Predicate distinctByKey(Function keyExtractor) { + Map seen = new ConcurrentHashMap<>(); + return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; + } + +} diff --git a/src/test/java/com/ljunggren/streamUtils/StreamUtilsTest.java b/src/test/java/com/ljunggren/streamUtils/StreamUtilsTest.java new file mode 100644 index 0000000..545d21c --- /dev/null +++ b/src/test/java/com/ljunggren/streamUtils/StreamUtilsTest.java @@ -0,0 +1,56 @@ +package com.ljunggren.streamUtils; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.Test; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +public class StreamUtilsTest { + + @Test + public void getSliceTest() { + List states = Arrays.asList(new String[] { + "Alabama", + "Alaska", + "Arizona", + "Arkansas", + "California", + "Colorado", + "Connecticut", + "Delaware", + "Florida", + "Georgia" + }); + List sublist = StreamUtils.getSlice(states, 2, 6); + assertEquals(states.get(2), sublist.get(0)); + assertEquals(states.get(6), sublist.get(4)); + } + + @Test + public void distinctByKeyTest() { + @Getter + @AllArgsConstructor + class User { + private String firstName; + private String lastName; + } + List users = Arrays.asList(new User[] { + new User("Alex", "Ljunggren"), + new User("James", "Ljunggren"), + new User("Jane", "Doe") + }); + List uniqueUsersByLastname = users.stream() + .filter(StreamUtils.distinctByKey(user -> user.getLastName())) + .collect(Collectors.toList()); + assertEquals(2, uniqueUsersByLastname.size()); + assertEquals(users.get(0), uniqueUsersByLastname.get(0)); + assertEquals(users.get(2), uniqueUsersByLastname.get(1)); + } + +}