I defined a map and filled it with 5 objects of the type Rectangle
. Each Rectangle-object has the attributes rectangleId
, aLength
, bLength
and color
.
I want to use the java stream api to stream the given map into the new map. While streaming, the value of bLength
shall be increased by 100 for all rectangles that are red
. Below is what I got so far. I can't seem to figure out how to change the value of bLength
.
public class Main {
public static void main(String[] args){
Rectangle r1 = new Rectangle(800, 100,200,"green");
Rectangle r2 = new Rectangle(900, 200,300,"red");
Rectangle r3 = new Rectangle(1000, 300,400,"yellow");
Rectangle r4 = new Rectangle(1100, 400,500,"blue");
Rectangle r5 = new Rectangle(1200, 500,600,"orange");
TreeMap<Integer, Rectangle> myMap = new TreeMap<>();
myMap.put(r1.rectangleId, r1);
myMap.put(r2.rectangleId, r2);
myMap.put(r3.rectangleId, r3);
myMap.put(r4.rectangleId, r4);
myMap.put(r5.rectangleId, r5);
Map<Integer, Rectangle> myMapNew = myMap.entrySet().stream()
.filter(r -> r.getValue().color == "red")
.collect(Collectors.toMap(r -> r.getKey(), r -> r.getValue()));
}
public class Rectangle {
public int rectangleId;
public int aLength;
public int bLength;
public String color;
public Rectangle(int rectangleId, int aLength, int bLength, String color){
this.rectangleId = rectangleId;
this.aLength = aLength;
this.bLength = bLength;
this.color = color;
}
}