美文网首页
Stream流的collect转化到集合

Stream流的collect转化到集合

作者: _MrWhite | 来源:发表于2021-02-07 09:50 被阅读0次
    package com.baifan.collecting;
    
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.sql.SQLOutput;
    import java.util.Arrays;
    import java.util.IntSummaryStatistics;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Objects;
    import java.util.Set;
    import java.util.TreeSet;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    /**
     * @author: baifan
     * @date: 2021/2/5
     */
    public class CollectingTest {
    
        public static void main(String[] args) throws Exception {
            Iterator<Integer> iterator = Stream.iterate(0, n -> n + 1).limit(10).iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
    
            Object[] numbers = Stream.iterate(0, n -> n + 1).limit(10).toArray();
            System.out.println("Object array:" + numbers);
    
            try {
                Integer number = (Integer) numbers[0];
                System.out.println("number:" + number);
                System.out.println("The following statement throws an exception:");
                Integer[] numbers2 = (Integer[]) numbers;
            } catch (Exception e) {
                System.out.println(e);
            }
    
            Integer[] numbers3 = Stream.iterate(0, n -> n + 1).limit(10).toArray(Integer[]::new);
            System.out.println("Integer array:" + numbers3);
    
            Set<String> wordset = fileSplitsStream().collect(Collectors.toSet());
            show("wordset", wordset);
    
            TreeSet<String> wordsTreeSet = fileSplitsStream().collect(Collectors.toCollection(TreeSet::new));
            show("wordsTreeSet", wordsTreeSet);
    
            String jionResult = fileSplitsStream().limit(10).collect(Collectors.joining());
            System.out.println("jionResult:" + jionResult);
    
            String jionResult2 = fileSplitsStream().limit(10).collect(Collectors.joining(", "));
            System.out.println("Jion with commas:" + jionResult2);
    
            //这个很强大用来统计超级好!!!!
            IntSummaryStatistics summary = fileSplitsStream().collect(Collectors.summarizingInt(String::length));
            double averageWordLength = summary.getAverage();
            double maxWordLength = summary.getMax();
            System.out.println("Average word length:" + averageWordLength);
            System.out.println("Max word length:" + maxWordLength);
            System.out.println("forEach:");
            fileSplitsStream().limit(10).forEach(System.out::println);
        }
    
        public static Stream<String> fileSplitsStream() throws Exception {
            String contents = new String(Files.readAllBytes(Paths.get("D:/sql/java库.sql")), StandardCharsets.UTF_8);
            List<String> wordList = Arrays.asList(contents.split("\\PL+"));
            Stream<String> words = wordList.stream();
            return words.map(s -> s.replaceAll("\\*", "1"));
        }
    
        public static <T> void show(String label, Set<T> set) {
            System.out.println(label + ":" + set.getClass().getName());
            System.out.println("[" + set.stream().limit(10).map(Objects::toString).collect(Collectors.joining(", ")) + "]");
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Stream流的collect转化到集合

          本文链接:https://www.haomeiwen.com/subject/zzqgtltx.html