Java中Stream流的用法及使用备忘

Java中Stream流的用法及使用备忘

一. 流的常用创建方法

1-1 使用Collection下的 stream() 和 parallelStream() 方法

List<String> list = new ArrayList<>();
Stream<String> stream = list.stream(); //获取一个顺序流
Stream<String> parallelStream = list.parallelStream(); //获取一个并行流

1-2 使用Arrays 中的 stream() 方法,将数组转成流

Integer[] nums = new Integer[10];
Stream<Integer> stream = Arrays.stream(nums);

1-3 使用Stream中的静态方法:of()、iterate()、generate()

// 1. of()
Stream<Integer> stream = Stream.of(1,2,3,4,5,6);
// 2. iterate()
Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 2).limit(6);
stream2.forEach(System.out::println); // 0 2 4 6 8 10
// 3. generate()
Stream<Double> stream3 = Stream.generate(Math::random).limit(2);
stream3.forEach(System.out::println);

1-4 使用 BufferedReader.lines() 方法,将每行内容转成流

BufferedReader reader = new BufferedReader(new FileReader("F:\\test_stream.txt"));
Stream<String> lineStream = reader.lines();
lineStream.forEach(System.out::println);

1-5 使用 Pattern.splitAsStream() 方法,将字符串分隔成流

Pattern pattern = Pattern.compile(",");
Stream<String> stringStream = pattern.splitAsStream("a,b,c,d");
stringStream.forEach(System.out::println);

二、流的中间操作

// 1. 筛选与切片
filter:过滤流中的某些元素
limit skip distinct sorted 都是有状态操作,这些操作只有拿到前面处理后的所有元素之后才能继续下去。
limit(n):获取前n个元素
skip(n):跳过前n元素,配合limit(n)可实现分页
distinct:通过流中元素的 hashCode()equals() 去除重复元素
// 2. 映射
map:接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
flatMap:接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。
// 3. 消费 peek , 类似map,	
//		map接收的是一个Function表达式,有返回值;
//		而peek接收的是Consumer表达式,没有返回值。
// 4. 排序
sorted():自然排序,流中元素需实现Comparable接口
sorted(Comparator com):定制排序,自定义Comparator排序器
// 5. 

2-1 筛选、去重与切片:filter、distinct、skip、limit

// 实例:集合内元素>5,去重,跳过前两位,取剩下元素的两个返回为新集合
Stream<Integer> stream = Stream.of(6, 4, 6, 7, 3, 9, 8, 10, 12, 14, 14);
Stream<Integer> newStream = stream.filter(s -> s > 5) //6 6 7 9 8 10 12 14 14
 .distinct() //6 7 9 8 10 12 14
 .skip(2) //9 8 10 12 14
 .limit(2); //9 8
newStream.forEach(System.out::println);

2-2 映射:map、flatMap

// 1. Map可以看成一个转换器,传入一个对象,返回新的对象
// map的使用实例   stream.map(x->x.getId());
List<String> list = Arrays.asList("a,b,c", "1,2,3");
// 去掉字符串中所有的,
List<String> collect = list.stream().map(s -> s.replaceAll(",", "")).collect(Collectors.toList());
// collect集合内容为:{abc,123}
System.out.println(collect);

// 2. flatMap   效果:结果展平 ,即把嵌套集合,按照子集合的形式,统一放入到新的一个集合中	
//				接收一个函数作为参数,将流中的每个值都换成另一个流,
//				然后把所有流连接成一个流。
Stream<String> stringStream = list.stream().flatMap(s -> {
	// 将字符串以,分割后得到一个字符串数组
	String[] split = s.split(",");
	// 然后将每个字符串数组对应流返回,flatMap会自动把返回的所有流连接成一个流
	Stream<String> stream = Arrays.stream(split);
	return stream;
});
// stringStream.collect(Collectors.toList())的集合内容为:{a,b,c,1,2,3}
System.out.println(stringStream.collect(Collectors.toList()));

2-3 归约:统计-计算-逻辑处理:reduce

//	说明:reduce看似效果和map相似,
//			但reduce返回的是函数经过执行运算后的结果,
//			而map返回的是处理后新的集合
List<String> memberNames = new ArrayList<>();
memberNames.add("Amitabh");
memberNames.add("Shekhar");
memberNames.add("Aman");
memberNames.add("Rahul");
memberNames.add("Shahrukh");
memberNames.add("Salman");
memberNames.add("Yana");
memberNames.add("Lokesh");

