Java Reference
In-Depth Information
@Override
public String toString() {
String str = String.format("(%s, %s, %s, %s, %.2f)",
id, name, gender, dob, income);
return str;
}
}
The
Person
class contains a static
Gender
enum to represent the gender of a person. The class declares five
instance variables (
id
,
name
,
gender
,
dob
, and
income
), getters, and setters. The
isMale()
and
isFemale()
methods
have been declared to be used as method references in lambda expressions. You will use a list of people frequently,
and the class contains a static method
persons()
to get a list of people.
Creating Streams
There are many ways to create streams. Many existing classes in the Java libraries have received new methods that
return a stream. Based on the data source, stream creation can be categorized as follows:
•
Streams from values
•
Empty streams
•
Streams from functions
•
Streams from arrays
•
Streams from collections
•
Streams from files
•
Streams from other sources
Streams from Values
The
Stream
interface contains the following two static
of()
methods to create a sequential
Stream
from a single value
and multiple values:
<T> Stream<T> of(T t)
•
<T> Stream<T> of(T...values)
•
The following snippet of code creates two streams:
// Creates a stream with one string elements
Stream<String> stream = Stream.of("Hello");
// Creates a stream with four strings
Stream<String> stream = Stream.of("Ken", "Jeff", "Chris", "Ellen");