Example of replacing anonymous inner class by a Lambda expression in Java 8

One  of the facilities that Java 8 offers is the reduction of the numbers of lines of code in some situations. In this example, we will replace an anonymous inner class by a Lamba Expression. Reducing the code used to compare two players to almost one single line.

First of all, we will need a Player object. A player has a name and a score. Please note that we will use the Integer wrapper type instead of the primitive int type, in order to benefit from the compareTo method offered by the Integer object.

package com.tutoref;

public class Player {
 
 private String name;
 private Integer score;
 
 // Default constructor to allow instanciation without parameters
 public Player(){}
 
 // Constructor with parameters
 public Player(String name, int score){
  this.name = name;
  this.score = score;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public Integer getScore() {
  return score;
 }
 
 public void setScore(Integer score) {
  this.score = score;
 }
 
 
 @Override
 public String toString() {
  // To customise the displaying of a player
  return "[name : "+this.name+", score : "+this.score+"]";
 }
}

 

Comparison using an anonymous inner class

The playerComparator inner class is used to tell the program how to compare two players, based on their score.

In this example we create the playerComparator of type Coparator<Player>, after that create and add some players to the ListArray collection, then we use the Collections.sort method ot sort the players based on their score.

package com.tutoref;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class PlayerSort {

 public static void main(String[] args) {
  
  // Create a comparator object
  Comparator<Player> playerComparator = new Comparator<Player> () {
   @Override
   public int compare(Player player1, Player player2) {
    return player1.getScore().compareTo(player2.getScore());
   }
  };
  
  
  // Create a list of players
  List<Player> playerList = new ArrayList<Player>();
  
  // add some players
  playerList.add(new Player("Messi",500));
  playerList.add(new Player("Ronaldo",750));
  playerList.add(new Player("Roberto",900));
  playerList.add(new Player("Albert",450));
  playerList.add(new Player("Michel",750));
  playerList.add(new Player("Mario",750));
  
  // Sort the list based on the score 
  // using the comparator object
  Collections.sort(playerList,playerComparator);
  
  // display the sorted list of players (in ascendant order)
  System.out.println(playerList);
 }

}

 

COMPARISON Using Lambda Expression

This example will replace the  anonymous inner class, reducing it to one line of code.

package com.tutoref;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PlayerSortUsingLambdaExp {

 public static void main(String[] args) {
  
  // Create a list of players
  List<Player> playerList = new ArrayList<Player>();
  
  // add some players
  playerList.add(new Player("Messi",500));
  playerList.add(new Player("Ronaldo",750));
  playerList.add(new Player("Roberto",900));
  playerList.add(new Player("Albert",450));
  playerList.add(new Player("Michel",750));
  playerList.add(new Player("Mario",750));
  
  Collections.sort(playerList, (p1, p2) -> p1.getScore().compareTo(p2.getScore()) );
  
  // display the sorted list of players (ascendent order)
  System.out.println(playerList);
 }

}

The compactor interface is also a Functional Interface, this is why it can be replaced by a lambda expression. You can imagine that we passed the function making the comparison as parameter.