java8新特性-Stream操作集合中的數據


1.類型轉換

(1)其他類型轉換成Stream對象

public class App {
    public static void main(String[] args) {
        //1.批量數據 --> Stream對象
        //多個數據
        Stream stream1 = Stream.of("admin", "tom", "mike");
        //數組
        String[] array = new String[]{"xuexi", "biyao"};
        Stream stream2 = Arrays.stream(array);
        //列表
        List<String> list = new ArrayList<>();
        list.add("wudang");
        list.add("emei");
        list.add("mingjiao");
        Stream stream3 = list.stream();
        //集合
        Set<String> set = new HashSet<>();
        set.add("shaolin");
        set.add("kongtong");
        Stream stream4 = set.stream();
        //map
        Map<String, Object> map = new HashMap<>();
        map.put("張三", 11);
        map.put("李四", 15);
        map.put("王五", 16);
        Stream stream5 = map.entrySet().stream();

        //2.Stream對象對於基本數據類型的功能封裝
        // int/long/double
        IntStream.of(new int[]{10, 20, 30}).forEach(System.out::println);
        IntStream.range(1, 5).forEach(System.out::println);
        IntStream.rangeClosed(1, 5).forEach(System.out::println);
    }
}

(2)Stream對象轉換成其他對象

//3.Stream轉換成指定數據對象
//數組
Object[] object = stream1.toArray(String[]::new);
//字符串
String s = stream2.collect(Collectors.joining()).toString();
System.out.println(s);
//列表
List<String> strings = (List<String>) stream3.collect(Collectors.toList());
System.out.println(strings);
//集合
Set<String> set1 = (Set<String>) stream4.collect(Collectors.toSet());
System.out.println(set1);
//map
Map<String, String> map1 = (Map<String, String>) stream5.collect(Collectors.toMap(x -> x, y -> y));
System.out.println(map1);

2.Stream對象常見API操作

        //4.Stream中常見的API操作
        List<String> accountList = new ArrayList<>();
        accountList.add("songjiang");
        accountList.add("wuyong");
        accountList.add("lujunyi");
        accountList.add("linchong");
        //map()中間操作,map()方法接收一個Function接口
        accountList = accountList.stream().map(x -> "梁山好漢:" + x).collect(Collectors.toList());
        //添加過濾條件,過濾符合條件的用戶 filter()
        accountList = accountList.stream().filter(x -> x.length() > 7).collect(Collectors.toList());
        // forEach 增強型循環
        accountList.forEach(x -> System.out.println("forEach:" + x));
        // peek() 迭代數據完成數據的依次處理過程
        accountList.stream().peek(x -> System.out.println("peek 1:" + x))
                .peek(x -> System.out.println("peek 2 :" + x)).forEach(System.out::println);
        accountList.forEach(System.out::println);
        //Stream中對於數字的操作
        List<Integer> intList = new ArrayList<>();
        intList.add(20);
        intList.add(14);
        intList.add(11);
        intList.add(12);
        intList.add(10);
        intList.add(18);
        intList.add(19);
        intList.add(12);
        //skip()中間操作,有狀態,跳過部分數據
        intList.stream().skip(3).forEach(System.out::println);
        //limit()限制輸出的數量
        intList.stream().skip(3).limit(3).forEach(System.out::println);
        System.out.println("-------------------分割線-----------------------");
        //distinct() 中間操作,有狀態,剔除重復數據
        intList.stream().distinct().forEach(System.out::println);
        System.out.println("-------------------分割線-----------------------");
        //sorted() 中間操作,有狀態,排序

        //max() 獲取最大操作
        //min() 獲取最小值
        //reduce() 合並處理數據
        Optional optional = intList.stream().max((x, y) -> x - y);
        System.out.println(optional.get());

        Optional optional1 = intList.stream().reduce((sum, x) -> sum + x);
        System.out.println(optional1.get());

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM