Professional Documents
Culture Documents
At First Glance
Example 2:
JButton testButton = new JButton("Test Button");
testButton.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent ae){
System.out.println(“Hello Anonymous inner class");
}
});
Lambda Expression
Example 1: with lambda
Comparator<Person> byAge =
(Person p1, Person p2) -> p1.getAge().compareTo(p2.getAge());
Lambda Syntax
Lambda Expression
Arrow
or
(parameters) -> { statements; }
Lambda Expression
❖ Lambda does not have:
✓ Name
✓ Return type
✓ Throws clause
✓ Type parameters
Lambda Expression
Examples:
1. (String s) -> s.length()
2. (Person p) -> p.getAge() > 20
3. () -> 92
4. (int x, int y) -> x + y
5. (int x, int y) -> {
System.out.println(“Result:”);
System.out.println(x + y);
}
Lambda Expression
Not allow:
Variable scope:
public void
public void printNumber(int
printNumber(int x)
x) {{
int yy == 2;
int 2;
Runnable rr == ()
Runnable () ->{System.out.println(“Total
-> System.out.println(“Total
-> { is:” + (x+y));
new x++;//compile error
Thread(r).start();System.out.println(this.toString());};
} new System.out.println(“Total
Thread(r).start(); is:” + (x+y))
} };
Free variable: not define in lambda parameters.
}
Lambda Expression
Listener:
ActionListener listener = e -> System.out.println(“Hello
Lambda")
testButton.addActionListener(listener );
Lambda Expression
Runnable:
// Anonymous class
Runnable r1 = new Runnable(){
@Override
public void run(){
System.out.println("Hello world one!");
}
};
// Lambda Runnable
Runnable r2 = () -> System.out.println("Hello world two!");
Lambda Expression
Iterator:
Annotation in Functional
Interface
Functional Interface
Add text here...
Functional Interfaces
Toolbox
Functional Interface
Add text here...
java.util.Collections
static <T> Collection<T>
java.util.Collection synchronizedCollection(Collection<T
What is a Stream?
Stream
Add text here...
Old Java:
Stream
Add text here...
Java 8:
Stream
Add text here...
1. Build a stream
2. Transform stream
3. Collect result
Stream - build streams
Add text here...
Terminal operations
❖ forEach()
❖ collect()
❖ match()
❖ ….
Stream - forEach()
Add text here...
/* Console output
* 2
* 4
* 6
* 8
*/
Add text here...
Stream - Converting to collections
Converting stream to list
List<Integer> numbers =
evenNumbers.collect(Collectors.toList());
● LocalDate
○ A LocalDate is a date, with a year,
month, and day of the month
LocalDate today = LocalDate.now();
LocalDate christmas2015 = LocalDate.of(2015, 12, 25);
LocalDate christmas2015 = LocalDate.of(2015,
Month.DECEMBER, 25);
Date / Time
Add text here...
Java 7:
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date dt = c.getTime();
Java 8:
LocalDate tomorrow = LocalDate.now().plusDay(1);
Date / Time
Add text here...
● Temporal adjuster
LocalDate nextPayDay = LocalDate.now()
.with(TemporalAdjusters.lastDayOfMonth());
Date / Time
Add text here...