Extract a specific element from the list of objects

Purpose

Extract only specific elements from objects in the list

means

Java 8 Stream

sample

object

NamePlate.java


package stream;

public class NamePlate {
	private Integer id;
	private String name;

	public NamePlate(Integer id, String name) {
		this.id = id;
		this.name = name;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Target processing

StreamSample.java


package stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamSample {
	public static void main(String[] args) {
		List<NamePlate> source = Arrays.asList(
				new NamePlate(1, "Taro")
				, new NamePlate(2, "Jiro")
				, new NamePlate(3, "Hanako")
				, new NamePlate(4, "Jiro")
				, new NamePlate(5, "Jiro")
				, new NamePlate(6, "Taro")
				);

		List<String> nameList = source.stream()
				.map(s -> s.getName())
				//.distinct()
				.collect(Collectors.toList());

		System.out.println(nameList);
	}
}

result

[Taro, Jiro, Hanako, Jiro, Jiro, Taro]

If you remove the commented out "distinct" [Taro, Jiro, Hanako]

reference

The point of understanding lambda expressions and Stream API is "type" Mastering Java 8 Stream

Recommended Posts

Extract a specific element from the list of objects
Extract a specific element from the list of objects
Sort a List of Java objects
[Java] How to extract the file name from the path
Extract a specific element from the list of objects
[XML] Obtaining earthquake information from the Japan Meteorological Agency ①
Find the difference from a multiple of 10
Set the time of LocalDateTime to a specific time
ArrayList and the role of the interface seen from List
Get a list of classes in a Guava specific package
A record of studying the Spring Framework from scratch
Check the dependency of a specific maven artifact in Coursier
From the habit of creating Value Objects for object-oriented understanding
A program that counts the number of words in a List
Aggregate the number of people every 10 years from List <Person>
[Java] Delete the elements of List
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
A memorandum of the FizzBuzz problem
Official logo list of the service
Create a Tokyo subway map from the CSV file of station data.jp
[Java] How to get to the front of a specific string using the String class
[Java] Generate a narrowed list from multiple lists using the Stream API
[Challenge CircleCI from 0] Learn the basics of CircleCI
[Java] Get the day of the specific day of the week
The story of RxJava suffering from NoSuchElementException
Ruby from the perspective of other languages
Extract a part of a string with Ruby
Easily measure the size of Java Objects
How to sort the List of SelectItem
Ability to display a list of products
I want to get a list of only unique character strings by excluding fixed character strings from the file name
Output the sum of each name and its contents from a multiple array
Get a list of Qiita articles for a specific user with Ruby + Qiita API v2
Get the path defined in Controller class of Spring boot as a list
Use Stream # collect to retrieve and list only specific fields from a JavaBean list
[No.004] Corrected the order list screen of the orderer
Extract subqueries from Relation objects assembled with ActiveRecord
Has the content of useBodyEncodingForURI changed from Tomcat8?
Add empty data to the top of the list
Make a margin to the left of the TextField
Measure the size of a folder in Java
A fledgling engineer learned JUnit from the basics
Create Scala Seq from Java, make Scala Seq a Java List
Takes out the HashMap value and returns the result sorted by the value of value as a list.
Extract elements by doing regular expression replacement from a lot of HTML with java
Simple statistical analysis of monetary value from the list searched by Mercari Java development
ArrayList and the role of the interface seen from List
Extract a specific element from the list of objects
[Java] Delete the elements of List
Extract the required information from pom.xml
Choose the first non-empty one from multiple Optional and call that method
[Java] Generate a narrowed list from multiple lists using the Stream API