Last active
October 25, 2016 20:08
-
-
Save peterszatmary/0047b97a01658d14b0022147f6a48c3b to your computer and use it in GitHub Desktop.
Java 7 and java 8 working with collection comparision. How to use void functions to affect objects. Lombok used for Objects that represents datas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
@NoArgsConstructor | |
@AllArgsConstructor | |
public @Data class Car { | |
private String type; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class Java7Versus8 { | |
public static void main(String[] args) { | |
Java7Versus8 java7Versus8 = new Java7Versus8(); | |
java7Versus8.java7Version(); | |
java7Versus8.java8Version(); | |
} | |
private void java8Version() { | |
System.out.println("Java 8 version : "); | |
List<User> data = getData(); | |
data.stream() | |
.filter(user -> user != null) | |
.forEach(user -> user.setName(user.getName() + "-plus")); | |
data.stream() | |
.filter(user -> user != null) | |
.flatMap(user -> user.getCars().stream()) | |
.filter(car -> car != null) | |
.forEach(car -> car.setType(car.getType() + "-minus")); | |
data.stream() | |
.forEach(System.out::println); | |
} | |
private void java7Version() { | |
System.out.println("Java 7 version : "); | |
List<User> data = getData(); | |
for (User user : data) { | |
if (null == user) { continue; } | |
user.setName(user.getName() + "-plus"); | |
for (Car car : user.getCars()) { | |
if (null == car) { continue; } | |
car.setType(car.getType() + "-minus"); | |
} | |
} | |
for (User user : data) { | |
System.out.println(user); | |
} | |
} | |
private List<User> getData() { | |
List<User> users = new ArrayList<>(); | |
users.add(getUser("Peter")); | |
users.add(getUser("Patrik")); | |
users.add(getUser("Mario")); | |
return users; | |
} | |
private User getUser(String name) { | |
User user = new User(name, null); | |
List<Car> carList = new ArrayList<>(); | |
carList.add(getCar("BMW")); | |
carList.add(getCar("Audi")); | |
carList.add(getCar("Skoda")); | |
user.setCars(carList); | |
return user; | |
} | |
private Car getCar(String name) { | |
return new Car(name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import java.util.List; | |
@NoArgsConstructor | |
@AllArgsConstructor | |
public @Data class User { | |
private String name; | |
private List<Car> cars; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment