扫码一下
查看教程更方便
使用 Mapstruct 我们可以像创建集合一样创建流映射。
@Mapper
public interface UtilityMapper {
Stream<String> getStream(Stream<Integer> source);
}
打开映射 Map 章节中更新的项目映射。
使用以下代码更新 UtilityMapper.java
UtilityMapper.java
package com.jiyik.mapper; import org.mapstruct.MapMapping; import org.mapstruct.Mapper; import java.util.GregorianCalendar; import java.util.Map; import java.util.stream.Stream; @Mapper public interface UtilityMapper { @MapMapping(valueDateFormat = "dd.MM.yyyy") Map<String, String> getMap(Map<Long, GregorianCalendar> source); Stream<String> getStream(Stream<Integer> source); }
使用以下代码更新 UtilityMapperTest.java
UtilityMapperTest.java
import com.jiyik.mapper.UtilityMapper; import org.junit.Test; import org.mapstruct.factory.Mappers; import java.util.Arrays; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; import java.util.stream.Stream; import static org.junit.Assert.assertEquals; 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()); } }
运行以下命令来测试映射。
$ mvn clean test
执行结果如下