1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) throws Exception { List<Fruit> fruitsBucket = new ArrayList<>(); fruitsBucket.add(new Fruit("りんご")); fruitsBucket.add(new Fruit("みかん")); fruitsBucket.add(new Fruit("オレンジ")); List<Juice> refrigerator = fruitsBucket.stream() .map(f -> new Juice(f.type)) .collect(Collectors.toList()); System.out.println("fruitsBucket : " + fruitsBucket); System.out.println("refrigerator : " + refrigerator); } } class Fruit { public String type; public Fruit(String type) { this.type = type; } public String toString() { return type; } } class Juice { public String type; public Juice(String type) { this.type = type; } public String toString() { return type + "ジュース"; } } |
1 2 |
fruitsBucket : [りんご, みかん, オレンジ] refrigerator : [りんごジュース, みかんジュース, オレンジジュース] |