hashmap用stream流操作_Java Stream流操作

时间:2025-03-23 16:38:53

Java流操作概览

在处理集合时如果需要遍历元素, 并且对每一个元素进行某项操作, 就可以使用Java的流操作

(1) 流操作不更改原始集合, 会生成一个新的集合

(2) 流操作是懒惰执行的, 直到需要拿到执行结果的时候才会执行

import ;

import ;

import ;

import ;

public class streamTest {

public static void main(String[] args) {

// filter

// Arrays

String[] a = {"a", "bb", "ccc", "dd"};

long aCount = (a).filter(x -> () >= 2).count();

(aCount);

List aa = (a).filter(x -> () >= 2).collect(());

(aa);

// ArrayList

ArrayList b = new ArrayList<>(("a", "bb", "ccc", "dd"));

long bCount = ().filter(x -> () >= 2).count();

(bCount);

List bb = ().filter(x -> () >= 2).collect(());

(bb);

// 使用parallelStream可以并行

List bbb = ().filter(x -> () >= 2).collect(());

(bbb);

}

}

Java流的创建

Collection有stream接口直接创建流, 数组可以通过静态方法, 或者可以指定数组的开始和结束位置

// 几种创建流的方式

// 数组

Stream aStream = (1, 2, 3, 4, 5);

List aList = (x -> x > 3).collect(());

(aList); // [4, 5]

Stream bStream = ("1,2,3,4,5".split(","));

List bList = (x -> (x)).filter(x -> x >3).collect(());

(bList); // [4, 5]

Stream cStream = ("1,2,3,4,5".split(","));

List cList = (x -> (x)).filter(x -> x >3).collect(());

(cList); // [4, 5]

// 指定数组的开始结束索引位置

Integer[] tmpArray = {1, 2 ,3 ,4, 5};

Stream dStream = (tmpArray, 1, 3);

(x -> (x)); // 2, 3

// 集合Collection

Stream eStream = (1, 2, 3, 4, 5).stream();

List eList = (x -> x > 3).collect(());

(eList); // [4, 5]

转换操作 filter, map, flatMap

流的转换会产生一个新的流, 转换操作需要指定转换函数, 可以使用lambda表达式或者方法引用::

// map

// lambda表达式

List fList = (1, 2, 3, 4, 5).stream().map(x -> x + 1).collect(());

(fList);

// 方法引用::

List gList = (1, 2, 3, 4, 5).stream().map(streamTest::inc1).collect(());

(gList);

private static Integer inc1(Integer value) {

return value + 1;

}

抽取子流 limit和skip

// 抽取子流 limit skip

List iList = (1, 2, 3, 4, 5);

().limit(3).forEach(x -> (x)); // 123

();

().skip(1).forEach(x -> (x)); // 2345

获取无限流

// 获取无限流

Stream jStream = (() -> 1); // 生成全是1的无限流

// (x -> (x));

Stream kStream = (Math::random).limit(10); // 生成10个[0,1)之间的随机小数

(x -> (x));

排序

// 排序

List lList = (1, 3, 5, 2, 4);

List mList = ().sorted().collect(());

(mList); // [1, 2, 3, 4, 5]

List nList = ().sorted(()).collect(());

(nList); // [5, 4, 3, 2, 1]

收集结果

可以将流操作之后的结果收集为ArrayList,HashSet,HashMap,String。

Stream stream = ("2", "2", "3");

List list = (s -> s + "_").collect(());

Set set = (s -> s + s).collect(());

Map map = (s -> s + s).collect((k -> k, v -> v + "_", (o, n) -> n));

String string = ((",")); // 2,2,3

在sql语句in条件查询使用stream收集为,分割字符串:

conn = ();

stmt = ();

String param = ().map(s -> "\"" + s + "\"").collect((","));

ResultSet rs = (("select feature2 from table where feature1 in (%s) and status='Y'", param));

while (()) {

// TODO

}

第一个参数是获得key,第二个参数是获得value,第三个参数是解决key冲突的处理方式

public static void streamToMao() {

List a = new ArrayList<>(("a", "b", "c", "d"));

Map b = ().collect((s -> s, s -> s, (o, n) -> n));

Map c = ().collect((s -> s + "_" + s, s -> s, (o, n) -> n));

// 方法引用

Map d = ().collect((test2::getString, v -> v, (o, n) -> n));;

// 等同于

Map e = new HashMap<>();

for (String item: a) {

(item + "_" + item, item);

}

}

使用基本类型流

使用包装器Stream<>是低效的, 流库有专门的类型IntStream, LongStream, DoubleStream, 无需使用包装器

// 使用基本类型流

IntStream intStream1 = (1, 2, 3, 4, 5);

(::print);

DoubleStream doubleStream1 = (1.0, 2.0, 3.0);

(::print);

多维数组的流操作

// 多维数组

Integer[][] multiArray = {{1, 2, 3}, {2, 3, 4}};

Stream multiStream = (multiArray);

// (x -> ((x)));

// 每个元素求长度

// List xLength = (x -> ).collect(());

// (xLength); // [3, 3]

// 每个元素求最大

List xMax = (x -> ((x))).collect(());

(xMax); // [3, 4]