编程知识 cdmana.com

Concise and efficient eclipse collections API

In this paper , You will learn Eclipse Collections Collection provides some useful features . Do you think Java Streams API Sometimes it's not enough ? I think it's worth seeing Eclipse aggregate . Let's discuss the reasons .

Variable or constant

If you like Kotlin collections API, This concept will attract you . You can create variable and immutable sets . Only mutable collections provide a way to add new objects .

Person p1 = new Person("Test1", 20);Person p2 = new Person("Test2", 18);Person p3 = new Person("Test3", 24);MutableList<Person> persons = Lists.mutable.with(p1);persons.add(p2);persons.set(1, p3);

The same method does not apply to, for example ImmutableList And cause compilation errors .

Person p1 = new Person("Test1", 20);Person p2 = new Person("Test2", 18);Person p3 = new Person("Test3", 24);ImmutableList<Person> persons = Lists.immutable.with(p1);persons.add(p2); // ERROR!persons.set(1, p3); // ERROR!

Lazy collections( inert set )

Eclipse Another interesting feature of sets is called lazy sets , It allows you to delay execution until a terminal operation is invoked . You can call asLazy Method to enable lazy API. Let's see how it works . First , We have the following categories . I am here getAge Method to add a log message .

public class Person {    private final String name;    private int age;    public Person(String name, int age) {        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public int getAge() {        System.out.println("Age: " + this.age);        return age;    }    public void setAge(int age) {        this.age = age;    }}

then , Let's create an inert collection . In the first step , We raise everyone's age 2 year . And then we filter 20 People over the age of . Last , We only get the first element from the result stream .

Person p1 = new Person("Test1", 20);Person p2 = new Person("Test2", 18);Person p3 = new Person("Test3", 24);Person p4 = new Person("Test4", 30);Person p5 = new Person("Test5", 35);MutableList<Person> persons = Lists.mutable.with(p1, p2, p3, p4, p5);persons   .asLazy()   .tap(p -> p.setAge(p.getAge() + 2))   .select(p -> p.getAge() > 20)   .getFirst();

What is the output of the above code ? Because we only get the first element from the list , So it only calls... For the first filter object getAge Method .

Age: 20Age: 22

Now? , Let's do the same for non lazy sets .

Person p1 = new Person("Test1", 20);Person p2 = new Person("Test2", 18);Person p3 = new Person("Test3", 24);Person p4 = new Person("Test4", 30);Person p5 = new Person("Test5", 35);MutableList<Person> persons = Lists.mutable.with(p1, p2, p3, p4, p5);persons   .tap(p -> p.setAge(p.getAge() + 2))   .select(p -> p.getAge() > 20)   .getFirst();

This is the result . For each object in the list , We call getAge Methods two , Although only the first object is called in the end .

Age: 22Age: 18Age: 24Age: 30Age: 35Age: 24Age: 20Age: 26Age: 32Age: 37

Collect elements

Use Eclipse aggregate , We can easily collect and filter elements using a single method . Let's consider the following java Flow operation . We have an organized input list . Then we just want to filter one with Test1 The name of the organization , Get all employees from the organization , Then convert it to an employee list .

Employee e1 = new Employee(1, "Test1", "Developer");Employee e2 = new Employee(2, "Test2", "Developer");Employee e3 = new Employee(3, "Test3", "Developer");Employee e4 = new Employee(4, "Test4", "Developer");Organization o1 = new Organization("Test1", List.of(e1, e2));Organization o2 = new Organization("Test2", List.of(e3, e4));List<Organization> organizations = List.of(o1, o2);List<Employee> employees = organizations   .stream()   .filter(o -> o.name().equals("Test1"))   .map(Organization::employees)   .flatMap(List::stream)   .collect(Collectors.toList());

about Eclipse aggregate , You can do the same in two ways .

MutableList<Employee> employees = organizations   .select(o -> o.name().equals("Test1"))   .flatCollect(Organization::employees);

Similarly , If you want to collect employees instead of using Java Streams Flattening , Then proceed as follows .

List<List<Employee>> employees2 = organizations   .stream()   .filter(o -> o.name().equals("Test1"))   .map(Organization::employees)   .collect(Collectors.toList());

On the other hand , about Eclipse aggregate , You can use a single method collectIf To achieve this .

MutableList<List<Employee>> employees2 = organizations   .collectIf(o -> o.name().equals("Test1"), Organization::employees);

Arithmetic operations

Use Eclipse aggregate , We can easily perform some arithmetic operations , Such as calculation or search mix or max Elements . In the following code snippet , We want to calculate the number of developers in a position .

Employee e1 = new Employee(1, "Test1", "Developer");Employee e2 = new Employee(2, "Test2", "Architect");Employee e3 = new Employee(3, "Test3", "Developer");Employee e4 = new Employee(4, "Test4", "Tester");List<Employee> employees = List.of(e1, e2, e3, e4);long c = employees   .stream()   .filter(emp -> emp.position().equals("Developer"))   .count();

about Eclipse aggregate , We can use a single method again .

MutableList<Employee> employees = Lists.mutable.of(e1, e2, e3, e4);int c = employees.count(emp -> emp.position().equals("Developer"));

Similarly ,Java It's also a bit complicated for a stream to find the smallest element in the list . We hope to find id The lowest employee .

Employee r1 = employees   .stream()   .min(Comparator.comparing(Employee::id))   .orElseThrow(NoSuchElementException::new);

As you might guess , We can pass in Eclipse Call a single method on a collection to perform the same operation .

Employee r1 = employees.minBy(Employee::id);

Summary

I only described Eclipse Some of the selected methods provided by the collection . About the whole API Detailed description of , Please refer to its documentation . Another might persuade you to switch to Eclipse The reason for the collection is the use of resources . After the document , It provides a collection 、 Efficient memory implementation of maps and other basic collections . in addition , If you are interested in other things about Java Interested in your article , You can read Java8 After the new developer friendly features .

Official website address :https://github.com/eclipse/eclipse-collections

版权声明
本文为[Old K's Java blog]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002145756175m.html

Scroll to Top