码迷,mamicode.com
首页 > 编程语言 > 详细

java之stream(jdk8)

时间:2016-05-19 21:11:22      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

一、例子

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by root on 16-5-19.
 */
public class StreamTest {

    /**
     * 集合中不小于50的数输出出来[1, 20, 63, 58, 185, 60, 59, 20]
     */
    private static void fun1(){
        int[] a = {1, 20, 63, 58, 185, 60, 59, 20};
        Arrays.stream(a).filter(x -> (x >=50)).forEach(System.out::println);
    }

    /**
     *利用stream自身属性判断下列集合是否全部大于50,[1, 20, 63, 58, 185, 60, 59, 20]
     */
    private static boolean fun2(){
        int[] a = {1, 20, 63, 58, 185, 60, 59, 20};
        return Arrays.stream(a).allMatch(p -> p > 50);
    }

    /**
     * 输出下列两个集合中的存在的相同字符串,[“qwer”,”asdf”,”zxcv”,”fgh”,”jum”,”rfv”],[“edc”,”zse”,”asdf”,”zxcv”,”yhnj”]
     */
    private static void fun3(){
        String[] s1 = {"qwer","asdf","zxcv","fgh","jum","rfv"};
        String[] s2 = {"edc","zse","asdf","zxcv","yhnj"};
        Arrays.stream(s1).forEach(x -> {
            Arrays.stream(s2).filter(y -> y.equals(x)).forEach(System.out::println);
        });
    }

    /**
     * 将下列集合中的所有数字乘以10,并得到新集合[1, 20, 63, 58, 185, 60, 59, 20]
     */
    private static void fun4(){
        Integer[] nums = {1, 20, 63, 58, 185, 60, 59, 20};
        Integer[] num = Arrays.stream(nums).map(n -> n * 10).toArray(Integer[]::new);
        Arrays.stream(num).forEach(System.out::println);
    }

    /**
     * 取出下列集合中的第3到7个值,加上5,得到新的集合[1, 20, 63, 58, 185, 60, 59, 20]
     */
    private static void fun5(){
        Integer[] nums = {1, 20, 63, 58, 185, 60, 59, 20};
        Integer[] num = Arrays.stream(nums).skip(2).limit(5).map(x -> x+5).toArray(Integer[]::new);
        Arrays.stream(num).forEach(System.out::println);
    }

    /**
     * 输出下列集合的最小值[1, 20, 63, 58, 185, 60, 59, 20]
     */
    private static void fun6(){
        int[] nums = {1, 20, 63, 58, 185, 60, 59, 20};
        System.out.println(Arrays.stream(nums).min().getAsInt());
    }

    /**
     * 对下列集合进行从小到大排序[1, 20, 63, 58, 185, 60, 59, 20]
     */
    private static void fun7(){
        int[] nums = {1, 20, 63, 58, 185, 60, 59, 20};
        Arrays.stream(nums).sorted().forEach(System.out::println);
    }

    /**
     * 集合合并:["qwe","dfg","sss"],["qwe","dfg","sss"],["qwe","dfg","sss"]
     * 合并为["qwe","dfg","sss","qwe","dfg","sss","qwe","dfg","sss"]
     */
    private static void fun8(){
        Stream<List<String>> inputStream = Stream.of(
                Arrays.asList("qwe", "dfg", "sss"),
                Arrays.asList("rty", "ghkj", "fghh"),
                Arrays.asList("gtb", "ssd", "dg")
        );
        Stream<String> outputStream = inputStream.flatMap(p -> p.stream());
        List<String> str = outputStream.collect(Collectors.toList());
        System.out.println(str);
    }

    /**
     * 取出下列集合中大于50的,并得到新的集合[1, 20, 63, 58, 185, 60, 59, 20]
     */
    private static void fun9(){
        Integer[] nums = {1, 20, 63, 58, 185, 60, 59, 20};
        Integer[] num = Arrays.stream(nums).filter(p -> p > 50).toArray(Integer[]::new);
        Arrays.stream(num).forEach(System.out::println);
    }

    /**
     * 将下列名字转换为大写,再排序输出("Fred Edwards", "Anna Cox", "Deborah Patterson", "Ruth Torres", "Shawn Powell")
     */
    private static void fun10(){
        String[] str = {"Fred Edwards", "Anna Cox", "Deborah Patterson", "Ruth Torres", "Shawn Powell"};
        Arrays.stream(str).map(p -> p.toUpperCase()).sorted().forEach(System.out::println);
    }

    public static void main(String[] args) {

        fun8();
    }

}

 

java之stream(jdk8)

标签:

原文地址:http://www.cnblogs.com/sunjf/p/java_stream.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!