Java 8 Stream的一些用法,持續更新中...

//int數組轉set集合
//1.轉換成stream,2.包裝,3.stream轉Set
Set<Integer> set= Arrays.stream(array).boxed().collect(Collectors.toSet());

//錯誤舉例,array元素需爲包裝類
Set<Integer> set=new HashSet<>(Arrays.asList(array));
//List<Integer> 轉 int[]
//1.list轉換成stream,2.轉換爲int流,3.轉換爲int[].
int[] array=list.stream().mapToInt(Integer::valueOf).toArray();
//String[]優雅地轉int[]
//String[]->stream->integer stream->int[]
int[] ints=Arrays.stream(strings).mapToInt(Integer::valueOf).toArray();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章