vendredi 2 janvier 2015

Java 8 Method references called on a local variable


Vote count:

0




I am in the process of learning Java 8 and I came across something that I find a bit strange. Imagine the following snippet:



private MyDaoClass myDao;

public void storeRelationships(Set<Relationship<ClassA, ClassB>> relationships) {

RelationshipTransformer transformer = new RelationshipTransformerImpl();

myDao.createRelationships(
relationships.stream()
.map((input) -> transformer.transformRelationship(input)
).collect(Collectors.toSet());
);
}


Basically, I need to map the input set called relationships to a different type in order to conform to the API of the DAO I'm using; For the conversion, I would like to use an existing RelationshipTransformerImpl class that I instantiate as a local variable.


Now, here's my question: If I was to modify the above code like so:



public void storeRelationships(Set<Relationship<ClassA, ClassB>> relationships) {

RelationshipTransformer transformer = new RelationshipTransformerImpl();

myDao.createRelationships(
relationships.stream()
.map((input) -> transformer.transformRelationship(input)
).collect(Collectors.toSet());
);

transformer = null; //setting the value of an effectively final variable
}


I would obviously get a compiler error, since the local variable "transformer" is no longer "effectively final". However, if I change the lambda so that it uses a method reference:


public void storeRelationships(Set> relationships) {



RelationshipTransformer transformer = new RelationshipTransformerImpl();

myDao.createRelationships(
relationships.stream()
.map(transformer::transformRelationship
).collect(Collectors.toSet());
);

transformer = null; //setting the value of an effectively final variable
}


Then I no longer get a compilation error ! Why does this happen? I thought the two ways to write the lambda expression should be equivalent, but there's clearly something more going on.


Thanks



asked 32 secs ago

Emil D

595






Java 8 Method references called on a local variable

Aucun commentaire:

Enregistrer un commentaire