MapStruct - Mapping Streams



Using Mapstruct we can create mapping of streams in the same way as we did for collections.

Syntax

@Mapper
public interface UtilityMapper {
   Stream<String> getStream(Stream<Integer> source);
}

Following example demonstrates the same.

Example - Mapping Streams

Open project mapping as updated in Mapping Map chapter in Eclipse.

Update UtilityMapper.java with following code −

UtilityMapper.java

package com.tutorialspoint.mapper;

import java.util.GregorianCalendar;
import java.util.Map;
import java.util.stream.Stream;
import org.mapstruct.MapMapping;
import org.mapstruct.Mapper;

@Mapper
public interface UtilityMapper {
   @MapMapping(valueDateFormat = "dd.MM.yyyy")
   Map<String, String> getMap(Map<Long, GregorianCalendar> source);
   Stream<String> getStream(Stream<Integer> source);
}

Update UtilityMapperTest.java with following code −

UtilityMapperTest.java

package com.tutorialspoint.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import com.tutorialspoint.mapper.UtilityMapper;

public class UtilityMapperTest {
   private UtilityMapper utilityMapper=Mappers.getMapper(UtilityMapper.class);
   
   @Test
   public void testMapMapping() {
      Map<Long, GregorianCalendar> source = new HashMap<>();
      source.put(1L, new GregorianCalendar(2015, 3, 5));

      Map<String, String> target = utilityMapper.getMap(source);
      assertEquals("05.04.2015", target.get("1"));		
   }
   @Test
   public void testGetStream() {
      Stream<Integer> numbers = Arrays.asList(1, 2, 3, 4).stream();

      Stream<String> strings = utilityMapper.getStream(numbers);
      assertEquals(4, strings.count());			
   }
}

Run the following command to test the mappings.

mvn clean test

Output

Once command is successful. Verify the output.

mvn clean test
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.tutorialspoint.test.[1mCarMapperTest[m
[INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.136 s -- in com.tutorialspoint.test.[1mCarMapperTest[m
[INFO] Running com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m
[INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 s -- in com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m
[INFO] Running com.tutorialspoint.test.[1mStudentMapperTest[m
[INFO] [1;32mTests run: [0;1;32m2[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.010 s -- in com.tutorialspoint.test.[1mStudentMapperTest[m
[INFO] Running com.tutorialspoint.test.[1mUtilityMapperTest[m
[INFO] [1;32mTests run: [0;1;32m2[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 s -- in com.tutorialspoint.test.[1mUtilityMapperTest[m
[INFO] 
[INFO] Results:
[INFO] 
[INFO] [1;32mTests run: 6, Failures: 0, Errors: 0, Skipped: 0[m
[INFO] 
[INFO] [1m------------------------------------------------------------------------[m
[INFO] [1;32mBUILD SUCCESS[m
[INFO] [1m------------------------------------------------------------------------[m
[INFO] Total time:  4.113 s
[INFO] Finished at: 2025-09-14T16:14:53+05:30
[INFO] [1m------------------------------------------------------------------------[m
Advertisements