Guide to use the UNDERSCORE framework in the Java class library
The UNDERSCORE framework is a Java class library, which aims to simplify the operation of collection and array.It provides developers with a set of simple and powerful tools that can process data easier.This article will introduce the use guide for the UNDERSCORE framework and provide relevant Java code examples.
1. Introduce the UNDERSCORE framework
First, we need to introduce the Underscore framework into our Java project.You can add the following dependencies in the project construction tool (such as Maven or Gradle):
Maven:
<dependency>
<groupId>com.github.javadev</groupId>
<artifactId>underscore</artifactId>
<version>1.59</version>
</dependency>
Gradle:
groovy
implementation 'com.github.javadev:underscore:1.59'
2. Basic features of using the UNDERSCORE framework
After adding the dependency item of the Underscore framework, we can start using the various functions it provided.
2.1 Early traversal and filtering
The UNDERSCORE framework is used to call a series of methods by calling a series of methods.For example, we can use the `_.each () method to traverse a collection:
import com.github.underscore.Underscore;
public class Main {
public static void main(String[] args) {
Underscore.asList(1, 2, 3, 4, 5)
.each((item, index) -> System.out.println(item));
}
}
The above code will have each element in the output collection.
We can also use the `_.filter ()` method to screen the elements that meet the conditions through the specified conditions:
import com.github.underscore.Underscore;
public class Main {
public static void main(String[] args) {
Underscore.asList(1, 2, 3, 4, 5)
.filter(item -> item % 2 == 0)
.each((item, index) -> System.out.println(item));
}
}
The above code will be all the even numbers in the output collection.
2.2 The conversion and mapping of the episodes
The UNDERSCORE framework also provides a series of methods that can convert and mappore the collection.For example, we can use the `_.map ()` method to convert the collection into another collection:
import com.github.underscore.Underscore;
public class Main {
public static void main(String[] args) {
Underscore.asList(1, 2, 3, 4, 5)
.map(item -> item * 2)
.each((item, index) -> System.out.println(item));
}
}
The above code will twice the elements in the output set.
2.3 Jet aggregate operation
The UNDERSCORE framework also provides some aggregate operations, such as `_Reduce ()` and `_.max ()` and so on.For example, we can use the `_ReDuce ()` method to accumulate an element of a collection:
import com.github.underscore.Underscore;
public class Main {
public static void main(String[] args) {
Integer sum = Underscore.asList(1, 2, 3, 4, 5)
.reduce(0, (memo, item) -> memo + item);
System.out.println(sum);
}
}
The above code will calculate the sum of all elements in the collection.
3. Summary
In this article, we introduced the use guidelines for the UNDERSCORE framework and provided some Java code examples.By introducing the UNDERSCORE framework, we can easily handle the operation of the collection and array to improve the development efficiency.