// 将集合中的元素按照#连接成字符串,并返回放置在Optional<String>中
Optional<String> reduced = memberNames.stream()
        .reduce((s1,s2) -> s1 + "#" + s2);
// 有值则取出打印显示        
reduced.ifPresent(System.out::println);  
// 输出内容:   Amitabh#Shekhar#Aman#Rahul#Shahrukh#Salman#Yana#Lokesh
// 计算统计实例:
    /**
     * T reduce(T identity, BinaryOperator<T> accumulator);
     * identity:它允许用户提供一个循环计算的初始值。
     * accumulator:计算的累加器,
     */
    private static void testReduce() {
        //T reduce(T identity, BinaryOperator<T> accumulator);
        System.out.println("给定个初始值,求和");
        System.out.println(Stream.of(1, 2, 3, 4).reduce(100, (sum, item) -> sum + item));
        System.out.println(Stream.of(1, 2, 3, 4).reduce(100, Integer::sum));
		// 输出:110

        System.out.println("给定个初始值,求min");
        System.out.println(Stream.of(1, 2, 3, 4).reduce(100, (min, item) -> Math.min(min, item)));
        System.out.println(Stream.of(1, 2, 3, 4).reduce(100, Integer::min));
        // 输出:1

        System.out.println("给定个初始值,求max");
        System.out.println(Stream.of(1, 2, 3, 4).reduce(100, (max, item) -> Math.max(max, item)));
        System.out.println(Stream.of(1, 2, 3, 4).reduce(100, Integer::max));
        // 输出:100
 
        //Optional<T> reduce(BinaryOperator<T> accumulator);
        // 注意返回值,上面的返回是T,泛型,传进去啥类型,返回就是啥类型。
        // 下面的返回的则是Optional类型
        System.out.println("无初始值,求和");
        System.out.println(Stream.of(1, 2, 3, 4).reduce(Integer::sum).orElse(0)); 
        // 输出:10
        Integer sum=Stream.of(1, 2, 3, 4).reduce((x,y)->x+y).get();
        System.out.println(sum);  // 输出:10
        
        System.out.println("无初始值,求max");
        System.out.println(Stream.of(1, 2, 3, 4).reduce(Integer::max).orElse(0));
        // 输出:4 
        
        System.out.println("无初始值,求min");
        System.out.println(Stream.of(1, 2, 3, 4).reduce(Integer::min).orElse(0));
        // 输出:1
 
    }

2-4 排序:sorted

// 按照默认字典顺序排序
stream.sorted(); 
// 按照sortNo排序
stream.sorted((x,y)->Integer.compare(x.getSortNo(),y.getSortNo()));
2-4-1 函数式接口排序
// 正向排序(默认)
pendingPeriod.stream().sorted(Comparator.comparingInt(ReservoirPeriodResult::getId));
// 逆向排序
pendingPeriod.stream().sorted(Comparator.comparingInt(ReservoirPeriodResult::getId).reversed());
2-4-2 LocalDate 和 LocalDateTime 排序
// 准备测试数据
Stream<DateModel> stream = Stream.of(new DateModel(LocalDate.of(2020, 1, 1))
, new DateModel(LocalDate.of(2021, 1, 1)), new DateModel(LocalDate.of(2022, 1, 1)));

// 正向排序(默认)
stream.sorted(Comparator.comparing(DateModel::getLocalDate))
.forEach(System.out::println);
// 逆向排序
stream.sorted(Comparator.comparing(DateModel::getLocalDate).reversed())
.forEach(System.out::println);

三. 流的终止操作 allMatch,noneMatch,anyMatch,findFirst,findAny,count,max,min

// 匹配和聚合
allmatch,noneMatch,anyMatch用于对集合中对象的某一个属性值是否存在判断。
	allMatch全部符合该条件返回true,
	noneMatch全部不符合该断言返回true
	anyMatch 任意一个元素符合该断言返回true
// 实例:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
boolean allMatch = list.stream().allMatch(e -> e > 10); //false
boolean noneMatch = list.stream().noneMatch(e -> e > 10); //true
boolean anyMatch = list.stream().anyMatch(e -> e > 4); //true

