java8新特性——Stream介面
Stream介面
1,stream的foreach方法
Stream不是一個數據結構,不直接儲存資料,它通過管道來操作資料。
建立Stream介面實現類物件:
stream():建立一個Stream介面實現類的物件:
Stream<Person> stream = People.stream();
使用方式:
先建立一個Person類:
package new_feature.stream;
public class Person {
public static enum Sex {
MALE, FEMALE
}
private String name;
private Sex gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Sex getGender() {
return gender;
}
public void setGender(Sex gender) {
this.gender = gender;
}
public Person(String name, Sex sex) {
this.name = name;
this.gender = sex;
}
@Override
public String toString() {
return "Person [name=" + name + ", gender=" + gender + "]";
}
}
測試類:
package new_feature.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import new_feature.stream.Person.Sex;
public class StreamTest {
// 集合初始化
static List<Person> createPeople() {
List<Person> people = new ArrayList<Person>();
Person person1 = new Person("張飛", Sex.MALE);
Person person2 = new Person("關羽", Sex.MALE);
Person person3 = new Person("劉備", Sex.MALE);
people.add(person3);
people.add(person2);
people.add(person1);
return people;
}
}
main方法中的呼叫:
public static void main(String[] args) {
List<Person> people = createPeople();
Stream<Person> stream = people.stream();
stream.forEach(p -> System.out.println(p.toString()));
}
這樣就可以一次打出person裡的內容了。
2,stream的filter方法
先給people設定兩類的值,一是male,一是female
// 集合初始化
static List<Person> createPeople() {
List<Person> people = new ArrayList<Person>();
Person person1 = new Person("張飛", Sex.MALE);
Person person2 = new Person("關羽", Sex.MALE);
Person person3 = new Person("劉備", Sex.MALE);
Person person4 = new Person("貂蟬", Sex.FEMALE);
Person person5 = new Person("黃月英", Sex.FEMALE);
people.add(person3);
people.add(person2);
people.add(person1);
people.add(person4);
people.add(person5);
return people;
}
然後在main方法中進行呼叫:
public static void main(String[] args) {
List<Person> people = createPeople();
people.stream().filter(p -> p.getGender() == Person.Sex.FEMALE).forEach(p -> System.out.println(p));
}
這樣就可以實現,先選擇出female類別的,在進行處理。
3,DoubleStream
DoubleStream介面表示元素型別是double的資料來源
DoubleStream介面的常用方法:
(1)max().getAsDouble():獲取流中資料集的最大值;
(2)stream.min().getAsDouble():獲取資料流中資料集的最小值;
(3)stream.average():獲取資料流中資料集的平均值;
修改person類,增加height欄位:
private double height;
並增加一個構造方法:
public Person(String name, Sex sex, double height) {
this.name = name;
this.gender = sex;
this.height = height;
}
構造資料的函式:
// 集合初始化
static List<Person> createPeople() {
List<Person> people = new ArrayList<Person>();
Person person1 = new Person("張飛", Sex.MALE, 1.8);
Person person2 = new Person("關羽", Sex.MALE, 1.85);
Person person3 = new Person("劉備", Sex.MALE, 1.75);
Person person4 = new Person("貂蟬", Sex.FEMALE, 1.63);
Person person5 = new Person("黃月英", Sex.FEMALE, 1.65);
people.add(person3);
people.add(person2);
people.add(person1);
people.add(person4);
people.add(person5);
return people;
}
測試方法:
public static void main(String[] args) {
List<Person> people = createPeople();
double aveHeight = people.stream().filter(p -> p.getGender() == Person.Sex.MALE).mapToDouble(p -> p.getHeight()).average().getAsDouble();
System.out.println("所有男性的平均身高:" + aveHeight + "米");
}
是不是方便了很多,感覺java學習了動態語言的功能,強化了靈活性。