référence http://www.ne.jp/asahi/hishidama/home/tech/java/stream.html
List.forEach
List<Integer> list = Arrays.asList(1,2,3);
list.forEach(System.out :: println);
C'est la même chose que
```
List<Integer> list = Arrays.asList(1,2,3);
for(int i: list){
System.out.println(i);
}
```
1. Map.forEach
```
Map<Integer,String> map = new HashMap<>();
map.put(1,"a");
map.put(2,"b");
map.forEach((k,v) -> System.out.println(v));
```
C'est la même chose que
```
Map<Integer,String> map = new HashMap<>();
map.put(1,"a");
map.put(2,"b");
for(Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getValue());
}
```
Fréquent
Créer une carte à partir de la liste Stream.collect (Collectors.toMap ())
```
class Employee {
private int id;
private String name;
public Employee(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
}
List<Employee> list = Arrays.asList(new Employee(1,"Sato"),new Employee(2,"grand arbre"),new Employee(3,"Tanaka"));
Map<Integer,String> map = lists.stream().collect(Collectors.toMap(Employee::getId,Employee::getName));
```
Processus dans la liste Stream.map ()
```
List<Integer> list = Arrays.asList(1,2,3);
List<Integer> list2 = list.stream().map(i -> i * 2).collect(Collectors.toList());
```
Stream.filter () Réduire
```
List<Integer> list = Arrays.asList(1,2,3);
list.stream().filter(i -> i < 3).forEach(System.out :: println);
```
Stream.sorted Trié
```
List<Integer> list = Arrays.asList(1,3,2);
list.stream().sorted(Comparator.comparing(i -> i)).forEach(System.out :: println);
```
Utilisez si bien
Stream.of () Création de flux
```
Stream<Integer> stream = Stream.of(1,2,3);
```
Stream.anyMatch () Vérifier les données correspondantes
```
List<Integer> list = Arrays.asList(1,2,3);
boolean m = list.stream().anyMatch(i -> i == 2);
```
Tout comme ci-dessus sont allMatch (toutes les valeurs correspondent) et noneMatch (toutes les valeurs ne correspondent pas).
List.stream (). FlatMap (l-> l.stream ()) Combine des listes dans une liste en une seule
```
List<List<Integer>> list = Arrays.asList(Arrays.asList(1,2,3),Arrays.asList(4,5,6));
List<Integer> list2 = list.stream().flatMap(l -> l.stream()).collect(Collectors.toList());
```
Recommended Posts