假设以下是我们的字符串列表-
List list = Arrays.asList("Football", "Basketball", "Hockey", "Cricket","Fencing");
现在,映射以将字符串添加到每个元素-
List str = list.stream().map(name -> "Sports " + name + " Outdoor") .collect(Collectors.toList());
上面,我们在每个元素中添加了“运动”和“室外”字符串。
以下是Map的示例,并将字符串值添加到Java中的每个元素-
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Demo { public static void main(String[] args) { List list = Arrays.asList("Football", "Basketball", "Hockey", "Cricket","Fencing"); List str = list.stream().map(name -> "Sports " + name + " Outdoor") .collect(Collectors.toList()); System.out.println(str); } }
输出结果
[Sports Football Outdoor, Sports Basketball Outdoor, Sports Hockey Outdoor, Sports Cricket Outdoor, Sports Fencing Outdoor]