ACMNexus / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Strange transform, isn't it? #106

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
List<Point> list = Lists.newArrayList(new Point(1,1), new Point(3,1), new
Point(5,1));

Iterable<Point> outList = Iterables.transform(list, new Function<Point,
Point>() {      
@Override public Point apply(Point from) {
    from.y += 5;
    return from;
}});

System.out.println(list);               
System.out.println(outList);
System.out.println(list);
System.out.println(outList);

==== Console output =======
[java.awt.Point[x=1,y=1], java.awt.Point[x=3,y=1], java.awt.Point[x=5,y=1]]
[java.awt.Point[x=1,y=6], java.awt.Point[x=3,y=6], java.awt.Point[x=5,y=6]]
[java.awt.Point[x=1,y=6], java.awt.Point[x=3,y=6], java.awt.Point[x=5,y=6]]
[java.awt.Point[x=1,y=11], java.awt.Point[x=3,y=11], java.awt.Point[x=5,y=11]]
============================
After each next sysout values are changes. I didn't expect that.

Original issue reported on code.google.com by and...@gmail.com on 14 Nov 2008 at 4:15

GoogleCodeExporter commented 8 years ago
The program's doing exactly what you're telling it to. Java does not have the 
concept
of pass-by-value except for primitives, so this is the inevitable result.

This is why mutable value objects are avoided as much as possible nowadays.
Unfortunately things like java.awt.Point survive.

Original comment by kevin...@gmail.com on 14 Nov 2008 at 4:43