04 MoreOnJava Part2
04 MoreOnJava Part2
Object-Oriented Programming
Outline
■ Input/Output
■ Command-line parameters
■ Packages
■ Default and Static methods of Interfaces
■ final keyword
■ Lambda and Streams
■ Details:
❑ Head First Java. Ch. 16
■ In this slide:
❑ standard input / output stream
■ System.in
❑ An InputStream object
// read a word
String s = sc.next());
// read an integer
int i = sc.nextInt();
} catch(IOException e) {
e.printStackTrace();
}
}
...
Đại học Công nghệ - ĐHQG HN More on Java 8
Write to a text file. Example
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
...
public static void main(String args[]) {
int i = 1; long l = 10;
try {
// create a printwriter to write output to a file stream
PrintWriter out = new PrintWriter(new FileWriter("test.data"));
// write to file
out.println("Hello " + i + " " + l);
out.close();
} catch(IOException e) {
e.printStackTrace();
}
}
...
■ Run
java Hello
■ Run
❑ C:\java\> java Hello
❑ C:\java\> java hanv.HelloMsg (if HelloMsg is a program)
(interface) (interface)
BinaryExpression Foo
default default
convertToString() convertToString()
BinaryFoo
17
Default methods - multi-inheritance?
(interface) (interface)
BinaryExpression Foo
default default
convertToString() convertToString()
BinaryFoo
convertToString()
have to
public String convertToString() { implement and specify
return BinaryExpression.super.convertToString() which version to use
+ Foo.super.convertToString();
}
18
Interface - static method
interface Vehicle {
...
static int getHorsePower(int rpm, int torque) {
return (rpm * torque) / 5252;
}
}
Vehicle.getHorsePower(2500, 480);
This function
- cannot access instance variables/method
- can be called from anywhere, just like a class’ static function
19
Abstract classes vs. Interfaces
20
Immutable data types
String: immutable
should use for Key types in data structures
StringBuilder, StringPrinter: mutable
21
final instance variables
22
final instance method
23
final class
24
Streams - motivation
loops
■ are used all over the place
■ tedious to write and error-prone
Common tasks on a collection of data:
- sort
- take only the top k items and ignore the rest
- ignore duplicated items
- ignore items that don’t match certain criteria
- map each item to something else to continue work on it
- sum
- grouping by certain key and make a map
- …
25
Stream API
26
Example
27
01 Source
Str
ea
m
Zero or more
intermediate operations
m
ea
Str
01 terminal operation
28
01 Source
Zero or more
intermediate operations
01 terminal operation
29
Stream pipeline
List<String> strings = Arrays.asList(
"I", "am", "a", "list", "of", "Strings", "Z");
List<String> result = strings.stream()
.sorted((s1, s2) -> s1.compareToIgnoreCase(s2))
.skip(2)
.limit(4)
.collect(Collectors.toList());
System.err.print("result = " + result);
30
Stream pipeline
List<String> strings = Arrays.asList(
"I", "am", "a", "list", "of", "Strings", "Z");
List<String> result = strings.stream()
.sorted((s1, s2) -> s1.compareToIgnoreCase(s2))
.skip(2)
.limit(4)
.collect(Collectors.toList());
System.err.print("result = " + result);
31
forEach()
List<String> strings
= Arrays.asList("I", "am", "a", "list", "of", "Strings");
strings.stream()
.sorted((s1, s2) -> s1.compareToIgnoreCase(s2))
.skip(2)
.limit(4)
.forEach(s -> System.err.print(s));
32
Lambda Expression - Functional Interface
33
Lambda Expression - Comparator Interface
34