The or prefix misleads developers (including myself when I asked the problem) into thinking that it is a short-circuiting operation, because that is what we are used to in boolean conditions. However, it is not, it is just a method name that has or in its prefix, so its arguments will be evaluated, irrespective of whether Optional is carrying a value or not.
For one-liner - collapse all lambdas to method references Arrays.stream(data).map(int[]::clone).toArray(int[][]::new); But please note that for huge arrays native System.arraycopy should be (probably?) faster, unless you parallel() your stream.
创建一个一样大小的二维数组
输出二维数组
把边转成树 ([][]转List<Set>)
找出所有叶子 (stream迭代时获得下标)
Is there a concise way to iterate over a stream with indices in Java 8?
The cleanest way is to start from a stream of indices:
The resulting list contains “Erik” only.
One alternative which looks more familiar when you are used to for loops would be to maintain an ad hoc counter using a mutable object, for example an AtomicInteger:
Note that using the latter method on a parallel stream could break as the items would not necesarily be processed “in order”.
generate takes a IntSupplier, which means that you are supposed to generate ints without being given anything. Example usages include creating a constant stream of the same integer, creating a stream of random integers. Notice how each element in the stream do not depend on the previous element.
iterate takes a seed and a IntUnaryOperator, which means that you are supposed to generate each element based on the previous element. This is useful for creating a inductively defined sequence, for example. In this case, each element is supposed to depend on the previous one.
数组填充,填充基本类型,不要用这个填充Object,会导致一个对象改动其他都改了,和Collections.nCopies一个道理
int[] a = new int[3];
Arrays.fill(a, 3);
源码:
public static void fill(int[] a, int val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}
产生0-9的平方的一维数组
int[] square = IntStream.range(0, 10).map(i -> i * i).toArray();
list填充
// https://stackoverflow.com/questions/5600668/how-can-i-initialize-an-arraylist-with-all-zeroes-in-java
//不要用nCopies这个方法产生非(基本类型+对应包装类型),用generate的方法
List<Integer> list = new ArrayList<Integer>(Collections.nCopies(60, 0));