Home > Java > javaTutorial > Stream API in Java 8: How to map collection to int type using mapToInt() method

Stream API in Java 8: How to map collection to int type using mapToInt() method

王林
Release: 2023-07-31 21:06:18
Original
4170 people have browsed it

Stream API in Java 8: How to use mapToInt() method to map a collection to int type

Introduction:
Java 8 introduced the Stream API to provide developers with a more concise and efficient Collection operation mode. One of the commonly used methods is mapToInt(), which maps the elements in the collection to type int and returns an IntStream. This article will introduce how to use the mapToInt() method and provide some usage examples.

1. Introduction to mapToInt() method

mapToInt() is a method provided in the Stream API, which can be used to map elements in a collection to int type. The declaration of this method is as follows:

IntStream mapToInt(ToIntFunction mapper)

Among them, ToIntFunction is a function interface, representing a function with input type T and output type int. . When using the mapToInt() method, we need to pass in a ToIntFunction implementation to convert the elements in the collection to int.

2. Usage Examples

The following are some examples of using the mapToInt() method:

Example 1: Map the length of elements in a string collection to the int type

List<String> list = Arrays.asList("Apple", "Banana", "Orange", "Grapes");
IntStream intStream = list.stream().mapToInt(String::length);
intStream.forEach(System.out::println);
Copy after login

In this example, we first create a string collection list and convert it to a Stream using the stream() method. Then, use the mapToInt() method to map the length of each element in the collection to type int and return an IntStream. Finally, we output each mapped int value through the forEach method.

Example 2: Mapping an attribute in the object collection to int type

Suppose we have a Person class, which has an age attribute. We can map the age attribute of each person in the Person object collection to the int type through the mapToInt() method.

class Person {
    private int age;
    
    public Person(int age) {
        this.age = age;
    }
    
    public int getAge() {
        return age;
    }
}

List<Person> list = Arrays.asList(
        new Person(20),
        new Person(30),
        new Person(40)
);

IntStream intStream = list.stream().mapToInt(Person::getAge);
intStream.forEach(System.out::println);
Copy after login

In this example, we first create a Person class and define an age attribute and a getAge() method in the class. Then, a collection list of Person objects is created and converted into a Stream through the stream() method. Next, use the mapToInt() method to map the age attribute of each Person object in the collection to the int type and return an IntStream. Finally, we output each mapped int value through the forEach method.

3. Summary

This article introduces the mapToInt() method of the Stream API in Java 8. This method can map the elements in the collection to the int type and return an IntStream. We demonstrated how to use the mapToInt() method through examples, including mapping the length of elements in the string collection to the int type and mapping a property in the object collection to the int type.

Using the Stream API and mapToInt() method, we can perform collection operations more concisely and efficiently in code. I hope this article is helpful for you to understand and use the Stream API in Java 8.

Reference materials:

  • Official documentation of Stream API in Java 8: https://docs.oracle.com/javase/8/docs/api/java/util/stream /package-summary.html

The above is the detailed content of Stream API in Java 8: How to map collection to int type using mapToInt() method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template