jdk8, [one step teaching, one step in place]

Posted by Valkrin on Tue, 21 Dec 2021 13:11:02 +0100

public class CollectorsDemo {

    public static void main(String[] args) {

        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("Zhang San", 23));

        studentList.add(new Student("Li Si", 20));

        studentList.add(new Student("Wang Wu", 19));

        studentList.add(new Student("Xiao Ming", 21));

        studentList.add(new Student("Xiao Hong", 24));



        Long collect = studentList.stream().collect(Collectors.counting());

        System.out.println(collect);



		//Equivalent to

        long count = studentList.stream().count();

        System.out.println(count);

    }

} 

Collectors. Internally made in counting, call reducing() to summarize data

 public static <T> Collector<T, ?, Long> counting() {

        return reducing(0L, e -> 1L, Long::sum);

    } 

Get the maximum and minimum values through maxBy() and minBy()

public class CollectorsDemo {

    public static void main(String[] args) {

        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("Zhang San", 23));

        studentList.add(new Student("Li Si", 20));

        studentList.add(new Student("Wang Wu", 19));

        studentList.add(new Student("Xiao Ming", 21));

        studentList.add(new Student("Xiao Hong", 24));



        //Get the oldest student

        Optional<Student> max = studentList.stream().collect(Collectors.maxBy(Comparator.comparing(Student::getAge)));

        System.out.println(max);



        //Get the youngest student

        Optional<Student> min = studentList.stream().collect(Collectors.minBy(Comparator.comparing(Student::getAge)));

        System.out.println(min);

    }

} 

**

Obtain the average value through averagingInt()

public class CollectorsDemo {

    public static void main(String[] args) {

        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("Zhang San", 23));

        studentList.add(new Student("Li Si", 20));

        studentList.add(new Student("Wang Wu", 19));

        studentList.add(new Student("Xiao Ming", 21));

        studentList.add(new Student("Xiao Hong", 24));



        //Method I:

        Double collect = studentList.stream().collect(Collectors.averagingInt(Student::getAge));

        System.out.println(collect);



        //Method 2: extract the age and then calculate the average value

        OptionalDouble average = studentList.stream().mapToDouble(Student::getAge).average();

        System.out.println(average);

    }

} 

**

Summarize data through summingInt()

public class CollectorsDemo {

    public static void main(String[] args) {

        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("Zhang San", 23));

        studentList.add(new Student("Li Si", 20));

        studentList.add(new Student("Wang Wu", 19));

        studentList.add(new Student("Xiao Ming", 21));

        studentList.add(new Student("Xiao Hong", 24));



        IntSummaryStatistics collect = studentList.stream().collect(Collectors.summarizingInt(Student::getAge));

        //total

        System.out.println(collect.getCount());

        //Sum

        System.out.println(collect.getSum());

        //Minimum decimal

        System.out.println(collect.getMin());

        //average

        System.out.println(collect.getAverage());

        //Maximum number

        System.out.println(collect.getMax());

    }

} 

**

Data splicing through joining()

public class CollectorsDemo {

    public static void main(String[] args) {

        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("Zhang San", 23));

        studentList.add(new Student("Li Si", 20));

        studentList.add(new Student("Wang Wu", 19));

        studentList.add(new Student("Xiao Ming", 21));

        studentList.add(new Student("Xiao Hong", 24));



        String collect = studentList.stream().map(Student::getName).collect(Collectors.joining(","));

        System.out.println(collect);

    }

} 

Each mapped value is spliced internally through StringBuilder

public static Collector<CharSequence, ?, String> joining(CharSequence delimiter,

                                                             CharSequence prefix,

                                                             CharSequence suffix) {

        return new CollectorImpl<>(

                () -> new StringJoiner(delimiter, prefix, suffix),

                StringJoiner::add, StringJoiner::merge,

                StringJoiner::toString, CH_NOID);

    } 

grouping

==================================================================

Grouping data through groupBy()

**

Simple grouping

Student information grouped by age

public class CollectorsDemo {

    public static void main(String[] args) {

        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("Zhang San", 23));

        studentList.add(new Student("Li Si", 23));

        studentList.add(new Student("Wang Wu", 19));

        studentList.add(new Student("Xiao Ming", 19));

        studentList.add(new Student("Xiao Hong", 20));



        Map<Integer, List<Student>> collect = studentList.stream().collect(Collectors.groupingBy(Student::getAge));

        System.out.println(collect);

    }

} 

**

result:

{19=[Student{id=null, name='Wang Wu', age=19}, Student{id=null, name='Xiao Ming', age=19}], 20=[Student{id=null, name='Xiao Hong', age=20}], 23=[Student{id=null, name='Zhang San', age=23}, Student{id=null, name='Li Si', age=23}]} 

Multilevel grouping

Collectors.groupingBy provides an overloaded method, which will group the results in the inner layer (the second parameter) and pass them to the outer layer group (the first parameter) as the basis for its continued grouping

Group by age and pass

public class CollectorsDemo {

    public static void main(String[] args) {

        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("Zhang San", 23, true));

