标签:转换 ase present ice random arrays map The limit
将ArrayList中的元素按照给定字符拼接起来
public static void main(String[] args) { List<String> list = Arrays.asList("nice","to","meet","you"); System.out.println(list.stream().collect(Collectors.joining("-")).toString()); }
把ArrayList中的字符串全变成大写
public static void main(String[] args) { List<String> list = Arrays.asList("nice","to","meet","you"); // list.stream().map(s -> { // return s.toUpperCase(); // }).collect(Collectors.toList()).forEach(s -> System.out.println(s)); list.stream().map(String::toUpperCase).collect(Collectors.toList()).forEach(System.out::println); }
flatMap()将stream流中的每一个元素转换成一个流再进行操作:
public static void main(String[] args) { Stream<List<Integer>> stream = Stream.of(Arrays.asList(1,3,5,7,9),Arrays.asList(2,4,6,8,10)); stream.flatMap(theList->theList.stream()).map(i->i*i).forEach(System.out::println); }
public static void main(String[] args) { //Supplier<T> s 不接收参数,但是返回一个数据类型 Stream<String> stream = Stream.generate(UUID.randomUUID()::toString); stream.findFirst().ifPresent( System.out::printf); //无限流 // Stream.iterate(1, i -> i+1 ).forEach(System.out::println); //限制一下 Stream.iterate(1, integer -> integer+10).limit(10).forEach(System.out::println); }
public static void main(String[] args) { Stream.iterate(1,item -> item + 2).limit(6).filter(item -> item > 2).map(item -> item * 2).skip(2).limit(2).reduce(Integer::sum).ifPresent(System.out::print); System.out.println(Stream.iterate(1,item -> item +2).limit(6).filter(item -> item >2 ).mapToInt(item -> item*2).skip(2).limit(2).sum()); Stream.iterate(1,item -> item +2).limit(6).filter(item -> item>2 ).mapToInt(item -> item*2).skip(2).limit(2).min().ifPresent(System.out::println); }
标签:转换 ase present ice random arrays map The limit
原文地址:https://www.cnblogs.com/zhvip/p/12840044.html