// 其他一些方法
findFirst:返回流中第一个元素
		String firstMatchedName = memberNames.stream()
	            .filter((s) -> s.startsWith("L"))
	            .findFirst().get();
findAny:返回流中的任意元素
count:返回流中元素的总个数
	long totalMatched = memberNames.stream()
	    .filter((s) -> s.startsWith("A"))
	    .count();
max:返回流中元素最大值
min:返回流中元素最小值

3-1 普通收集 – 收集为List

// 默认返回的类型为ArrayList,可通过Collectors.toCollection(LinkedList::new)
//		显示指明使用其它数据结构作为返回值容器。
List<String> collect = stream.collect(Collectors.toList());

// 由集合创建流的收集需注意:仅仅修改流字段中的内容,没有返回新类型,
//		如下操作直接修改原始集合,无需处理返回值。
userVos.stream().map(e -> e.setDeptName(hashMap.get(e.getDeptId())))
.collect(Collectors.toList());

// 收集偶数集合的实例:
List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i< 10; i++){
    list.add(i);
}
Stream<Integer> stream = list.stream();
List<Integer> evenNumbersList = stream.filter(i -> i%2 == 0)
                                       .collect(Collectors.toList());
System.out.print(evenNumbersList);

3-2 普通收集 – 收集流为数组(Array)

// list 为 {1,2,3,.....100}
Stream<Integer> stream = list.stream();
Integer[] evenNumbersArr = stream.filter(i -> i%2 == 0).toArray(Integer[]::new);

3-3 普通收集 – 收集为Set

// 默认返回类型为HashSet,可通过Collectors.toCollection(TreeSet::new)
//		显示指明使用其它数据结构作为返回值容器。
Set<String> collect = stream.collect(Collectors.toSet());

3-4 高级收集 – 收集为Map

//	默认返回类型为HashMap,可通过Collectors.toCollection(LinkedHashMap::new)
//		显示指明使用其它数据结构作为返回值容器

// 测试实体类
@Data
public class Entity {
    private Integer id;
    private String name;
}

// 模拟从数据库中查询批量的数据
List<Entity> entityList = Stream.of(new Entity(1,"A"), 
new Entity(2,"B"), new Entity(3,"C")).collect(Collectors.toList());

// 将集合数据转化成id与name的Map
Map<Integer, String> hashMap = entityList.stream()
.collect(Collectors.toMap(Entity::getId, Entity::getName));

3-5 高级收集 – 分组收集

// 默认使用List作为分组后承载容器
Map<Integer, List<XUser>> hashMap = 
xUsers.stream().collect(Collectors.groupingBy(XUser::getDeptId));
 
// 显示指明使用List作为分组后承载容器
Map<Integer, List<XUser>> hashMap = 
xUsers.stream().collect(Collectors.groupingBy(XUser::getDeptId, Collectors.toList()));

// 映射后再分组
Map<Integer, List<String>> hashMap = xUsers.stream().collect(Collectors.groupingBy(XUser::getDeptId,Collectors.mapping(XUser::getUserName,Collectors.toList())));

四. Steam拓展

4-1 集合与对象互转

/**
 * 将单个对象转化为集合
 *
 * @param t   对象实例
 * @param <T> 对象类型
 * @param <C> 集合类型
 * @return 包含对象的集合实例
 */
public static <T, C extends Collection<T>> Collection<T> toCollection(T t) {
    return toCollection(t, ArrayList::new);
}

/**
 * 用户自定义返回的集合实例类型:  将单个对象转化为集合
 *
 * @param t        对象实例
 * @param supplier 集合工厂
 * @param <T>      对象类型
 * @param <C>      集合类型
 * @return 包含对象的集合实例
 */
public static <T, C extends Collection<T>> Collection<T> toCollection(T t, Supplier<C> supplier) {
    return Stream.of(t).collect(Collectors.toCollection(supplier));
}

4-2 集合转对象

/**
 * 取出集合中第一个元素
 *
 * @param collection 集合实例
 * @param <E>        集合中元素类型
 * @return 泛型类型
 */
