-
Notifications
You must be signed in to change notification settings - Fork 48
Higher order function
LI YUAN XIN edited this page Nov 30, 2020
·
3 revisions
There are quite a number of ways to build higher order function. Let's take comparator as an example.
class PersonComparator implements Comparator<Person> {
@Override
public int compare(Person a, Person b){
return a.getName().compareTo(b.getName());
}
}
PriorityQueue<Person> pq = new PriorityQueue<>(new PersonComparator());
PriorityQueue<Person> pq = new PriorityQueue<>(new Comparator<Person>() {
@Override
public int compare(Person a, Person b){
return a.getName().compareTo(b.getName());
}
});
PriorityQueue<Person> pq = new PriorityQueue<>((x,y) -> x.getName().compareTo(y.getName()));
// pre-requisite: You need to have a getter method for that attribute that you wish to compare
PriorityQueue<Person> pq = new PriorityQueue<>(Comparator.comparing(Person::getName));
pq.add(new Person("charlie"));
pq.add(new Person("alpha"));
pq.add(new Person("delta"));
pq.add(new Person("bravo"));
while(!pq.isEmpty()) {
System.out.println(pq.poll());
}
Peer Learning
Codecrunch Contributions
Piazza Contributions
Wiki Contributions
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Sunfire
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
CS2030 Contents
Lecture 1 SummaryCompile-run vs Run-time Summary
Quick Guide To Abstraction
Generics and Variance of Types
Comparable vs Comparator
Summary of completable future
CS2030S Notes
ELI5 Optional.map vs Optional.flatMap
PECS Example Code
Java Collection Framework (Iterator)
Generic
Generic Type Parameter and Generic Wildcard
Calculator
Lambda-Expression
Single Abstract Method (SAM)
Method Reference
Functional Interfaces 2
Simple Usage of Sandbox
Associative-but-not-commutative
Higher Order function
Functional Programming
Calculator With Functor
Eager Evaluation VS Lazy Evaluation
Simple Usage of Lazy Evaluation
Lazy Evaluation for LazyList
Lazy Evaluation for BinaryTree
Stream
Parallel Stream
Optional
Simple Usage of Stream
Asynchronous Programming
Notes on CompletableFuture
Notes on CompletableFuture 2
Simple Usage of CompletableFuture
Mind Map
Exception Handling
Links
CS2030 Java Style Guide
CS2030 Javadoc Specification
JDK 11 Download Link
JDK 11 API Docs
Codecrunch
Piazza Forum