This project is born out of curiosity. This project currently consists of custom collectors and Convenience Factory Methods for Collections (JEP 269). I haven't thought what to add next but I will update README if I add any new things to it.
So what is this project essentially? This project currently has 2 pieces of cake:
- Custom Collectors (Here are all Collectors.)
- Convenience Factory Methods for Collections
Let us understand them one by one.
-
Collections with initial capacity and Comparator.
-
With this Custom Collectors you can collect data in Collection with initial capacity and comparator(TreeSet). Below are few samples of it. ``` List names = people.stream() .map(Person::getName) .collect(toArrayList(20));
List<String> names = people.stream() .map(Person::getName) .collect(toVector(20)); Set<String> names = people.stream() .map(Person::getName) .collect(toHashSet(20)); Set<String> names = people.stream() .map(Person::getName) .collect(toLinkedHashSet(20)); NavigableSet<String> names = people.stream() .map(Person::getName) .collect(toTreeSet(String::compareTo)) ```
-
-
Google Guava Collections.
- With this Custom Collectors you can collect data in Google Guava Collections. Below are few samples of it.
ImmutableList<String> immutableList = people.stream() .map(Person::getName) .collect(toImmutableList()); ImmutableSet<String> immutableSet = people.stream() .map(Person::getName) .collect(toImmutableSet()); ImmutableSortedSet<String> immutableSortedSet = people.stream() .map(Person::getName) .collect(toImmutableSortedSet()); ImmutableSortedSet<String> immutableSortedSet =people.stream() .map(Person::getName) .collect(toImmutableSortedSetReverse()); ImmutableMap<Integer, Entry<Integer, String>> immutableMap = map.entrySet() .stream() .collect(toImmutableMap(k -> k.getKey())); ImmutableMap<Integer, String> immutableMap = map.entrySet() .stream() .collect(toImmutableMap( k -> k.getKey(), v -> v.getValue())); ```
- With this Custom Collectors you can collect data in Google Guava Collections. Below are few samples of it.
- Factory methods for creating
List
.Lists.of()
returns aList<>
//returns immutable list containing no or zero(0) elements. List<Integer> list = Lists.of(); //returns immutable list containing 1 element. List<Integer> list = Lists.of(1); //returns immutable list containing 2 elements. List<Integer> list = Lists.of(1, 2); .. and so on.
- Factory methods for creating
Set
.Sets.of()
returnsHashSet<>
//returns immutable set containing no or zero(0) elements. Set<Integer> set = Sets.of() //returns immutable set containing 1 element. Set<Integer> set = Sets.of(1); //returns immutable set containing 2 elements. Set<Integer> set = Sets.of(1, 2); .. and so on.
Sets.ofOrdered()
returnsLinkedHashSet<>
//returns immutable set containing no or zero(0) elements. Set<Integer> set = Sets.ofOrdered() //returns immutable set containing 1 element. Set<Integer> set = Sets.ofOrdered(1); //returns immutable set containing 2 elements. Set<Integer> set = Sets.ofOrdered(1, 2); .. and so on.
Sets.ofSorted()
returnsTreeSet<>
//returns immutable set containing no or zero(0) elements. Set<Integer> set = Sets.ofSorted() //returns immutable set containing 1 element. Set<Integer> set = Sets.ofSorted(1); //returns immutable set containing 2 elements. Set<Integer> set = Sets.ofSorted(1, 2); .. and so on.
- Factory methods for creating
Map
.-
Maps.of()
returnsHashMap<>
//returns immutable map containing no or zero(0) entries. Map<Integer, Integer> map = Maps.of(); //returns immutable map containing 1 entry. Map<Integer, Integer> map = Maps.of(1, 1);
-
Maps.ofOrdered()
returnsLinkedHashMap<>
//returns immutable map containing no or zero(0) entries. Map<Integer, Integer> map = Maps.ofOrdered(); //returns immutable map containing 1 entry. Map<Integer, Integer> map = Maps.ofOrdered(1, 1);
-
Maps.ofSorted()
returnsTreeMap<>
//returns immutable map containing no or zero(0) entries. Map<Integer, Integer> map = Maps.ofSorted(); //returns immutable map containing 1 entry. Map<Integer, Integer> map = Maps.ofSorted(1, 1);
-
Maps.ofEntries
returnsHashMap<>
.Map<Character, Integer> map = Maps.ofEntries( Maps.entry('A', 65), Maps.entry('B', 66), Maps.entry('C', 67)); Map<Character, Integer> map = Maps.ofOrderedEntries( Maps.entry('A', 65), Maps.entry('B', 66), Maps.entry('C', 67)); Map<Character, Integer> map = Maps.ofSortedEntries( Maps.entry('A', 65), Maps.entry('B', 66), Maps.entry('C', 67));
-
Enjoy.
TODO:
- Add additional operations for java.util.Optional.
- Check if we can add better methods for Collectors.
- Add comments.