public static <E> E toObject(Collection<E> collection) {
    // 处理集合空指针异常
    Collection<E> coll = Optional.ofNullable(collection).orElseGet(ArrayList::new);
    // 此处可以对流进行排序,然后取出第一个元素
    return coll.stream().findFirst().orElse(null);
}

4-3 Java Steam中的并发操作实例

List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i< 10; i++){
    list.add(i);
}
Stream<Integer> stream = list.parallelStream();    // 创建并发流

Integer[] evenNumbersArr = stream.filter(i -> i%2 == 0).toArray(Integer[]::new);
System.out.print(evenNumbersArr);   // 打印出的偶数为无规则排序的
<think>嗯,用户想了解C语言中与“steam”相关的用法或问题,不过可能打错了,应该是“stream”。首先需要确认用户的需求是否正确,但根据常见的C语言术语,确实有标准输入输出的概念,比如stdin、stdout、stderr,这些属于文件指针类型,即FILE*。 接下来,我需要整理C语言中与相关的基本概念和使用方法。根据引用内容,用户提到的引用[3]指出stdin、stdout、stderr是FILE*类型,称为文件指针。所以需要围绕这三个标准进行解释,并结合示例代码说明使用方法。 用户可能对如何操作这些感兴趣,比如读取输入、输出到控制台或文件。例如,使用fgetc从stdin读取字符,或者用fprintf输出到stdout。同时,可能需要解释如何重定向这些或者处理错误。 另外,用户提供的引用中提到了fgetc、fscanf、fopen等函数的使用,这些都可以结合到的操作中。比如,从文件中读取数据到结构体,或者处理键盘输入后的缓冲区问题。需要确保示例代码正确,符合C语言的语法,并且注释清晰。 可能需要注意用户对“steam”的拼写错误,正确应为“stream”,所以在回答中需要明确指出这一点,避免混淆。同时,生成的相关问题要围绕stream的应用、常见问题以及与其他函数的区别展开,帮助用户深入理解。 最后,确保回答结构清晰,分点说明基本概念、常用函数、示例和注意事项,并引用提供的参考文献来支持内容。检查示例代码的正确性,比如使用正确的函数名和参数,避免语法错误。例如,引用[2]中的fgetchar可能需要更正为getchar或fgetc(stdin),因为标准库中没有fgetchar函数,这可能是用户的笔误,需要修正并说明。</think>关于C语言中**标准stream)**的使用方法及常见问题,以下是详细说明: --- ### 一、标准的基本概念 C语言中定义了3个标准,通过`FILE*`类型的文件指针操作: 1. **stdin**:标准输入(默认关联键盘)[^3] 2. **stdout**:标准输出(默认关联屏幕) 3. **stderr**:标准错误(默认关联屏幕)[^3] --- ### 二、常用函数及示例 #### 1. 字符级操作 ```c // 从stdin读取字符(等效于getchar()) char ch = fgetc(stdin); // 需处理回车残留问题[^2] fputc(ch, stdout); // 输出到屏幕 // 示例:清空输入缓冲区 while ((ch = fgetc(stdin)) != '\n' && ch != EOF); ``` #### 2. 行级操作 ```c char buffer[100]; fgets(buffer, 100, stdin); // 从键盘读取一行 fputs(buffer, stdout); // 输出到屏幕 ``` #### 3. 格式化I/O ```c struct S s = {0}; fscanf(stdin, "%f-%c-%d", &s.f, &s.c, &s.n); // 从键盘读取结构化数据[^1] fprintf(stdout, "输出:%f-%c-%d\n", s.f, s.c, s.n); ``` #### 4. 错误处理 ```c FILE* pf = fopen("data.txt", "r"); if (!pf) { fprintf(stderr, "错误:文件打开失败\n"); // 通过stderr输出错误信息 exit(EXIT_FAILURE); } ``` --- ### 三、重定向示例 ```c freopen("output.log", "w", stdout); // 将stdout重定向到文件 printf("这行内容将写入文件"); // 不再输出到屏幕 ``` --- ### 四、注意事项 1. **缓冲区管理**:行缓冲(stdout)vs 无缓冲(stderr) 2. **安全性**:优先使用`fgets()`替代`gets()`避免缓冲区溢出 3. **错误检查**:所有操作后应检查`ferror()`和`feof()` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值