JAva 8における極めて強力な新しい特性Stream(非常に実用的)
41328 ワード
このブログは転載しています.https://zhuanlan.zhihu.com/p/97493325
1.Stream構文の説明
1.Stream構文の説明
Streamの実行プロセスは簡単で、主に3つあります.まずStreamを作成し、Streamを使用してデータを操作し、最後にStreamを終了します.Streamのライフサイクルに似ています.以下、その流れに沿って説明します.まずStudentクラスを作成し、その後、私たちは毎回このクラスを操作します.public class Student {
private Integer id;
private String name;
private Integer age;
private Double score;
public Student() {
}
public Student(Integer id, String name, Integer age, Double score) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}
//getter setter
//toString
}
2.Streamを作成
2.1.1つのコレクションからStreamを作成
@Test
public void test1(){
List<Student> studentList = StudentData.getStudents();
// :
Stream<Student> stream = studentList.stream();
// :
Stream<Student> stream2 = studentList.parallelStream();
}
2.2.1つの配列でStreamを作成
@Test
public void test2(){
// Stream
int[] arr = new int[]{1,2,34,4,65,7,87,};
IntStream intStream = Arrays.stream(arr);
// Student Stream
Student[] students = StudentData.getArrStudents();
Stream<Student> stream = Arrays.stream(students);
}
2.3.ストリームを通るof
@Test
public void test3(){
Stream<Integer> integerStream = Stream.of(1, 2, 3, 5, 6, 7, 8);
Stream<String> stringStream = Stream.of("1", "2", "3", "4", "5");
Stream<Student> studentStream = Stream.of(
new Student(1, " ", 18, 90.4),
new Student(2, " ", 19, 87.4),
new Student(3, " ", 21, 67.4));
}
2.4.無限ストリームの作成
@Test
public void test4(){
// 5 , 0 ,
Stream.iterate(0,t->t+5).forEach(System.out::println);
// 5 , 0 , 5
Stream.iterate(0,t->t+5).limit(5).forEach(System.out::println);
//
Stream.generate(Math::random).limit(5).forEach(System.out::println);
}
3.Stream操作データの使用
3.1.フィルタとスライス
@Test
public void test1(){
List<Student> list = StudentData.getStudents();
//(1) : 20
list.stream().filter(item->item.getAge()>20).forEach(System.out::println);
//(2) : 3
list.stream().limit(3).forEach(System.out::println);
//(3) : 5
list.stream().skip(5).forEach(System.out::println);
//(4) :
list.stream().distinct().forEach(System.out::println);
}
3.2.マッピング
@Test
public void test2(){
//(1)map
List<String> list = Arrays.asList("java","python","go");
Stream<String> stream = list.stream();
//
stream.map(str -> str.toUpperCase()).forEach(System.out::println);
// , 20
List<Student> studentList = StudentData.getStudents();
Stream<Student> stream1 = studentList.stream();
Stream<Integer> ageStream = stream1.map(Student::getAge);
ageStream.filter(age->age>20).forEach(System.out::println);
//(2)floatMap:
}
3.3.ツールバーの
public void test3(){
//(1)
List<Integer> list = Arrays.asList(4,3,7,9,12,8,10,23,2);
Stream<Integer> stream = list.stream();
stream.sorted().forEach(System.out::println);
//(2) : comparable ,
// : compable
List<Student> studentList = StudentData.getStudents();
studentList.stream().sorted().forEach(System.out::println);
// : comparable
List<Student> studentList1 = StudentData.getStudents();
studentList1.stream()
.sorted((e1,e2)-> Integer.compare(e1.getAge(),e2.getAge()))
.forEach(System.out::println);
}
4.Streamの終了
4.1.照合と検索
public void test1(){
List<Student> list = StudentData.getStudents();
//(1) 20
boolean allMatch = list.stream().allMatch(item -> item.getAge() > 20);
//(2) 20
boolean anyMatch = list.stream().anyMatch(item -> item.getAge() > 20);
//(3)
boolean noneMatch = list.stream().noneMatch(item -> item.getName().equals(" "));
//(4)
Optional<Student> first = list.stream().findFirst();
//(5)
long count = list.stream().count();
long count1 = list.stream().filter(item -> item.getScore() > 90.0).count();
//(6)
Optional<Student> any = list.stream().findAny();
//(7) :Student comparable ,
Stream<Double> doubleStream = list.stream().map(item -> item.getScore());
doubleStream.max(Double::compare);
//(8)
}
4.2.に約束
public void test2(){
//(1)
List<Integer> list = Arrays.asList(1,2,3,4,5);
list.stream().reduce(0,Integer::sum);
//(3)
List<Student> studentList = StudentData.getStudents();
Stream<Double> doubleStream = studentList.stream().map(Student::getScore);
doubleStream.reduce(Double::sum);
}
4.3.収集
public void test3(){
List<Student> studentList = StudentData.getStudents();
// list
List<Student> listStream = studentList.stream()
.filter(e -> e.getAge() > 18)
.collect(Collectors.toList());
// Set
Set<Student> setStream = studentList.stream()
.filter(e -> e.getAge() > 18)
.collect(Collectors.toSet());
//
}
public class Student {
private Integer id;
private String name;
private Integer age;
private Double score;
public Student() {
}
public Student(Integer id, String name, Integer age, Double score) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}
//getter setter
//toString
}
2.1.1つのコレクションからStreamを作成
@Test
public void test1(){
List<Student> studentList = StudentData.getStudents();
// :
Stream<Student> stream = studentList.stream();
// :
Stream<Student> stream2 = studentList.parallelStream();
}
2.2.1つの配列でStreamを作成
@Test
public void test2(){
// Stream
int[] arr = new int[]{1,2,34,4,65,7,87,};
IntStream intStream = Arrays.stream(arr);
// Student Stream
Student[] students = StudentData.getArrStudents();
Stream<Student> stream = Arrays.stream(students);
}
2.3.ストリームを通るof
@Test
public void test3(){
Stream<Integer> integerStream = Stream.of(1, 2, 3, 5, 6, 7, 8);
Stream<String> stringStream = Stream.of("1", "2", "3", "4", "5");
Stream<Student> studentStream = Stream.of(
new Student(1, " ", 18, 90.4),
new Student(2, " ", 19, 87.4),
new Student(3, " ", 21, 67.4));
}
2.4.無限ストリームの作成
@Test
public void test4(){
// 5 , 0 ,
Stream.iterate(0,t->t+5).forEach(System.out::println);
// 5 , 0 , 5
Stream.iterate(0,t->t+5).limit(5).forEach(System.out::println);
//
Stream.generate(Math::random).limit(5).forEach(System.out::println);
}
3.Stream操作データの使用
3.1.フィルタとスライス
@Test
public void test1(){
List<Student> list = StudentData.getStudents();
//(1) : 20
list.stream().filter(item->item.getAge()>20).forEach(System.out::println);
//(2) : 3
list.stream().limit(3).forEach(System.out::println);
//(3) : 5
list.stream().skip(5).forEach(System.out::println);
//(4) :
list.stream().distinct().forEach(System.out::println);
}
3.2.マッピング
@Test
public void test2(){
//(1)map
List<String> list = Arrays.asList("java","python","go");
Stream<String> stream = list.stream();
//
stream.map(str -> str.toUpperCase()).forEach(System.out::println);
// , 20
List<Student> studentList = StudentData.getStudents();
Stream<Student> stream1 = studentList.stream();
Stream<Integer> ageStream = stream1.map(Student::getAge);
ageStream.filter(age->age>20).forEach(System.out::println);
//(2)floatMap:
}
3.3.ツールバーの
public void test3(){
//(1)
List<Integer> list = Arrays.asList(4,3,7,9,12,8,10,23,2);
Stream<Integer> stream = list.stream();
stream.sorted().forEach(System.out::println);
//(2) : comparable ,
// : compable
List<Student> studentList = StudentData.getStudents();
studentList.stream().sorted().forEach(System.out::println);
// : comparable
List<Student> studentList1 = StudentData.getStudents();
studentList1.stream()
.sorted((e1,e2)-> Integer.compare(e1.getAge(),e2.getAge()))
.forEach(System.out::println);
}
4.Streamの終了
4.1.照合と検索
public void test1(){
List<Student> list = StudentData.getStudents();
//(1) 20
boolean allMatch = list.stream().allMatch(item -> item.getAge() > 20);
//(2) 20
boolean anyMatch = list.stream().anyMatch(item -> item.getAge() > 20);
//(3)
boolean noneMatch = list.stream().noneMatch(item -> item.getName().equals(" "));
//(4)
Optional<Student> first = list.stream().findFirst();
//(5)
long count = list.stream().count();
long count1 = list.stream().filter(item -> item.getScore() > 90.0).count();
//(6)
Optional<Student> any = list.stream().findAny();
//(7) :Student comparable ,
Stream<Double> doubleStream = list.stream().map(item -> item.getScore());
doubleStream.max(Double::compare);
//(8)
}
4.2.に約束
public void test2(){
//(1)
List<Integer> list = Arrays.asList(1,2,3,4,5);
list.stream().reduce(0,Integer::sum);
//(3)
List<Student> studentList = StudentData.getStudents();
Stream<Double> doubleStream = studentList.stream().map(Student::getScore);
doubleStream.reduce(Double::sum);
}
4.3.収集
public void test3(){
List<Student> studentList = StudentData.getStudents();
// list
List<Student> listStream = studentList.stream()
.filter(e -> e.getAge() > 18)
.collect(Collectors.toList());
// Set
Set<Student> setStream = studentList.stream()
.filter(e -> e.getAge() > 18)
.collect(Collectors.toSet());
//
}
@Test
public void test1(){
List<Student> list = StudentData.getStudents();
//(1) : 20
list.stream().filter(item->item.getAge()>20).forEach(System.out::println);
//(2) : 3
list.stream().limit(3).forEach(System.out::println);
//(3) : 5
list.stream().skip(5).forEach(System.out::println);
//(4) :
list.stream().distinct().forEach(System.out::println);
}
@Test
public void test2(){
//(1)map
List<String> list = Arrays.asList("java","python","go");
Stream<String> stream = list.stream();
//
stream.map(str -> str.toUpperCase()).forEach(System.out::println);
// , 20
List<Student> studentList = StudentData.getStudents();
Stream<Student> stream1 = studentList.stream();
Stream<Integer> ageStream = stream1.map(Student::getAge);
ageStream.filter(age->age>20).forEach(System.out::println);
//(2)floatMap:
}
public void test3(){
//(1)
List<Integer> list = Arrays.asList(4,3,7,9,12,8,10,23,2);
Stream<Integer> stream = list.stream();
stream.sorted().forEach(System.out::println);
//(2) : comparable ,
// : compable
List<Student> studentList = StudentData.getStudents();
studentList.stream().sorted().forEach(System.out::println);
// : comparable
List<Student> studentList1 = StudentData.getStudents();
studentList1.stream()
.sorted((e1,e2)-> Integer.compare(e1.getAge(),e2.getAge()))
.forEach(System.out::println);
}
4.1.照合と検索
public void test1(){
List<Student> list = StudentData.getStudents();
//(1) 20
boolean allMatch = list.stream().allMatch(item -> item.getAge() > 20);
//(2) 20
boolean anyMatch = list.stream().anyMatch(item -> item.getAge() > 20);
//(3)
boolean noneMatch = list.stream().noneMatch(item -> item.getName().equals(" "));
//(4)
Optional<Student> first = list.stream().findFirst();
//(5)
long count = list.stream().count();
long count1 = list.stream().filter(item -> item.getScore() > 90.0).count();
//(6)
Optional<Student> any = list.stream().findAny();
//(7) :Student comparable ,
Stream<Double> doubleStream = list.stream().map(item -> item.getScore());
doubleStream.max(Double::compare);
//(8)
}
4.2.に約束
public void test2(){
//(1)
List<Integer> list = Arrays.asList(1,2,3,4,5);
list.stream().reduce(0,Integer::sum);
//(3)
List<Student> studentList = StudentData.getStudents();
Stream<Double> doubleStream = studentList.stream().map(Student::getScore);
doubleStream.reduce(Double::sum);
}
4.3.収集
public void test3(){
List<Student> studentList = StudentData.getStudents();
// list
List<Student> listStream = studentList.stream()
.filter(e -> e.getAge() > 18)
.collect(Collectors.toList());
// Set
Set<Student> setStream = studentList.stream()
.filter(e -> e.getAge() > 18)
.collect(Collectors.toSet());
//
}