Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Rectangle {
- private int bLength;
- private int aLength;
- private int number;
- private String color;
- private int area;
- public Rectangle(int aLength, int bLength, int number, String color) {
- // TODO: add validations here (non-negative, etc.)
- this.aLength = aLength;
- this.bLength = bLength;
- this.number = number;
- this.color = Objects.requireNonNull(color);
- this.area = -1;
- }
- public int getbLength() {
- return bLength;
- }
- public int getaLength() {
- return aLength;
- }
- public int getNumber() {
- return number;
- }
- public String getColor() {
- return color;
- }
- public int getArea() {
- if (area != -1) {
- area = aLength * bLength;
- }
- return area;
- }
- }
- public class App {
- public static void main(String[] args) {
- Map<String, Rectangle> rectangles = Map.of(
- "one", new Rectangle(10, 20, 0, "green"),
- "two", new Rectangle(15, 25, 1, "red"),
- "three", new Rectangle(20, 30, 2, "yellow"),
- "four", new Rectangle(25, 35, 3, "red"),
- "five", new Rectangle(30, 40, 4, "white"),
- "six", new Rectangle(35, 45, 5, "red")
- );
- var modifiedRectangles = rectangles.values().stream()
- .filter(r -> isRed(r))
- .map(r -> withChangedAValue(r))
- .collect(Collectors.toList());
- }
- private static boolean isRed(Rectangle rectangle) {
- return rectangle.getColor().equalsIgnoreCase("red");
- }
- private static Rectangle withChangedAValue(Rectangle r) {
- // do the needed change here
- return new Rectangle(r.getaLength() + 100, r.getbLength(), r.getNumber(), r.getColor())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement