Java 8 Lambda:comparator Example __java

Source: Internet
Author: User

In the following example, we'll show you how to use the Java 8 LAMBDA expression to write a comparator that sorts the list collection.

1. Classic Comparator example.

comparator<developer> byname = new comparator<developer> () {
	@Override public
	int Compare ( Developer O1, Developer O2) {return
		o1.getname (). CompareTo (O2.getname ());

2. LAMBDA expression equivalent.

Comparator<developer> byname =
	(Developer O1, Developer O2)->o1.getname (). CompareTo (O2.getname ());
1. Do not use the LAMBDA to achieve sorting The   example demonstrates the ability to sort the age property with a Developer   object. Normally, you would use Collections.sort to implement the anonymous comparator  comparer, as follows: Testsorting.java
Package com.mkyong.java8;
Import Java.math.BigDecimal;
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Comparator;

Import java.util.List; 

		public class Testsorting {public static void main (string[] args) {list<developer> Listdevs = Getdevelopers ();
		System.out.println ("before Sort");
		for (Developer Developer:listdevs) {System.out.println (Developer); //sort by Age Collections.sort (Listdevs, New comparator<developer> () {@Override public int compare (Dev
			Eloper O1, Developer O2) {return o1.getage ()-o2.getage ();

		}
		});
		System.out.println ("after Sort");
		for (Developer Developer:listdevs) {System.out.println (Developer); private static list<developer> Getdevelopers () {list<developer> result = new Arraylist<develope

		R> ();
		Result.add (New Developer ("Mkyong", New BigDecimal ("70000"), 33);
		Result.add (New Developer ("Alvin", New BigDecimal ("80000"), 20); Result.add (New Developer ("Jason", New BigDecimal ("100000"), 10);

		Result.add (New Developer ("Iris", New BigDecimal ("170000"), 55);

	return result; }

}

Output

Before Sort
Developer [Name=mkyong, salary=70000, age=33]
Developer [Name=alvin, salary=80000, age=20]
Developer [Name=jason, salary=100000, age=10]
Developer [Name=iris, salary=170000, age=55] after

Sort
Developer [Name=jason, salary=100000, age=10]
Developer [Name=alvin, salary=80000, age=20]
Developer [name= Mkyong, salary=70000, age=33]
Developer [Name=iris, salary=170000, age=55]

When the sort requirement changes, you can only implement it through a new anonymous class, as follows:

//sort by Age Collections.sort (Listdevs, New comparator<developer> () {@Override public int compare (Developer O1,
	Developer O2) {return o1.getage ()-o2.getage ();

}
});  Sort by name Collections.sort (Listdevs, New comparator<developer> () {@Override public int compare (Developer O1, Developer O2) {return o1.getname (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.