Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to use java 8 comparator

public class Apple implements Comparable<Apple> {
    private int id;
    private Color color;
    private int weight;
    //constructor
    public Apple(int id, Color color, int weight) {
        this.id = id;
        this.color = color;
        this.weight = weight;
    }  
   // Here we implement the compareTo() method of the Comparable interface.    
    @Override
    public int compareTo(Apple apple) {
        return this.getColor().compareTo(apple.getColor());
    }
    // getters / setters and toString()
    //
    //
    enum Color {
        RED, GREEN
    }
}
Comment

How to use java 8 comparator

import static java.util.Comparator.comparing;

public class Main {
    public static void main(String[] args) {

        List<Apple> applesList = getApples();
        
      1. //Using comparator with a custom class that implements 
       //the Comparator Interface.  
      
        System.out.println("
//Using comparator with a custom class ");
        applesList.sort(new ColorComparator()
                  .reversed());
        applesList.forEach(System.out::println);

        
       2.  //Using the comparator in a functional style to compare 
        // apples based on a single attribute, the color

        System.out.println("
Comparing apples based on the color ");
        applesList.sort(comparing(Apple::getColor)
                  .reversed());
        applesList.forEach(System.out::println);

       3.  //Using the comparator in a functional style to compare 
        // apples based on multiple attributes, the color and the weight.

        System.out.println("
Comparing apples based on the color and the weight");
        applesList.sort(comparing(Apple::getColor)
                  .thenComparing(Apple::getWeight)
                  .reversed());
        applesList.forEach(System.out::println);
    }
    
    private static List<Apple> getApples() {
        List<Apple> apples = new ArrayList<>();
        apples.add(new Apple(1, Apple.Color.RED, 35));
        apples.add(new Apple(2, Apple.Color.GREEN, 23));
        apples.add(new Apple(3, Apple.Color.GREEN, 27));
        apples.add(new Apple(4, Apple.Color.RED, 30));
        apples.add(new Apple(5, Apple.Color.GREEN, 35));
        apples.add(new Apple(6, Apple.Color.RED, 23));
        return apples;
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java add element to existing array 
Java :: java clear string buffer 
Java :: richest customer wealth 
Java :: add custom font to android paint object 
Java :: spring boot onetomany relationsion 
Java :: spring boot get request body 
Java :: equals ignore case java 
Java :: in java how to compare two strings 
Java :: run java from terminal 
Java :: the main of java 
Java :: basic java programs 
Java :: sort a list with custom comparator java 
Java :: java actionevent 
Java :: Java Looping Through Array Elements 
Java :: reverse number java 
Java :: print 1 to 10 using thread in java 
Java :: how to get elements of a list in java 
Java :: binary search java 
Java :: comparacion de strings java 
Java :: Random Float in java 
Java :: mockito method called 
Java :: java lambda expressions 
Java :: java max value between two numbers 
Java :: Float to bytes java 
Java :: FlutterSound.java uses or overrides a deprecated API. 
Java :: bfs java 
Java :: jdbc dependency 
Java :: how to compute age from local date 
Java :: hasNext for scanner class java 
Java :: Stop gninnipS My sdroW! javascrip codewars 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =