[Java 8 Examples] - Transforming a List




In this post, we will see how to transform a list object into different list using streams from Java 8 and Method references.

Example #1: Convert all names to Upper case names in a List

package java8examples;

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

/**
 * 
 * @author #JavaInspires
 *
 */
public class Java8Example {

	public static void main(String[] args) {

		List<String> names = new ArrayList<String>();
		names.add("john");
		names.add("Steve");
		names.add("Lee");
		names.add("Kent");
		names.add("Robert");
		System.out.println(names);

		List<String> upperCaseNames = names.stream().map(String::toUpperCase).collect(Collectors.toList());
		System.out.println(upperCaseNames);

	}
}

Output

[john, Steve, Lee, Kent, Robert]
[JOHN, STEVE, LEE, KENT, ROBERT]







Post a Comment

Previous Post Next Post