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.

Answer from Trilok Singh Devda on Stack Overflow
🌐
JavaTechOnline
javatechonline.com › home › lambda expression vs anonymous inner class
Lambda Expression Vs Anonymous Inner Class - JavaTechOnline
July 7, 2022 - First, we will implement it using anonymous inner class, then by using Lambda Expression in order to have clear picture on both. public class SortingTest { public static void main(String[] args) { String [] mobiles = {"Samsung", "Lenovo", "Xiaomi", ...
🌐
Blogger
javarevisited.blogspot.com › 2015 › 01 › how-to-use-lambda-expression-in-place-anonymous-class-java8.html
How to use Lambda Expression in Place of Anonymous Class in Java 8 - Example Tutorial
That's all on how to use Lambda expression in place of an anonymous class in Java 8. If you are coding in Java 8 make sure you use lambda expression instead of anonymous class for implementing Comparable, Comparator, Runnable, Callable, ...
🌐
Benchresources
benchresources.net › home › java › java 8 – anonymous inner class v/s lambda expression
Java 8 - Anonymous Inner class v/s Lambda Expression - BenchResources.Net
April 21, 2020 - Let us look at the same example discussed above using lambda expression · Above 1.A anonymous inner class example requires 5-6 lines of custom logic using Comparator interface for sorting
🌐
Baeldung
baeldung.com › home › java › core java › convert anonymous class into lambda in java
Convert Anonymous Class into Lambda in Java | Baeldung
May 29, 2025 - As we can see, we replaced the anonymous class with an arrow between the send() parameter and its body. We can enhance this even more: We can change the lambda statement to a lambda expression by removing the parameter type and the return statement:
🌐
Vertex Academy
vertex-academy.com › tutorials › en › java-8-lambda-comparator-example
Java 8 Lambda: Comparator Example • Vertex Academy
January 26, 2018 - In this article, we will show you how to work with Java 8 Lambda expressions using the Comparator interface in Java. Before Java 8 was released, we had to create an anonymous inner class for the Comparator to sort a collection.
🌐
Baeldung
baeldung.com › home › java › core java › lambda expression vs. anonymous inner class
Lambda Expression vs. Anonymous Inner Class | Baeldung
January 8, 2024 - On the other hand, a lambda expression is a method without a name. We provide the signature of the unimplemented method in the functional interface without a name. We also need not mention the data types of the method arguments. The method resolves at runtime. In an anonymous class, this keyword refers to the anonymous class itself.
Find elsewhere
🌐
Medium
medium.com › @ujjawalr › convert-anonymous-classes-to-a-lambda-expression-in-java-db4f421b0c5c
Convert anonymous classes to a Lambda expression in java | by Ujjawal Rohra | Medium
February 1, 2023 - Below are some of the examples which demonstrate implementations using anonymous class and their lambda equivalent. Comparator<Integer> comparator = new Comparator<Integer>() { public int compare(Integer a, Integer b) { return a.compareTo(b); } };
🌐
Medium
medium.com › @nagarjun_nagesh › lambda-functions-vs-anonymous-inner-classes-in-java-80893c2214d5
Lambda Functions vs. Anonymous Inner Classes in Java | by Nagarjun (Arjun) Nagesh | Medium
February 23, 2024 - Additionally, lambda functions have less overhead in terms of memory allocation compared to anonymous inner classes. Lambda functions can be serialized automatically by the Java runtime if the functional interface they implement is serializable. This is because the lambda expression’s target type is determined at runtime, allowing the lambda to be serialized along with the class that contains it.
🌐
Baeldung
baeldung.com › home › java › java – powerful comparison with lambdas
Java – Powerful Comparison with Lambdas | Baeldung
January 8, 2024 - Read more → · First, let’s ... Comparator used in the sort: new Comparator<Human>() { @Override public int compare(Human h1, Human h2) { return h1.getName().compareTo(h2.getName()); } } This would simply be used to sort ...
🌐
Stack Overflow
stackoverflow.com › questions › 61066052 › java-comparator-interface-nullslast-with-anonymous-inner-class
sorting - Java Comparator Interface NullsLast with anonymous inner class - Stack Overflow
Copyif (o1 == null || o2 == null) { // This results in nulls last. Use != for nulls first. return Boolean.compare(o1 == null, o2 == null); } Now you can proceed with your if (sortGetter.getReturnType().equals(String.class))-type logic (you obviously don't need to invoke sortGetter.invoke(e1) and sortGetter.invoke(e2) again).
Top answer
1 of 1
6

Sonar is suggesting to replace

prop.putAll(temp.entrySet().stream()
    .filter( entry -> !prop.containsKey(entry.getKey()))
    .map( new Function<Entry<Object, Object>, Entry<String, String>>(){ 
        @Override
        public Entry<String, String> apply(Entry<Object, Object> entry) {
            return new Entry<String, String>() {
                @Override
                public String setValue(String value) {
                    return value.trim().toLowerCase();
                }

                @Override
                public String getValue() {
                    return ((String) entry.getValue()).trim().toLowerCase();
                }

                @Override
                public String getKey() {
                    return ((String) entry.getKey()).trim().toLowerCase();
                }
            };
        }
    })
    .collect(Collectors.toMap(Entry::getKey, Entry::getValue)));

(I removed the unnecessary type arguments in the collector)

with

prop.putAll(temp.entrySet().stream()
    .filter( entry -> !prop.containsKey(entry.getKey()))
    .map(entry -> new Entry<String, String>() { 
        @Override
        public String setValue(String value) {
            return value.trim().toLowerCase();
        }

        @Override
        public String getValue() {
            return ((String) entry.getValue()).trim().toLowerCase();
        }

        @Override
        public String getKey() {
            return ((String) entry.getKey()).trim().toLowerCase();
        }
    })
    .collect(Collectors.toMap(Entry::getKey, Entry::getValue)));

which uses the lambda expression as replacement for the anonymous inner class implementing Function, not for the Entry implementation.

Still, it doesn’t make sense to implement the Entry interface manually here, especially not with the actually unwanted setValue method in this contract violating way. You only want an immutable Entry instance, therefore, you can create an instance of an existing class instead:

prop.putAll(temp.entrySet().stream()
    .filter( entry -> !prop.containsKey(entry.getKey()))
    .map(entry -> new AbstractMap.SimpleImmutableEntry<>(
        ((String) entry.getKey()).trim().toLowerCase(),
        ((String) entry.getValue()).trim().toLowerCase()))
    .collect(Collectors.toMap(Entry::getKey, Entry::getValue)));

as a last improvement, you can get rid of the Entry instance entirely, when performing the transformation in the functions passed to the toMap collector:

prop.putAll(temp.entrySet().stream()
    .filter( entry -> !prop.containsKey(entry.getKey()))
    .collect(Collectors.toMap(
        entry -> ((String) entry.getKey())  .trim().toLowerCase(),
        entry -> ((String) entry.getValue()).trim().toLowerCase())));
🌐
Coderanch
coderanch.com › t › 668678 › java › Replacing-Anonymous-Class-equivalent-Lambda
Replacing Anonymous Inner Class with equivalent Lambda Expression Causes Compile-Time Error (Beginning Java forum at Coderanch)
I'm guessing this has something to do with the fact that lambdas aren't implemented as anonymous inner classes. Is there some way the lambda might execute before the constructor returns, but the anonymous inner class's method can't?
🌐
Mkyong
mkyong.com › home › java8 › java 8 lambda : comparator example
Java 8 Lambda : Comparator example | mkyong.com
August 5, 2015 - Comparator<Developer> byName = (Developer o1, Developer o2)->o1.getName().compareTo(o2.getName()); Example to compare the Developer objects using their age. Normally, you use Collections.sort and pass an anonymous Comparator class like this ...
🌐
Blogtech
blogtech.online › home › java › anonymous inner classes and lambda expressions in java
Anonymous Inner Classes and Lambda Expressions in Java - Privacy, Security and technical blog!
June 6, 2019 - When the interface being implemented has only one abstract method, the anonymous inner class can be converted into a special expression called a lambda expression (as of Java 8), which simplifies the code. This post talks about anonymous inner classes and lambda expressions in detail.
🌐
System
system.camp › tutorial › lambdas-anonymous-inner-classes-and-when-to-prefer-what
Lambdas, Anonymous Inner Classes and When to Prefer What - System.Camp
October 6, 2020 - As you can see the above code, creationTimeComparator and companyNameComparator are the Anonymous Inner Classes. They also don’t have any name. The Instance was declared on the fly.