public final class Iterables extends Object
Modifier and Type | Method and Description |
---|---|
static <E> List<List<E>> |
cartesianProduct(Collection<? extends Collection<? extends E>> cs)
Note: copy from Google Guava under Apache License v2.
|
static <E> List<List<E>> |
cartesianProduct(Collection<? extends E>... cs)
Note: copy from Google Guava under Apache License v2.
|
static <T> Set<T> |
commonSet(Collection<? extends Collection<? extends T>> c) |
static <T> Set<T> |
commonSet(Collection<? extends T> a,
Collection<?> b) |
static <T> Set<T> |
differentSet(Collection<? extends T> a,
Collection<?> b) |
static <T,E extends Exception> |
findFirst(Collection<T> c,
Try.Predicate<? super T,E> predicate) |
static <T,E extends Exception> |
findFirst(T[] a,
Try.Predicate<? super T,E> predicate) |
static <T,E extends Exception> |
findFirstIndex(Collection<? extends T> c,
Try.Predicate<? super T,E> predicate)
Find first index.
|
static <T,E extends Exception> |
findFirstIndex(T[] a,
Try.Predicate<? super T,E> predicate)
Find first index.
|
static <T,E extends Exception> |
findFirstNonNull(Collection<T> c,
Try.Predicate<? super T,E> predicate)
Find first non null.
|
static <T,E extends Exception> |
findFirstNonNull(T[] a,
Try.Predicate<? super T,E> predicate)
Find first non null.
|
static <T,E extends Exception> |
findLast(Collection<T> c,
Try.Predicate<? super T,E> predicate) |
static <T,E extends Exception> |
findLast(T[] a,
Try.Predicate<? super T,E> predicate) |
static <T,E extends Exception> |
findLastIndex(Collection<? extends T> c,
Try.Predicate<? super T,E> predicate)
Find last index.
|
static <T,E extends Exception> |
findLastIndex(T[] a,
Try.Predicate<? super T,E> predicate)
Find last index.
|
static <T,E extends Exception> |
findLastNonNull(Collection<T> c,
Try.Predicate<? super T,E> predicate)
Find last non null.
|
static <T,E extends Exception> |
findLastNonNull(T[] a,
Try.Predicate<? super T,E> predicate)
Find last non null.
|
static <T> u.Nullable<T> |
getOnlyElement(Iterable<? extends T> iterable)
Gets the only element.
|
static u.OptionalInt |
indexOf(Collection<?> c,
Object objToFind) |
static u.OptionalInt |
lastIndexOf(Collection<?> c,
Object objToFind)
Last index of.
|
static <E extends Comparable<? super E>> |
orderedPermutations(Collection<E> elements)
Note: copy from Google Guava under Apache License v2.
|
static <E> Collection<List<E>> |
orderedPermutations(Collection<E> elements,
Comparator<? super E> comparator)
Note: copy from Google Guava under Apache License v2.
|
static <T,E extends Exception> |
parse(Collection<? extends Iterator<? extends T>> iterators,
int readThreadNum,
int processThreadNum,
int queueSize,
Try.Consumer<? super T,E> elementParser) |
static <T,E extends Exception,E2 extends Exception> |
parse(Collection<? extends Iterator<? extends T>> iterators,
int readThreadNum,
int processThreadNum,
int queueSize,
Try.Consumer<? super T,E> elementParser,
Try.Runnable<E2> onComplete) |
static <T,E extends Exception> |
parse(Collection<? extends Iterator<? extends T>> iterators,
long offset,
long count,
int readThreadNum,
int processThreadNum,
int queueSize,
Try.Consumer<? super T,E> elementParser) |
static <T,E extends Exception,E2 extends Exception> |
parse(Collection<? extends Iterator<? extends T>> iterators,
long offset,
long count,
int readThreadNum,
int processThreadNum,
int queueSize,
Try.Consumer<? super T,E> elementParser,
Try.Runnable<E2> onComplete)
Parse the elements in the specified iterators one by one.
|
static <T,E extends Exception> |
parse(Collection<? extends Iterator<? extends T>> iterators,
long offset,
long count,
Try.Consumer<? super T,E> elementParser) |
static <T,E extends Exception,E2 extends Exception> |
parse(Collection<? extends Iterator<? extends T>> iterators,
long offset,
long count,
Try.Consumer<? super T,E> elementParser,
Try.Runnable<E2> onComplete) |
static <T,E extends Exception> |
parse(Collection<? extends Iterator<? extends T>> iterators,
Try.Consumer<? super T,E> elementParser) |
static <T,E extends Exception,E2 extends Exception> |
parse(Collection<? extends Iterator<? extends T>> iterators,
Try.Consumer<? super T,E> elementParser,
Try.Runnable<E2> onComplete) |
static <T,E extends Exception> |
parse(Iterator<? extends T> iter,
long offset,
long count,
int processThreadNum,
int queueSize,
Try.Consumer<? super T,E> elementParser) |
static <T,E extends Exception,E2 extends Exception> |
parse(Iterator<? extends T> iter,
long offset,
long count,
int processThreadNum,
int queueSize,
Try.Consumer<? super T,E> elementParser,
Try.Runnable<E2> onComplete)
Parse the elements in the specified iterators one by one.
|
static <T,E extends Exception> |
parse(Iterator<? extends T> iter,
long offset,
long count,
Try.Consumer<? super T,E> elementParser) |
static <T,E extends Exception,E2 extends Exception> |
parse(Iterator<? extends T> iter,
long offset,
long count,
Try.Consumer<? super T,E> elementParser,
Try.Runnable<E2> onComplete) |
static <T,E extends Exception> |
parse(Iterator<? extends T> iter,
Try.Consumer<? super T,E> elementParser) |
static <T,E extends Exception,E2 extends Exception> |
parse(Iterator<? extends T> iter,
Try.Consumer<? super T,E> elementParser,
Try.Runnable<E2> onComplete) |
static <E> Collection<List<E>> |
permutations(Collection<E> elements)
Note: copy from Google Guava under Apache License v2.
|
static <E> Set<Set<E>> |
powerSet(Set<E> set)
Note: copy from Google Guava under Apache License v2.
|
static boolean |
removeAll(Collection<?> c,
Collection<?> objsToRemove)
Removes the all.
|
static boolean |
retainAll(Collection<?> c,
Collection<?> objsToKeep) |
static <T> List<List<T>> |
rollup(Collection<? extends T> c) |
static <T> Stream<T> |
splitByCount(int maxCount,
int totalSize,
boolean smallerFirst,
IntBiFunction<T> func)
final int[] a = Array.rangeClosed(1, 7);
splitByCount(5, 7, true, (fromIndex, toIndex) -> N.copyOfRange(a, fromIndex, toIndex)).forEach(Fn.println()); // [[1], [2], [3], [4, 5], [6, 7]]
splitByCount(5, 7, false, (fromIndex, toIndex) -> N.copyOfRange(a, fromIndex, toIndex)).forEach(Fn.println()); // [[1, 2], [3, 4], [5], [6], [7]]
|
static <T> Set<T> |
symmetricDifferentSet(Collection<? extends T> a,
Collection<? extends T> b)
Symmetric different set.
|
public static <T> Set<T> differentSet(Collection<? extends T> a, Collection<?> b)
T
- a
- b
- public static <T> Set<T> symmetricDifferentSet(Collection<? extends T> a, Collection<? extends T> b)
T
- a
- b
- public static <T> Set<T> commonSet(Collection<? extends T> a, Collection<?> b)
T
- a
- b
- public static <T> Set<T> commonSet(Collection<? extends Collection<? extends T>> c)
T
- c
- public static boolean removeAll(Collection<?> c, Collection<?> objsToRemove)
c
- objsToRemove
- public static boolean retainAll(Collection<?> c, Collection<?> objsToKeep)
c
- objsToKeep
- public static <T> u.Nullable<T> getOnlyElement(Iterable<? extends T> iterable)
T
- iterable
- iterable
.public static u.OptionalInt indexOf(Collection<?> c, Object objToFind)
c
- objToFind
- public static u.OptionalInt lastIndexOf(Collection<?> c, Object objToFind)
c
- objToFind
- public static <T,E extends Exception> u.OptionalInt findFirstIndex(T[] a, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- a
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.OptionalInt findFirstIndex(Collection<? extends T> c, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- c
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.OptionalInt findLastIndex(T[] a, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- a
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.OptionalInt findLastIndex(Collection<? extends T> c, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- c
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.Nullable<T> findFirst(T[] a, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- a
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.Nullable<T> findFirst(Collection<T> c, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- c
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.Nullable<T> findLast(T[] a, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- a
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.Nullable<T> findLast(Collection<T> c, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- c
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.Optional<T> findFirstNonNull(T[] a, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- a
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.Optional<T> findFirstNonNull(Collection<T> c, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- c
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.Optional<T> findLastNonNull(T[] a, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- a
- predicate
- E
- the eE extends Exception
public static <T,E extends Exception> u.Optional<T> findLastNonNull(Collection<T> c, Try.Predicate<? super T,E> predicate) throws E extends Exception
T
- E
- c
- predicate
- E
- the eE extends Exception
public static <T> List<List<T>> rollup(Collection<? extends T> c)
T
- c
- public static <E> Set<Set<E>> powerSet(Set<E> set)
set
. For example,
powerSet(ImmutableSet.of(1, 2))
returns the set {{},
{1}, {2}, {1, 2}}
.
Elements appear in these subsets in the same iteration order as they appeared in the input set. The order in which these subsets appear in the outer set is undefined. Note that the power set of the empty set is not the empty set, but a one-element set containing the empty set.
The returned set and its constituent sets use equals
to decide
whether two elements are identical, even if the input set uses a different
concept of equivalence.
Performance notes: while the power set of a set with size n
is of size 2^n
, its memory usage is only O(n)
. When the
power set is constructed, the input set is merely copied. Only as the
power set is iterated are the individual subsets created, and these subsets
themselves occupy only a small constant amount of memory.
E
- set
- the set of elements to construct a power set fromIllegalArgumentException
- if set
has more than 30 unique
elements (causing the power set size to exceed the int
range)NullPointerException
- if set
is or contains null
public static <E> Collection<List<E>> permutations(Collection<E> elements)
Collection
of all the permutations of the specified
Collection
.
Notes: This is an implementation of the Plain Changes algorithm for permutations generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2.
If the input list contains equal elements, some of the generated permutations will be equal.
An empty collection has only one permutation, which is an empty list.
E
- elements
- the original collection whose elements have to be permuted.Collection
containing all the different
permutations of the original collection.NullPointerException
- if the specified collection is null or has any
null elements.public static <E extends Comparable<? super E>> Collection<List<E>> orderedPermutations(Collection<E> elements)
Collection
of all the permutations of the specified
Iterable
.
Notes: This is an implementation of the algorithm for Lexicographical Permutations Generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The iteration order follows the lexicographical order. This means that the first permutation will be in ascending order, and the last will be in descending order.
Duplicate elements are considered equal. For example, the list [1, 1]
will have only one permutation, instead of two. This is why the elements
have to implement Comparable
.
An empty iterable has only one permutation, which is an empty list.
This method is equivalent to
Collections2.orderedPermutations(list, Ordering.natural())
.
E
- elements
- the original iterable whose elements have to be permuted.Collection
containing all the different
permutations of the original iterable.NullPointerException
- if the specified iterable is null or has any
null elements.public static <E> Collection<List<E>> orderedPermutations(Collection<E> elements, Comparator<? super E> comparator)
Collection
of all the permutations of the specified
Iterable
using the specified Comparator
for establishing
the lexicographical ordering.
Examples:
for (List<String> perm : orderedPermutations(asList("b", "c", "a"))) {
println(perm);
}
// -> ["a", "b", "c"]
// -> ["a", "c", "b"]
// -> ["b", "a", "c"]
// -> ["b", "c", "a"]
// -> ["c", "a", "b"]
// -> ["c", "b", "a"]
for (List<Integer> perm : orderedPermutations(asList(1, 2, 2, 1))) {
println(perm);
}
// -> [1, 1, 2, 2]
// -> [1, 2, 1, 2]
// -> [1, 2, 2, 1]
// -> [2, 1, 1, 2]
// -> [2, 1, 2, 1]
// -> [2, 2, 1, 1]
Notes: This is an implementation of the algorithm for Lexicographical Permutations Generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The iteration order follows the lexicographical order. This means that the first permutation will be in ascending order, and the last will be in descending order.
Elements that compare equal are considered equal and no new permutations are created by swapping them.
An empty iterable has only one permutation, which is an empty list.
E
- elements
- the original iterable whose elements have to be permuted.comparator
- a comparator for the iterable's elements.Collection
containing all the different
permutations of the original iterable.NullPointerException
- If the specified iterable is null, has any
null elements, or if the specified comparator is null.@SafeVarargs public static <E> List<List<E>> cartesianProduct(Collection<? extends E>... cs)
Lists.cartesianProduct(ImmutableList.of(
ImmutableList.of(1, 2),
ImmutableList.of("A", "B", "C")))
returns a list containing six lists in the following order:
ImmutableList.of(1, "A")
ImmutableList.of(1, "B")
ImmutableList.of(1, "C")
ImmutableList.of(2, "A")
ImmutableList.of(2, "B")
ImmutableList.of(2, "C")
The result is guaranteed to be in the "traditional", lexicographical order for Cartesian products that you would get from nesting for loops:
for (B b0 : lists.get(0)) {
for (B b1 : lists.get(1)) {
...
ImmutableList<B> tuple = ImmutableList.of(b0, b1, ...);
// operate on tuple
}
}
Note that if any input list is empty, the Cartesian product will also be empty. If no lists at all are provided (an empty list), the resulting Cartesian product has one element, an empty list (counter-intuitive, but mathematically consistent).
Performance notes: while the cartesian product of lists of size
m, n, p
is a list of size m x n x p
, its actual memory
consumption is much smaller. When the cartesian product is constructed, the
input lists are merely copied. Only as the resulting list is iterated are
the individual lists created, and these are not retained after iteration.
E
- any common base class shared by all axes (often just Object
)cs
- the lists to choose elements from, in the order that
the elements chosen from those lists should appear in the resulting
listsIllegalArgumentException
- if the size of the cartesian product would
be greater than Integer.MAX_VALUE
NullPointerException
- if lists
, any one of the
lists
, or any element of a provided list is nullpublic static <E> List<List<E>> cartesianProduct(Collection<? extends Collection<? extends E>> cs)
Lists.cartesianProduct(ImmutableList.of(
ImmutableList.of(1, 2),
ImmutableList.of("A", "B", "C")))
returns a list containing six lists in the following order:
ImmutableList.of(1, "A")
ImmutableList.of(1, "B")
ImmutableList.of(1, "C")
ImmutableList.of(2, "A")
ImmutableList.of(2, "B")
ImmutableList.of(2, "C")
The result is guaranteed to be in the "traditional", lexicographical order for Cartesian products that you would get from nesting for loops:
for (B b0 : lists.get(0)) {
for (B b1 : lists.get(1)) {
...
ImmutableList<B> tuple = ImmutableList.of(b0, b1, ...);
// operate on tuple
}
}
Note that if any input list is empty, the Cartesian product will also be empty. If no lists at all are provided (an empty list), the resulting Cartesian product has one element, an empty list (counter-intuitive, but mathematically consistent).
Performance notes: while the cartesian product of lists of size
m, n, p
is a list of size m x n x p
, its actual memory
consumption is much smaller. When the cartesian product is constructed, the
input lists are merely copied. Only as the resulting list is iterated are
the individual lists created, and these are not retained after iteration.
E
- any common base class shared by all axes (often just Object
)cs
- the lists to choose elements from, in the order that
the elements chosen from those lists should appear in the resulting
listsIllegalArgumentException
- if the size of the cartesian product would
be greater than Integer.MAX_VALUE
NullPointerException
- if lists
, any one of the lists
,
or any element of a provided list is nullpublic static <T,E extends Exception> void parse(Iterator<? extends T> iter, Try.Consumer<? super T,E> elementParser) throws E extends Exception
T
- E
- iter
- elementParser
- E
- the eE extends Exception
public static <T,E extends Exception,E2 extends Exception> void parse(Iterator<? extends T> iter, Try.Consumer<? super T,E> elementParser, Try.Runnable<E2> onComplete) throws E extends Exception, E2 extends Exception
T
- E
- E2
- iter
- elementParser
- onComplete
- E
- the eE2
- the e2E extends Exception
public static <T,E extends Exception> void parse(Iterator<? extends T> iter, long offset, long count, Try.Consumer<? super T,E> elementParser) throws E extends Exception
T
- E
- iter
- offset
- count
- elementParser
- E
- the eE extends Exception
public static <T,E extends Exception,E2 extends Exception> void parse(Iterator<? extends T> iter, long offset, long count, Try.Consumer<? super T,E> elementParser, Try.Runnable<E2> onComplete) throws E extends Exception, E2 extends Exception
T
- E
- E2
- iter
- offset
- count
- elementParser
- onComplete
- E
- the eE2
- the e2E extends Exception
public static <T,E extends Exception> void parse(Iterator<? extends T> iter, long offset, long count, int processThreadNum, int queueSize, Try.Consumer<? super T,E> elementParser) throws E extends Exception
T
- E
- iter
- offset
- count
- processThreadNum
- queueSize
- elementParser
- E
- the eE extends Exception
public static <T,E extends Exception,E2 extends Exception> void parse(Iterator<? extends T> iter, long offset, long count, int processThreadNum, int queueSize, Try.Consumer<? super T,E> elementParser, Try.Runnable<E2> onComplete) throws E extends Exception, E2 extends Exception
T
- E
- E2
- iter
- offset
- count
- processThreadNum
- new threads started to parse/process the lines/recordsqueueSize
- size of queue to save the processing records/lines loaded from source data. Default size is 1024.elementParser
- onComplete
- E
- the eE2
- the e2E extends Exception
public static <T,E extends Exception> void parse(Collection<? extends Iterator<? extends T>> iterators, Try.Consumer<? super T,E> elementParser) throws E extends Exception
T
- E
- iterators
- elementParser
- E
- the eE extends Exception
public static <T,E extends Exception,E2 extends Exception> void parse(Collection<? extends Iterator<? extends T>> iterators, Try.Consumer<? super T,E> elementParser, Try.Runnable<E2> onComplete) throws E extends Exception, E2 extends Exception
T
- E
- E2
- iterators
- elementParser
- onComplete
- E
- the eE2
- the e2E extends Exception
public static <T,E extends Exception> void parse(Collection<? extends Iterator<? extends T>> iterators, long offset, long count, Try.Consumer<? super T,E> elementParser) throws E extends Exception
T
- E
- iterators
- offset
- count
- elementParser
- E
- the eE extends Exception
public static <T,E extends Exception,E2 extends Exception> void parse(Collection<? extends Iterator<? extends T>> iterators, long offset, long count, Try.Consumer<? super T,E> elementParser, Try.Runnable<E2> onComplete) throws E extends Exception, E2 extends Exception
T
- E
- E2
- iterators
- offset
- count
- elementParser
- onComplete
- E
- the eE2
- the e2E extends Exception
public static <T,E extends Exception> void parse(Collection<? extends Iterator<? extends T>> iterators, int readThreadNum, int processThreadNum, int queueSize, Try.Consumer<? super T,E> elementParser) throws E extends Exception
T
- E
- iterators
- readThreadNum
- processThreadNum
- queueSize
- elementParser
- E
- the eE extends Exception
public static <T,E extends Exception,E2 extends Exception> void parse(Collection<? extends Iterator<? extends T>> iterators, int readThreadNum, int processThreadNum, int queueSize, Try.Consumer<? super T,E> elementParser, Try.Runnable<E2> onComplete) throws E extends Exception
T
- E
- E2
- iterators
- readThreadNum
- processThreadNum
- queueSize
- elementParser
- onComplete
- E
- the eE extends Exception
public static <T,E extends Exception> void parse(Collection<? extends Iterator<? extends T>> iterators, long offset, long count, int readThreadNum, int processThreadNum, int queueSize, Try.Consumer<? super T,E> elementParser) throws E extends Exception
T
- E
- iterators
- offset
- count
- readThreadNum
- processThreadNum
- queueSize
- elementParser
- E
- the eE extends Exception
public static <T,E extends Exception,E2 extends Exception> void parse(Collection<? extends Iterator<? extends T>> iterators, long offset, long count, int readThreadNum, int processThreadNum, int queueSize, Try.Consumer<? super T,E> elementParser, Try.Runnable<E2> onComplete) throws E extends Exception, E2 extends Exception
T
- E
- E2
- iterators
- offset
- count
- readThreadNum
- new threads started to parse/process the lines/recordsprocessThreadNum
- new threads started to parse/process the lines/recordsqueueSize
- size of queue to save the processing records/lines loaded from source data. Default size is 1024.elementParser
- onComplete
- E
- the eE2
- the e2E extends Exception
public static <T> Stream<T> splitByCount(int maxCount, int totalSize, boolean smallerFirst, IntBiFunction<T> func)
final int[] a = Array.rangeClosed(1, 7);
splitByCount(5, 7, true, (fromIndex, toIndex) -> N.copyOfRange(a, fromIndex, toIndex)).forEach(Fn.println()); // [[1], [2], [3], [4, 5], [6, 7]]
splitByCount(5, 7, false, (fromIndex, toIndex) -> N.copyOfRange(a, fromIndex, toIndex)).forEach(Fn.println()); // [[1, 2], [3, 4], [5], [6], [7]]
T
- maxCount
- totalSize
- smallerFirst
- func
- Copyright © 2019. All rights reserved.