Stream/Optional
orElse和orElseGet的区别 orElse传的是一个对象,orElseGet传的是一个函数,orElse的参数即使optional.isPresent为true也会执行
Difference between
Optional.orElse()andOptional.orElseGet()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.
Array转list
int[]转list
int[] nums = {0, -1,0, -1,0, -1,99};
List<Integer> a = Arrays.stream(nums).boxed().collect(Collectors.toList());❌ 不能用Arrays::asList,会映射成List<int[]>
int[] a = {-1, 0, 1, 2, -1, -4};
List<int[]> aList = Arrays.asList(a); // 这里aList只包含一个元素,该元素是int[]类型。int[][]转list
自己写的 找找有没有更简单的写法
int[][] num={ {2}, {3,4}, {6,5,7}, {4, 1, 8, 3} };
List<List<Integer>> resList = Arrays.stream(num).map(li -> IntStream.of(li).boxed().collect(Collectors.toList())).collect(Collectors.toList());输出二维数组
Integer[]转list
Arrays.asList
Arrays.asList Arrays.asList内部实现是return new ArrayList<>(a); 这个ArrayList是 Arrays类内部定义的 private的静态内部类java.util.Arrays.ArrayList,不是java.util.ArrayList。所以只能用List接,不能用ArrayList,会报错。
并且这个list只实现了set方法,add元素时会抛出java.lang.UnsupportedOperationException异常。
✅ 转成正常ArrayList
✅ stream
Integer[][] 转 list
---------
guava新建List
list转hashset
直接 new HashSet<>(words)
https://stackoverflow.com/questions/36000097/big-o-what-is-the-time-complexity-for-this-algorithm
判断有没有重复
toSet retainAll返回true表示有交集,注意retainAll会改变原集合
创建二维数组
可以直接new int[2][3]。。。以前用stream写了个寂寞。
复制二维数组
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”.
给[]和list填充相同元素
Stream.generate 初始化List<Object>
无参构造函数
list初始化
List<Integer>[]初始化
有参构造函数
List<List<Integer>>初始化
基本类型及其包装类型
Arrays.fill 和 Collections.nCopies,最好都只用来初始化基本类型,如果初始化Object,数组的所有元素都指向同一个对象。
Stream
1. toMap
Collectors.toMap(key, value, key重复时对应的(旧value, 新value) -> 选一个值或者算一个值返回)
如果value要一个固定的String值,可以把第二个函数改成 r->""
2. toList
.map(func).toList去某一个属性构成list,这个函数也可以用lamda表达式来自己算值
3. flatmap
flatmap Stream flatMap() in Java with examples
4. findAny orElse isPresent
5. reduce
6. skip limit
7. Stream.iterate
Java8新特性学习-函数式编程(Stream/Function/Optional/Consumer)
Note how their signatures are different:
generatetakes aIntSupplier, 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.
iteratetakes aseedand aIntUnaryOperator, 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.
8. sort
排序
JDK 8 之 Stream sorted() 示例 以及评论
* 对于List排序
* 对于Array排序
9. mapToObj, mapToInt map(Stream VS IntStream)
Stream 包含 IntStream, LongStream, DoubleStream 等
Stream<T> 不能使用primitive type,只能使用Integer这样的包装类型,而IntStream就是基本数据类型int的Stream
IntStream不转换成Integer的话不能直接collect,只能toArray
10. groupingBy
统计String里各个字符个数, t.chars()返回的是IntStream,不用mapToObj的话会报错
为什么`groupingBy`里`Function.identity()`换成`Function::identity`会报错,但是`toMap`里却是`::`这种格式的?
11.summaryStatistics 统计
https://mp.weixin.qq.com/s/84TSGjui1pT4cL1o8vNsTw
IntSummaryStatistics 用于收集统计信息(如count、min、max、sum和average)的状态对象。
示例:得到最大、最小、之和以及平均数。
Last updated
Was this helpful?