From charlesreid1

(Created page with "==Lambda Expressions== When it comes to dealing with functions as first-class objects in the language, Java is much more limited than Python. In fact, it was not until Java 8...")
 
Line 17: Line 17:
</pre>
</pre>


Now suppose we want to sort point objects by their x coordinates only (ignoring y coordinates). We start by defining a Comparator object that wraps Point objects: <code>Comparator<Point> pc</code>.
Now suppose we want to sort a list of Point objects, using some kind of custom criteria. We could have Point extend Comparable, and define a compareTo() method, so that we could perform a direct comparison with two point objects. But suppose I have many criteria, and I don't have any control over the Point object source code, so I can't modify the class definition. In this situation I can use a Comparator object, which defines a compare() method taking two objects, performs some calculation, and returns an integer result (analogous to the result that a compareTo method would return).


See [[Java/Comparable]] for more on built-in compareTo functionality and extending Comparable.


See [[Java/Comparators]] for more on comparators, which we are going to define below with some shorthand.


Comparator<Developer> byName =
We will demonstrate with some lambda Comparator examples. here is the framework:
(Developer o1, Developer o2)->o1.getName().compareTo(o2.getName());


<pre>
import java.util.Comparator;
import java.util.Arrays;
import java.util.Random;


public class MyComparator {
public static final Random r = new Random();


public static void main(String[] args) {


// DEFINE COMPARATOR HERE
// Make random points
int n = 7;
Point[] points = new Point[n];
for(int i=0; i<points.length; i++) {
Point p = new Point( randCoord(), randCoord() );
points[i] = p;
}
Arrays.sort(points, pc);
System.out.println(Arrays.toString(points));
}
public static int randCoord() {
int MIN = 100;
int MAX = 999;
return r.nextInt(MAX-MIN)+MIN;
}
public static class Point {
public int x, y;
public Point(int x0, int y0) {x=x0; y=y0;}
public String toString() { return "("+this.x+","+this.y+")"; }
}
}
</pre>
===Comparator Examples===
insert here.


==References==
==References==


Oracle Java tutorial on lambda expressions: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
Oracle Java tutorial on lambda expressions: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

Revision as of 23:48, 5 July 2017

Lambda Expressions

When it comes to dealing with functions as first-class objects in the language, Java is much more limited than Python. In fact, it was not until Java 8 that Java finally got lambda expressions, which are ways of defining functions using in-place, shorthand expressions.

Lambdas for Comparators

You can throw together a fast comparator with a single lambda function, by skipping the class definition and the compare method, and simply using a lambda function instead.

Suppose I have a simple Point object storing two elements:

public class Point {
    public int x, y;
    public Point(int x0, int y0) { this.x = x0; this.y = y0; }
    public String tString() { return "("+this.x+","+this.y+")";}
}

Now suppose we want to sort a list of Point objects, using some kind of custom criteria. We could have Point extend Comparable, and define a compareTo() method, so that we could perform a direct comparison with two point objects. But suppose I have many criteria, and I don't have any control over the Point object source code, so I can't modify the class definition. In this situation I can use a Comparator object, which defines a compare() method taking two objects, performs some calculation, and returns an integer result (analogous to the result that a compareTo method would return).

See Java/Comparable for more on built-in compareTo functionality and extending Comparable.

See Java/Comparators for more on comparators, which we are going to define below with some shorthand.

We will demonstrate with some lambda Comparator examples. here is the framework:

import java.util.Comparator;
import java.util.Arrays;
import java.util.Random;

public class MyComparator {
	public static final Random r = new Random();

	public static void main(String[] args) { 

		// DEFINE COMPARATOR HERE
		
		// Make random points 
		int n = 7;
		Point[] points = new Point[n];
		for(int i=0; i<points.length; i++) { 
			Point p = new Point( randCoord(), randCoord() );
			points[i] = p;
		}
		Arrays.sort(points, pc);
		System.out.println(Arrays.toString(points));
	}

	public static int randCoord() { 
		int MIN = 100;
		int MAX = 999;
		return r.nextInt(MAX-MIN)+MIN;
	}

	public static class Point {
		public int x, y;
		public Point(int x0, int y0) {x=x0; y=y0;}
		public String toString() { return "("+this.x+","+this.y+")"; }
	}

}

Comparator Examples

insert here.

References

Oracle Java tutorial on lambda expressions: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html