DEV Community

loizenai
loizenai

Posted on

How to use Java 8 Stream Map Examples with a List or Array

https://grokonez.com/java/java-8/how-to-use-java-8-stream-map-examples-with-a-list-or-array

How to use Java 8 Stream Map Examples with a List or Array

[no_toc]
Converting or transforming a List and Array Objects in Java is a common task when programming. In the tutorial, We show how to do the task with lots of Java examples code by 2 approaches:

  • Using Traditional Solution with basic Looping
  • Using a powerful API - Java 8 Stream Map

Now let's do details with examples!

Related posts:

Java Transform a List with Traditional Solution by Looping Examples

Before Java 8, for mapping a List Objects, we use the basic approach with looping technical solution.

Looping Example - Java Transform an Integer List

  • Example: How to double value for each element in a List?

package com.grokonez.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StreamMapExamples {
    public static void main(String[] args) {
        List intLst = Arrays.asList(1, 2, 3, 4);
        
        // How to double value for each element in a List
        // n -> n*2
        
        List newLst = new ArrayList();
        
        for(int i : intLst) {
            newLst.add(i*2);
        }
        
        System.out.println(newLst);
        // [2, 4, 6, 8]
    }
}

Looping Example - Java Transform a String List

  • Example: How to uppercase string value for each element in a String List?

https://grokonez.com/java/java-8/how-to-use-java-8-stream-map-examples-with-a-list-or-array

How to use Java 8 Stream Map Examples with a List or Array

Top comments (0)