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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| * @author vincent */ public class LambdaTest { @Data private static class Student { private Integer id; private String name; private Integer age; private Integer interestType; private List<Hobby> hobbies; private List<Fancy> fancies; private Subject subject;
private Student(Integer id, String name, Integer age, Integer interestType, List<Hobby> hobbies, List<Fancy> fancies, Subject subject) { this.id = id; this.name = name; this.age = age; this.interestType = interestType; this.hobbies = hobbies; this.fancies = fancies; this.subject = subject; }
}
enum InterestType { HOBBY(1, "兴趣"), FANCY(2, "喜好"); private Integer code; private String message;
InterestType(Integer code, String message) { this.code = code; this.message = message; }
public Integer getCode() { return code; }
public static InterestType getInterestTypeByCode(int code) { return Arrays.stream(InterestType.values()).filter(interestType -> interestType.getCode() == code).findFirst().orElse(null); }
}
@Data private static class Hobby { private String basketball; private String running; private String drinking;
private Hobby(String basketball, String running, String drinking) { this.basketball = basketball; this.running = running; this.drinking = drinking; } }
@Data private static class Fancy { private String dance; private String takePhotos; private String meetGirls;
private Fancy(String dance, String takePhotos, String meetGirls) { this.dance = dance; this.takePhotos = takePhotos; this.meetGirls = meetGirls; } }
@Data private static class Subject { private String english; private String chinese; private String mathematics;
private Subject(String english, String chinese, String mathematics) { this.english = english; this.chinese = chinese; this.mathematics = mathematics; } }
private List<Student> getStudent() { List<Student> list = Lists.newArrayList(); list.add(new Student(100, "小明", 10, 1, Lists.newArrayList( new Hobby("篮球", "跑步", "喝酒"), new Hobby("篮球_1", "跑步_1", "喝酒_1"), new Hobby("篮球_2", "跑步_2", "喝酒_2"), new Hobby("篮球_3", "跑步_3", "喝酒_3") ), Lists.newArrayList( new Fancy("街舞", "摄影", "泡妞"), new Fancy("街舞_1", "摄影_1", "泡妞_1"), new Fancy("街舞_2", "摄影_2", "泡妞_2"), new Fancy("街舞_3", "摄影_3", "泡妞_3") ), new Subject("英语", "语文", "数学") )); list.add(new Student(200, "小红", 10, 2, Lists.newArrayList( new Hobby("篮球", "跑步", "喝酒"), new Hobby("篮球_1", "跑步_1", "喝酒_1"), new Hobby("篮球_2", "跑步_2", "喝酒_2"), new Hobby("篮球_3", "跑步_3", "喝酒_3") ), Lists.newArrayList( new Fancy("街舞", "摄影", "泡妞"), new Fancy("街舞_1", "摄影_1", "泡妞_1"), new Fancy("街舞_2", "摄影_2", "泡妞_2"), new Fancy("街舞_3", "摄影_3", "泡妞_3") ), new Subject("英语", "语文", "数学") )); return list; }
@Data private static class Person { private Integer pid; private String pname; private Integer page; private String interest; private String subject; }
private final static BiConsumer<Person, Student> HOBBY = (person, student) -> { Optional.ofNullable(student.getHobbies()) .flatMap(hobbies -> hobbies.stream().findFirst()) .ifPresent(hobby -> person.setInterest(hobby.getDrinking())); Optional.ofNullable(student.subject).ifPresent(subject -> person.setSubject(subject.getEnglish())); };
private final static BiConsumer<Person, Student> FANCY = (person, student) -> { Optional.ofNullable(student.getFancies()) .flatMap(fancies -> fancies.stream().findFirst()) .ifPresent(fancy -> person.setInterest(fancy.getDance())); Optional.ofNullable(student.subject).ifPresent(subject -> person.setSubject(subject.getMathematics())); };
private final static ImmutableMap<InterestType, BiConsumer<Person, Student>> OF = ImmutableMap.of( InterestType.HOBBY, HOBBY, InterestType.FANCY, FANCY );
* BiConsumer<T, U> 实例 */ @Test public void t() { List<Student> studentList = getStudent(); List<Object> collect = studentList.stream().map(student -> { Person person = new Person(); Optional.ofNullable(student).ifPresent(stu -> { person.setPid(stu.getId()); person.setPname(stu.getName()); person.setPage(stu.getAge());
Integer interestType = student.getInterestType(); InterestType interestTypeByCode = InterestType.getInterestTypeByCode(interestType); person.setInterest(interestTypeByCode.message);
OF.get(interestTypeByCode).accept(person, student); }); return person; }).collect(Collectors.toList());
System.out.println(collect); } }
|