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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值