代码
public class AnswerApp {
public static void main(String[] args) {
List<String> list = Lists.newArrayList("Answer", "AnswerAIL", "AI");
List<String> result1 = list.stream().map(e -> "Prev-" + e).collect(Collectors.toList());
System.out.println(result1);
List<Entity> result2 = IntStream.range(0, list.size()).mapToObj(i -> new Entity(i, list.get(i))).collect(Collectors.toList());
System.out.println(result2);
}
}
@Data
@AllArgsConstructor
class Entity {
private Integer index;
private String data;
}
程序运行输出
[Prev-Answer, Prev-AnswerAIL, Prev-AI]
[Entity(index=0, data=Answer), Entity(index=1, data=AnswerAIL), Entity(index=2, data=AI)]