JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to sort LinkedList using Comparator?


Description:

This example gives you how to sort an LinkedList using Comparator. The LinkedList contains user defined objects. By using Collections.sort() method you can sort the LinkedList. You have to pass Comparator object which contains your sort logic. The example sorts the Empl objects based on highest salary.


Code:
package com.java2novice.linkedlist;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

public class MyLinkedListSort {

	public static void main(String a[]){
		
		LinkedList<Empl> list = new LinkedList<Empl>();
		list.add(new Empl("Ram",3000));
		list.add(new Empl("John",6000));
		list.add(new Empl("Crish",2000));
		list.add(new Empl("Tom",2400));
		Collections.sort(list,new MySalaryComp());
		System.out.println("Sorted list entries: ");
		for(Empl e:list){
			System.out.println(e);
		}
	}
}

class MySalaryComp implements Comparator<Empl>{

	@Override
	public int compare(Empl e1, Empl e2) {
		if(e1.getSalary() < e2.getSalary()){
			return 1;
		} else {
			return -1;
		}
	}
}

class Empl{
	
	private String name;
	private int salary;
	
	public Empl(String n, int s){
		this.name = n;
		this.salary = s;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public String toString(){
		return "Name: "+this.name+"-- Salary: "+this.salary;
	}
}

Output:
Sorted list entries: 
Name: John-- Salary: 6000
Name: Ram-- Salary: 3000
Name: Tom-- Salary: 2400
Name: Crish-- Salary: 2000
<< Previous Program | Next Program >>

List Of All LinkedList Sample Programs:

  1. Basic LinkedList Operations.
  2. How to read all elements in LinkedList by using iterator?
  3. How to copy or clone a LinkedList?
  4. How to add all elements of a list to LinkedList?
  5. How to delete all elements from my LinkedList?
  6. How to find does LinkedList contains all list elements or not?
  7. How to copy LinkedList to array?
  8. How to get sub list from LinkedList?
  9. How to sort LinkedList using Comparator?
  10. How to reverse LinkedList content?
  11. How to shuffle elements in LinkedList?
  12. How to swap two elements in a LinkedList?
  13. How to convert list to csv string format?
  14. How to add element at first position in LinkedList?
  15. How to add element at last position in LinkedList?
  16. How to read first element from LinkedList?
  17. How to read last element from LinkedList?
  18. How to iterate through LinkedList in reverse order?
  19. LinkedList push(), pop() operations examples.
  20. How to remove elements from LinkedList?
Knowledge Centre
What is abstract class or abstract method?
We cannot create instance for an abstract class. We can able to create instance for its subclass only. By specifying abstract keyword just before class, we can make a class as abstract class.

public abstract class MyAbstractClass{

}

Abstract class may or may not contains abstract methods. Abstract method is just method signature, it does not containes any implementation. Its subclass must provide implementation for abstract methods. Abstract methods are looks like as given below:

public abstract int getLength();
Famous Quotations
I don’t know the key to success, but the key to failure is trying to please everybody.
-- Bill Cosby

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.