        studentList.add(new Student("Li Si", 23, false));

        studentList.add(new Student("Wang Wu", 19, true));

        studentList.add(new Student("Xiao Ming", 19, true));

        studentList.add(new Student("Xiao Hong", 20, true));



        Map<Integer, Map<String, List<Student>>> collect = studentList.stream().collect(Collectors.groupingBy(Student::getAge, Collectors.groupingBy(item -> {

            if (item.isPass()) {

                return "pass";

            } else {

                return "no pass";

            }

        })));

        System.out.println(collect);

    }

} 

Operation results:

{19={pass=[Student{id=null, name='Wang Wu', age=19, pass=true}, Student{id=null, name='Xiao Ming', age=19, pass=true}]}, 20={pass=[Student{id=null, name='Xiao Hong', age=20, pass=true}]}, 23={pass=[Student{id=null, name='Zhang San', age=23, pass=true}], no pass=[Student{id=null, name='Li Si', age=23, pass=false}]}} 

Final content

At the beginning of sharing with you, I said that I was not prepared for the interview. It all depends on my usual accumulation. I really crammed for it temporarily, so that I was very upset myself. (maybe you can get a 40k when you're ready, but only 30k + when you're not ready. Do you understand that feeling)

How to prepare for an interview?

1. Preliminary bedding (technical deposition)

Programmer interview is actually a thorough examination of technology. If your technology is awesome, you are the boss. The requirements of large factories for technology are mainly reflected in five aspects: foundation, principle, in-depth study of source code, breadth and actual combat. Only by combining principle theory with actual combat can we understand the technical points thoroughly.

The following are some information notes I will read, hoping to help you learn Java from shallow to deep, from point to face, and deal with the soul questioning of large factory interviewers

CodeChina open source project: [analysis of Java interview questions of front-line large manufacturers + core summary learning notes + latest explanation Video]

This part of the content is too much. Xiaobian only posted part of the content to show you. Forgive me!

  • Java programmers must read java development core notes (Huashan version)

  • Redis learning notes

  • Learning notes on Java Concurrent Programming

The fourth part divides concurrent programming in detail -- Concurrent Programming + mode + application + principle

  • Java programmers must read the book in-depth understanding of ava virtual machine version 3 (pdf version)

  • Interview questions for large factories -- Notes on data structure and algorithm collection

Others like Spring, SpringBoot, SpringCloud, SpringCloudAlibaba, Dubbo, Zookeeper, Kafka, RocketMQ, RabbitMQ, Netty, MySQL, Docker, K8s, etc. I've sorted them out. I won't show them one by one here.

2. Crazy brush interview questions

The technology is mainly reflected in the accumulation and practicality at ordinary times. You can review it well in two months before the interview, and then you can brush the interview questions. The following interview questions are carefully sorted out by Xiaobian and posted for everyone to see.

① Dachang high frequency 45 pen test questions (IQ questions)

② Interview summary of BAT large factory (screenshot of some contents)

③ Interview summary

3. Modify the resume according to the actual situation

Programmers' resumes must make more efforts, especially to think over some words. For example, the differences between "proficient, familiar and understand" must be distinguished clearly, otherwise they are digging a hole for themselves. Of course not. I can give you my resume for reference. If it's not enough, you can choose from the following resume templates:

I hope you can find a good job in the golden, silver and four job hopping season, but please remember that the technology must be accumulated through work or self-study (or enrolling in class and learning from teachers) through actual combat. Don't cram temporarily.

In addition, you might as well try to talk about your own ideas for the problems you won't encounter in the interview, because some problems don't examine our programming ability, but our logical thinking and expression ability; Finally, we should conduct self-analysis and evaluation, make career planning, constantly explore, and improve our programming ability and abstract thinking ability.

[external chain picture transferring... (IMG ggfcdjdn-163030139521)]

③ Interview summary

[external chain picture transferring... (img-q9q03yhs-163030139521)]

[external chain pictures are being transferred... (img-3dp8dxid-163031639522)]

3. Modify the resume according to the actual situation

Programmers' resumes must make more efforts, especially to think over some words. For example, the differences between "proficient, familiar and understand" must be distinguished clearly, otherwise they are digging a hole for themselves. Of course not. I can give you my resume for reference. If it's not enough, you can choose from the following resume templates:

[external chain pictures are being transferred... (img-8zki8h5h-163031639523)]

I hope you can find a good job in the golden, silver and four job hopping season, but please remember that the technology must be accumulated through work or self-study (or enrolling in class and learning from teachers) through actual combat. Don't cram temporarily.

In addition, you might as well try to talk about your own ideas for the problems you won't encounter in the interview, because some problems don't examine our programming ability, but our logical thinking and expression ability; Finally, we should conduct self-analysis and evaluation, make career planning, constantly explore, and improve our programming ability and abstract thinking ability.

All notes, interview questions and other materials mentioned in the above articles can be shared for free. If necessary, you can Poke here, pack and take away All right.

Topics: Java Big Data Back-end Rust