Examples of Using Comparator in Java 8

We are using a standard Java Priority Queue, but on elements that aren't comparable. We pass a comparator for the queue objects to the constructor.

The class we are using:



public class Animal {
	private String name;
	private double weight;
	
	public Animal(String name, double weight) {
		this.name = name;
		this.weight = weight;
	}
	
	public String getName() {
		return name;
	}
	
	public double getWeight() {
		return weight;
	}
}

The priority queue with different comparators:


import java.util.Comparator;
import java.util.PriorityQueue;


public class PQWithComparator {
	public static void main(String args[]) {
		PriorityQueue<Animal> pq = new PriorityQueue<>(new ComparatorByName());	
		pq.add(new Animal("lion", 400));
		pq.add(new Animal("zebra", 900));
		pq.add(new Animal("mouse", 0.0001));
	
		for (Animal a: pq){
			System.out.println(a.getName());
		}
		
		// equivalent definition with "lambda" expression
		PriorityQueue<Animal> pq1 = new PriorityQueue<>(
				(Animal a1, Animal a2)->a1.getName().compareTo(a2.getName()));	
		
		pq1.add(new Animal("lion", 400));
		pq1.add(new Animal("zebra", 900));
		pq1.add(new Animal("mouse", 0.0001));
	
		for (Animal a: pq1){
			System.out.println(a.getName());
		}
		
		Comparator<Animal> weightComparator = (Animal a1, Animal a2)-> (int) ((a1.getWeight() - a2.getWeight()))*1000;
		PriorityQueue<Animal> pq2 = new PriorityQueue<Animal>(weightComparator);
				
		
		pq2.add(new Animal("lion", 400));
		pq2.add(new Animal("zebra", 900));
		pq2.add(new Animal("mouse", 0.0001));
	
		for (Animal a: pq2){
			System.out.println(a.getName());
		}
		
		PriorityQueue<Animal> pq3 = new PriorityQueue<Animal>(weightComparator.reversed());
						
		pq3.add(new Animal("lion", 400));
		pq3.add(new Animal("zebra", 900));
		pq3.add(new Animal("mouse", 0.0001));
	
		for (Animal a: pq3){
			System.out.println(a.getName());
		}
		
		
	}
}

class ComparatorByName implements Comparator<Animal> {

	@Override
	public int compare(Animal animal1, Animal animal2) {
		return animal1.getName().compareTo(animal2.getName());
	}
	
}

CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.