corejava第十版day02(去除元音 处理文档 计数 使用Map保留数据)

来源:互联网 发布:win7显示网络连接图标 编辑:程序博客网 时间:2024/06/05 01:05
public static void main(String[] args) throws IOException {        Iterator<Integer> iter = Stream.iterate(0, n->n+1).limit(10).iterator();        while(iter.hasNext()) {            System.out.println(iter.next());        }        Object[] numbers = Stream.iterate(0, n->n+1).limit(10).toArray();        //是Object型数组        System.out.println("numbers:"+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);        }        System.out.println("——————————");        Integer[] numbers3 = Stream.iterate(0, n->n+1).limit(10).toArray(Integer[]::new);        //是整数型数组        System.out.println("Integer array:"+numbers3);        Set<String> noVowelSet = noVowels().collect(Collectors.toSet());        /*Iterator<String> a = noVowelSet.iterator();        while(a.hasNext()) {            System.out.println(a.next());        }*/        show("noVowelSet",noVowelSet);        TreeSet<String> noVowelTreeSet = noVowels().collect(Collectors.toCollection(TreeSet::new));        show("noVowelTreeSet",noVowelTreeSet);        String result = noVowels().limit(10).collect(Collectors.joining());        System.out.println("joining:"+result);        result = noVowels().limit(10).collect(Collectors.joining(","));        System.out.println("joining with commas:"+result);        IntSummaryStatistics summary = noVowels().collect(Collectors.summarizingInt(String::length));        System.out.println(summary);        double averageWordLength = summary.getAverage();        double maxWordLength = summary.getMax();        System.out.println("Average word length:"+averageWordLength);        System.out.println("Max word lenght:"+maxWordLength);        System.out.println("foreach:");        noVowels().limit(10).forEach(System.out::println);    }    public static Stream<String> noVowels() throws IOException{        String contents = new String(Files.readAllBytes(Paths.get("a.txt")),StandardCharsets.UTF_8);        List<String> wordList = Arrays.asList(contents.split("\\PL+"));        Stream<String> words = wordList.stream();        return words.map(s->s.replaceAll("[aeiouAEIOU]", ""));    }    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(Object::toString)                .collect(Collectors.joining(","))+"]");    }
public static void main(String[] args) {        //{1001=Peter, 1002=Paul, 1003=Mary}        Map<Integer,String> idToName = people().collect(Collectors.toMap(Person::getId, Person::getName));        System.out.println("IdToName:"+idToName);        Map<Integer,Person> idToPerson = people().collect(Collectors.toMap(Person::getId, Function.identity()));        //HashMap   {1001=Person [id=1001, name=Peter], 1002=Person [id=1002, name=Paul], 1003=Person [id=1003, name=Mary]}        System.out.println("idToPerson:"+idToPerson.getClass().getName()+"------------------"+idToPerson);        //TreeMap   {1001=Person [id=1001, name=Peter], 1002=Person [id=1002, name=Paul], 1003=Person [id=1003, name=Mary]}        idToPerson = people()                .collect(Collectors.toMap(Person::getId, Function.identity(), (existingValue, newValue) -> {                    throw new IllegalStateException();                }, TreeMap::new));        System.out.println("idToPerson:" + idToPerson.getClass().getName() + idToPerson);        Stream<Locale> locales = Stream.of(Locale.getAvailableLocales());        Map<String, String> languageNames = locales.collect(Collectors.toMap(Locale::getDisplayLanguage,                l -> l.getDisplayLanguage(l), (existingValue, newValue) -> existingValue));        System.out.println("languageNames:" + languageNames);        locales = Stream.of(Locale.getAvailableLocales());        Map<String, Set<String>> countryLanguageSets = locales.collect(Collectors.toMap(Locale::getDisplayCountry,                b -> Collections.singleton(b.getDisplayLanguage()), (a, c) -> {                    Set<String> union = new HashSet<>(a);                    union.addAll(c);                    return union;                }));        System.out.println("countryLanguageSets:"+countryLanguageSets);    }    public static Stream<Person> people(){        return Stream.of(new Person(1001,"Peter"),new Person(1002,"Paul"),new Person(1003,"Mary"),new Person(999,"Mar2y"));    }    public static class Person    {        private int id;        private String name;        public Person() {            super();        }        public Person(int id, String name) {            super();            this.id = id;            this.name = name;        }        public int getId() {            return id;        }        public void setId(int id) {            this.id = id;        }        public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }        @Override        public String toString() {            return "Person [id=" + id + ", name=" + name + "]";        }    }
阅读全文
0 0
原创粉丝点击