[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/huanpython/learn-java8/master/java8-stream/README.md [Back]  [Original]

# Stream

## 

### 

Java8
SQL
Java

### 

1. 
    ()
    
2. 
    
    

### 



1. 
    
    
2. 
    
    

### 



1. 
2. 
    
3. 
    

## 

### 

StreamAPI

**1. **

stream()

```java
List list = new ArrayList(); 
Stream stream = list.stream();
```

**2. **

Arraysstream()

```java
String[] names = {"chaimm","peter","john"};
Stream stream = Arrays.stream(names);
```

**3. **



```java
Stream stream = Stream.of("chaimm","peter","john");
```

**4. **

```java
try(Stream lines = Files.lines(Paths.get(),Charset.defaultCharset())){
    //lines
}catch(IOException e){
}
```

**5. iterator**

****

```java
Stream.iterate(0, n -> n + 2)
      .limit(10)
      .forEach(System.out::println);
```
    
> PSJava7IOIOtryIO

###  filter

filter Lambdabooleanfiltertrue


```java
List result = list.stream()
                    .filter(Person::isStudent)
                    .collect(toList());
```

### distinct



```java
List result = list.stream()
                    .distinct()
                    .collect(toList());
```
 
### 

N

```java
List result = list.stream()
                    .limit(3)
                    .collect(toList());
```

### 

n

```java
List result = list.stream()
                    .skip(3)
                    .collect(toList());
```

### 

mapmapLambda
(PerosnString)

```java
List result = list.stream()
                    .map(Person::getName)
                    .collect(toList());
```

### 

ListList

```java
List list = new ArrayList();
list.add("I am a boy");
list.add("I love the girl");
list.add("But the girl loves another girl");
```



list

```java
list.stream();
```



```java
list.stream()
            .map(line->line.split(" "));
```

String[]

 `String[]` 

```java
list.stream()
            .map(line->line.split(" "))
            .map(Arrays::stream)
```



 `flatMap`  map

```java
list.stream()
    .map(line->line.split(" "))
    .flatMap(Arrays::stream)
```



```java
list.stream()
    .map(line->line.split(" "))
    .flatMap(Arrays::stream)
    .distinct()
    .collect(toList());
```

### anyMatch

anyMatchLambdaanyMatchboolean
list

```java
boolean result = list.stream()
            .anyMatch(Person::isStudent);
```

### allMatch

allMatchLambdaanyMatchboolean


```java
boolean result = list.stream()
            .allMatch(Person::isStudent);
```

### noneMatch

noneMatchallMatch

```java
boolean result = list.stream()
            .noneMatch(Person::isStudent);
```

### findAny

findAnyOptional

```java
Optional person = list.stream().findAny();
```

### findFirst

```java
Optional person = list.stream().findFirst();
```

### 



reduce
reduce

1. 
2. Lambda

**Lambda**



```java
int age = list.stream().reduce(0, (person1,person2)->person1.getAge()+person2.getAge());
```

1. reduce0
2. reduceLambdareduceLambda

**Integer.sum**

LambdaIntegersumLambda

```java
int age = list.stream().reduce(0, Integer::sum);
```

Integer `min``max` 

### 

reduce


****

StreamAPIIntStreamDoubleStreamLongStreammapToIntmapToDoublemapToLong
Personage

```java
IntStream stream = list.stream().mapToInt(Person::getAge);
```

****

maxminsum

```java
OptionalInt maxAge = list.stream()
                                .mapToInt(Person::getAge)
                                .max();
```

maxOptionalIntOptional
mapToIntmapToDoublemapToLongOptionalIntOptionalDoubleOptionalLong

## 

|  |  |  | / |  |
|:-----:|:--------|:-------|:-------|:-------|
| `filter` |  | `Stream` | `Predicate` | `T -> boolean` |
| `distinct` |  | `Stream` |  |  |
| `skip` |  | `Stream` | long |  |
| `map` |  | `Stream` | `Function` | `T -> R` |
| `flatMap` |  | `Stream` | `Function` | `T -> Stream` |
| `limit` |  | `Stream` | long  | |
| `sorted` |  | `Stream` | `Comparator` | `(T, T) -> int` |
| `anyMatch` |  | `boolean` | `Predicate` | `T -> boolean` |
| `noneMatch` |  | `boolean` | `Predicate` | `T -> boolean` |
| `allMatch` |  | `boolean` | `Predicate` | `T -> boolean` |
| `findAny` |  | `Optional` | |  |
| `findFirst` |  | `Optional` | |  |
| `forEach` |  | `void` | `Consumer` | `T -> void` |
| `collect` |  | `R` | `Collector` |  |
| `reduce` |  | `Optional` | `BinaryOperator` | `(T, T) -> T` |
| `count` |  | `long` | | |

## Collector 


`collect`  `Collector` 
`Collector`  `Collector` 

### 



### 

 `Collectors.reducing` 

* 
* 
* 

## 

Collectors`Collectors.summingInt`
 int `collect` 

### 

SQL `GROUPBY`

### 


 `groupingBy` 

* 
*  `groupingBy` 

**Collectors **

|  |  |  |  |
|:-----:|:--------|:-------|:-------|
| `toList` | `List` |  List | `List projects = projectStream.collect(toList());` |
| `toSet` | `Set` |  Set | `Set projects = projectStream.collect(toSet());` |
| `toCollection` | `Collection` |  | `Collection projects = projectStream.collect(toCollection(), ArrayList::new);` |
| `counting` | `Long` |  | `long howManyProjects = projectStream.collect(counting());` |
| `summingInt` | `Integer` |  | `int totalStars = projectStream.collect(summingInt(Project::getStars));` |
| `averagingInt` | `Double` |  Integer  | `double avgStars = projectStream.collect(averagingInt(Project::getStars));` |
| `summarizingInt` | `IntSummaryStatistics` |  Integer   | `IntSummaryStatistics projectStatistics = projectStream.collect(summarizingInt(Project::getStars));` |
| `joining` | `String` |  toString  | `String shortProject = projectStream.map(Project::getName).collect(joining(", "));` |
| `maxBy` | `Optional` |  Optional  Optional.empty() | `Optional fattest = projectStream.collect(maxBy(comparingInt(Project::getStars)));` |
| `minBy` | `Optional` |  Optional  Optional.empty() | `Optional fattest = projectStream.collect(minBy(comparingInt(Project::getStars)));` |
| `reducing` |  |  BinaryOperator  | `int totalStars = projectStream.collect(reducing(0, Project::getStars, Integer::sum));` |
| `collectingAndThen` |  |  | `int howManyProjects = projectStream.collect(collectingAndThen(toList(), List::size));` |
| `groupingBy` | `Map` |   Map  | `Map projectByLanguage = projectStream.collect(groupingBy(Project::getLanguage));` |
| `partitioningBy` | `Map` |  | `Map vegetarianDishes = projectStream.collect(partitioningBy(Project::isVegetarian));` |

### 

 `toList` `java.util.List` 
 `toSet`  `toCollection` `Set`  `Collection` 
 

- 
- 
- 

 `toCollection`

```java
stream.collect(toCollection(TreeSet::new));
```

 `maxBy`  `minBy` 

### 


 `Map`  `Boolean`: truefalse

truefalse

### 



 `parallel()` 

```java
public static long parallelSum(long n){
    return Stream.iterate(1L, i -> i +1).limit(n).parallel().reduce(0L,Long::sum);
}
```

 `sequential()` 

```java
stream.parallel().filter(...).sequential().map(...).parallel().reduce();
```
 






```java
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "12");
```




1.  
2.  IntStream, LongStream, DoubleStream
3. limitfindFirstfindAny
4. (Q)(N), QNQ, 
5. 
6. 

****

|  |  |
|:-----:|:-------|
| `ArrayList` |  |
| `LinkedList` |  |
| `IntStream.range` |  |
| `Stream.iterate` |  |
| `HashSet` |  |
| `TreeSet` |  |

** filter**

****

Web Proxy Viewer  |  New URL  |  Original Page