Java Stream流的 list案例
时间: 2024-04-30 21:02:03 浏览: 9
好的,我了解您的要求。以下是一个Java Stream流的 list案例。
假设我们有一个Person类,其中包含属性name和age。我们有一个List<Person>对象,现在我们要使用Java Stream流来过滤出其中年龄大于18岁的人的名字。
代码如下:
```
List<Person> people = new ArrayList<>();
people.add(new Person("Mike", 20));
people.add(new Person("John", 17));
people.add(new Person("Lucy", 25));
List<String> names = people.stream()
.filter(p -> p.getAge() > 18)
.map(Person::getName)
.collect(Collectors.toList());
System.out.println(names);
```
输出结果为:
```
[Mike, Lucy]
```
以上代码中,我们使用Java Stream的filter()方法来过滤出年龄大于18岁的人,然后使用map()方法来将过滤后的Person对象转为其name属性值,最后使用collect()方法将所有name属性值收集到一个List<String>中。
相关问题
java stream分组求和案例
下面是一个使用Java Stream进行分组求和的例子:
```java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class GroupingAndSummingExample {
public static void main(String[] args) {
// 创建一个包含多个学生的列表
List<Student> students = Arrays.asList(
new Student("Alice", "Math", 85),
new Student("Bob", "English", 75),
new Student("Alice", "English", 90),
new Student("Bob", "Math", 80),
new Student("Charlie", "Math", 95)
);
// 使用Stream进行分组求和
Map<String, Integer> sumByStudent = students.stream()
.collect(Collectors.groupingBy(Student::getName, Collectors.summingInt(Student::getScore)));
// 打印每个学生的总分
sumByStudent.forEach((name, totalScore) -> System.out.println(name + ": " + totalScore));
}
static class Student {
private String name;
private String subject;
private int score;
public Student(String name, String subject, int score) {
this.name = name;
this.subject = subject;
this.score = score;
}
public String getName() {
return name;
}
public String getSubject() {
return subject;
}
public int getScore() {
return score;
}
}
}
```
上述例子中,我们创建了一个包含多个学生的列表。然后使用Stream的`groupingBy`方法对学生进行分组,按照学生名字进行分组,并通过`summingInt`方法对每个分组中的学生成绩进行求和。最后,我们遍历结果Map,打印每个学生的总分。
运行上述代码,将会输出:
```
Alice: 175
Bob: 155
Charlie: 95
```
可以看到,学生Alice的总分是175,学生Bob的总分是155,学生Charlie的总分是95。
写一个java stream流的完整案例代码包含stream流中的所有功能
以下是一个完整的Java Stream流案例,包含了Stream流中的常用功能:
```java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class StreamDemo {
public static void main(String[] args) throws IOException {
// 读取文件并过滤掉空行
List<String> lines = Files.lines(Paths.get("data.txt"))
.filter(line -> !line.trim().isEmpty())
.collect(Collectors.toList());
// 将过滤后的内容写入到新文件中
Files.write(Paths.get("data_copy.txt"), lines);
// 将过滤后的内容转为大写并写入到新文件中
Files.write(Paths.get("data_uppercase.txt"), lines.stream()
.map(String::toUpperCase)
.collect(Collectors.toList()));
// 将过滤后的内容写入到gz压缩文件中
try (GZIPOutputStream out = new GZIPOutputStream(Files.newOutputStream(Paths.get("data.txt.gz")))) {
lines.forEach(line -> {
try {
out.write(line.getBytes());
out.write('\n');
} catch (IOException e) {
e.printStackTrace();
}
});
}
// 读取gz压缩文件并解压缩
try (GZIPInputStream in = new GZIPInputStream(Files.newInputStream(Paths.get("data.txt.gz")))) {
byte[] buffer = new byte[1024];
StringBuilder sb = new StringBuilder();
int read;
while ((read = in.read(buffer)) != -1) {
sb.append(new String(buffer, 0, read));
}
String content = sb.toString();
System.out.println(content);
}
// 从数组中创建Stream并输出每个元素的平方
int[] numbers = {1, 2, 3, 4, 5};
Arrays.stream(numbers)
.map(n -> n * n)
.forEach(System.out::println);
// 从List中创建Stream并输出满足条件的元素
List<String> fruits = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");
fruits.stream()
.filter(fruit -> fruit.startsWith("a") || fruit.startsWith("c"))
.forEach(System.out::println);
}
}
```
以上代码实现了以下功能:
1. 读取本地的data.txt文件,并过滤掉空行,将过滤后的内容写入到新文件data_copy.txt中;
2. 将过滤后的内容转为大写并写入到新文件data_uppercase.txt中;
3. 将过滤后的内容写入到gz压缩文件data.txt.gz中;
4. 读取gz压缩文件data.txt.gz并解压缩,输出解压缩后的内容;
5. 从数组中创建Stream并输出每个元素的平方;
6. 从List中创建Stream并输出满足条件的元素。
以上代码演示了Java Stream流的常用操作,包括创建Stream、过滤、映射、收集等操作,以及与文件和压缩文件的交互。