Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8, Lambda : replace Anonymous inner class by lambda

I've got a class which contains the following:

List roles = ldapTemplate.search(baseDn, replaceFilter, sc,             new AttributesMapper() {                 public Object mapFromAttributes(Attributes attrs)                         throws NamingException {                     return attrs.get("cn").get();                 }             }); 

IntelliJ tells me to replace the anonymous inner class with a lambda. So I tried:

List roles = ldapTemplate.search(     baseDn, replaceFilter, sc,     (Attributes a)  -> { return a.get("cn").get(); }; ); 

However, I get a compilation error:

Error:(46, 50) java: incompatible types: inference variable T has incompatible bounds     equality constraints: java.lang.String     lower bounds: java.lang.Object 

I can't find the solution to this problem. Do you have any ideas?

like image 773
user3934519 Avatar asked Aug 12 '14 17:08

user3934519


People also ask

Can we replace anonymous class with lambda?

Since the most common use of Anonymous class is to provide a throwaway, stateless implementation of abstract class and interface with a single function, those can be replaced by lambda expressions, but when you have a state field or implementing more than one interface, you cannot use lambdas to replace the anonymous ...

Which is better lambda expression or anonymous inner class?

Lambda expression can be used where a class implements a functional interface to reduce the complexity of the code. An inner anonymous class is more powerful as we can use many methods as we want, whereas lambda expression can only be used where an interface has only a single abstract method.

Does lambda expression eliminates the need of anonymous class?

Lambda expressions are introduced in Java 8. These are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.

Can replace any interface in Java with a lambda expression?

Can every anonymous class be replaced by a lambda expression? The answer is no.


2 Answers

Try this (removing extra semi colon)

List roles = ldapTemplate.search(     baseDn, replaceFilter, sc,     (Attributes a)  -> { return a.get("cn").get(); }             ); 
like image 26
snaikar Avatar answered Oct 08 '22 22:10

snaikar


A simple azure storage Entity Resolver interface and it's implemented method:

EntityResolver<String> orderNumberResolver = new EntityResolver<String>() {     @Override     public String resolve(String partitionKey, String rowKey, Date timeStamp,         HashMap<String, EntityProperty> properties, String etag) {       return properties.get("SomeColumnName").getValueAsString();     }   }; 

Lambda of the above method will be :

 EntityResolver<String> orderNumberResolver = (           partitionKey, rowKey, timeStamp, properties, etag       ) -> properties.get("SomeColumnName").getValueAsString(); 

It's clear from above example that lambda's are smart enough to handle the type of method parameters according to their anonymous inner class thus it makes the implementation of overridden method easy. Hope this will be helpful.

like image 64
Trilok Singh Devda Avatar answered Oct 08 '22 23:10

Trilok Singh Devda