package com.stream;
import com.stream.pojo.User;
import org.junit.Test;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class InstreamOrMap {
@Test
public void test() {
mapToObj();
}
private void generateIntStream() {
Random random = new Random();
IntStream limit = IntStream.generate(() -> random.nextInt(1000))
.limit(3);
limit.forEach(System.out::print);
}
private void rangeIntStream() {
IntStream.range(1, 10)
.forEach(System.out::print);
System.out.println();
IntStream.rangeClosed(1, 10)
.forEach(System.out::print);
}
private void ofIntStream() {
IntStream.of(1, 2, 3, 4, 5, 6, 7).forEach(System.out::println);
}
private void builderIntStream() {
IntStream.builder()
.add(10)
.add(11)
.add(12)
.build()
.forEach(System.out::print);
}
private void iterateIntStream() {
IntStream.iterate(1, e -> e * 2)
.limit(3)
.forEach(System.out::println);
}
private void concatIntStream() {
Random random = new Random();
IntStream stream_1 = IntStream.generate(() -> random.nextInt(1000))
.limit(3);
IntStream stream_2 = IntStream.generate(() -> random.nextInt(1000))
.limit(3);
IntStream concat = IntStream.concat(stream_1, stream_2);
concat.forEach(System.out::println);
}
private void map() {
IntStream.rangeClosed(1, 10)
.map(e -> e * 100)
.forEach(System.out::println);
}
private void mapToObj() {
List<User> userList = IntStream.rangeClosed(1, 10)
.mapToObj(item -> User.builder().userName("userName-" + item).age(item).build())
.collect(Collectors.toList());
System.out.println(userList);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101