free mobile sex java_Java 8中Stream API的这些奇技淫巧!你都Get到了吗?

free mobile sex java_Java 8中Stream API的这些奇技淫巧!你都Get到了吗?原标题:Java8中StreamAPI的这些奇技淫巧!你都Get到了吗?作者:我是你的小眼睛儿链接:https://www.jianshu.com/p/9fe8632d0bc2Stream简介1、Java8引入了全新的StreamAPI。这里的Stream和I/O流不同,它更像具有Iterable的集合类,但行为和集合类又有所不同。2、stream是对集合对象功能的增强,它专注于对集合对象…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

原标题:Java 8中Stream API的这些奇技淫巧!你都Get到了吗?

作者:我是你的小眼睛儿

链接:https://www.jianshu.com/p/9fe8632d0bc2

Stream简介

1、Java 8引入了全新的Stream API。这里的Stream和I/O流不同,它更像具有Iterable的集合类,但行为和集合类又有所不同。

2、stream是对集合对象功能的增强,它专注于对集合对象进行各种非常便利、高效的聚合操作,或者大批量数据操作。

3、只要给出需要对其包含的元素执行什么操作,比如 “过滤掉长度大于 10 的字符串”、“获取每个字符串的首字母”等,Stream 会隐式地在内部进行遍历,做出相应的数据转换。

为什么要使用Stream

1、函数式编程带来的好处尤为明显。这种代码更多地表达了业务逻辑的意图,而不是它的实现机制。易读的代码也易于维护、更可靠、更不容易出错。

2、高端

实例数据源

publicclassData{

privatestaticList list= null;

static{

PersonModel wu = newPersonModel( “wu qi”, 18, “男”);

PersonModel zhang = newPersonModel( “zhang san”, 19, “男”);

PersonModel wang = newPersonModel( “wang si”, 20, “女”);

PersonModel zhao = newPersonModel( “zhao wu”, 20, “男”);

PersonModel chen = newPersonModel( “chen liu”, 21, “男”);

list= Arrays.asList(wu, zhang, wang, zhao, chen);

}

publicstaticList getData {

returnlist;

}

}

Filter

1、遍历数据并检查其中的元素时使用。

2、filter接受一个函数作为参数,该函数用Lambda表达式表示。

1e73edd62436d91acf114cc733251a2b.png

/**

* 过滤所有的男性

*/

publicstaticvoidfiterSex(){

List data = Data.getData;

//old

List temp= newArrayList<>;

for(PersonModel person:data) {

if( “男”. equals(person.getSex)){

temp. add(person);

}

}

System. out.println(temp);

//new

List collect = data

.stream

.filter(person -> “男”. equals(person.getSex))

.collect(toList);

System. out.println(collect);

}

/**

* 过滤所有的男性 并且小于20岁

*/

publicstaticvoidfiterSexAndAge(){

List data = Data.getData;

//old

List temp= newArrayList<>;

for(PersonModel person:data) {

if( “男”. equals(person.getSex)&&person.getAge< 20){

temp. add(person);

}

}

//new 1

List collect = data

.stream

.filter(person -> {

if( “男”. equals(person.getSex)&&person.getAge< 20){

returntrue;

}

returnfalse;

})

.collect(toList);

//new 2

List collect1 = data

.stream

.filter(person -> ( “男”. equals(person.getSex)&&person.getAge< 20))

.collect(toList);

}

Map

1、map生成的是个一对一映射,for的作用

2、比较常用

3、而且很简单

a08ca6c67c4b03deb80bfe11df2d6a97.png

/**

* 取出所有的用户名字

*/

publicstaticvoid getUserNameList{

List data = Data.getData;

//old

List list= newArrayList<>;

for(PersonModel persion:data) {

list.add(persion.getName);

}

System.out.println( list);

//new 1

List collect = data.stream.map(person -> person.getName).collect(toList);

System.out.println(collect);

//new 2

List collect1 = data.stream.map(PersonModel::getName).collect(toList);

System.out.println(collect1);

//new 3

List collect2 = data.stream.map(person -> {

System.out.println(person.getName);

returnperson.getName;

}).collect(toList);

}

FlatMap

1、顾名思义,跟map差不多,更深层次的操作

2、但还是有区别的

3、map和flat返回值不同

4、Map 每个输入元素,都按照规则转换成为另外一个元素。

还有一些场景,是一对多映射关系的,这时需要 flatMap。

5、Map一对一

6、Flatmap一对多

7、map和flatMap的方法声明是不一样的

(1) Stream map(Function mapper);

(2) Stream flatMap(Function> mapper);

(3) map和flatMap的区别:我个人认为,flatMap的可以处理更深层次的数据,入参为多个list,结果可以返回为一个list,而map是一对一的,入参是多个list,结果返回必须是多个list。通俗的说,如果入参都是对象,那么flatMap可以操作对象里面的对象,而map只能操作第一层。

b49dab4b904586af91a16269a8c00c50.png

publicstaticvoid flatMapString {

List data = Data.getData;

//返回类型不一样

List collect = data.stream

.flatMap(person -> Arrays.stream(person.getName.split( ” “))).collect(toList);

List> collect1 = data.stream

.map(person -> Arrays.stream(person.getName.split( ” “))).collect(toList);

//用map实现

List collect2 = data.stream

.map(person -> person.getName.split( ” “))

.flatMap(Arrays::stream).collect(toList);

//另一种方式

List collect3 = data.stream

.map(person -> person.getName.split( ” “))

.flatMap(str -> Arrays.asList(str).stream).collect(toList);

}

Reduce

1、感觉类似递归

2、数字(字符串)累加

3、个人没咋用过

cd4d2ea98b5ea0738111ff13f57544ee.png

public static void reduceTest{

//累加,初始化值是 10

Integer reduce = Stream. of( 1, 2, 3, 4)

.reduce( 10, (count, item)->{

System.out.println( “count:”+count);

System.out.println( “item:”+item);

returncount + item;

} );

System.out.println(reduce);

Integer reduce1 = Stream. of( 1, 2, 3, 4)

.reduce( 0, (x, y)->x + y);

System.out.println(reduce1);

String reduce2 = Stream. of( “1”, “2”, “3”)

.reduce( “0”, (x, y)->(x + “,”+ y));

System.out.println(reduce2);

}

Collect

1、collect在流中生成列表,map,等常用的数据结构

2、toList

3、toSet

4、toMap

5、自定义

/**

* toList

*/

publicstaticvoid toListTest{

List data = Data.getData;

List collect = data.stream

.map(PersonModel::getName)

.collect(Collectors.toList);

}

/**

* toSet

*/

publicstaticvoid toSetTest{

List data = Data.getData;

Set collect = data.stream

.map(PersonModel::getName)

.collect(Collectors.toSet);

}

/**

* toMap

*/

publicstaticvoid toMapTest{

List data = Data.getData;

Map collect = data.stream

.collect(

Collectors.toMap(PersonModel::getName, PersonModel::getAge)

);

data.stream

.collect(Collectors.toMap(per->per.getName, value->{

returnvalue+ “1”;

}));

}

/**

* 指定类型

*/

publicstaticvoid toTreeSetTest{

List data = Data.getData;

TreeSet collect = data.stream

.collect(Collectors.toCollection(TreeSet::new));

System.out.println(collect);

}

/**

* 分组

*/

publicstaticvoid toGroupTest{

List data = Data.getData;

Map> collect = data.stream

.collect(Collectors.groupingBy(per -> “男”.equals(per.getSex)));

System.out.println(collect);

}

/**

* 分隔

*/

publicstaticvoid toJoiningTest{

List data = Data.getData;

String collect = data.stream

.map(personModel -> personModel.getName)

.collect(Collectors.joining( “,”, “{“, “}”));

System.out.println(collect);

}

/**

* 自定义

*/

publicstaticvoid reduce{

List collect = Stream.of( “1”, “2”, “3”).collect(

Collectors.reducing( newArrayList, x -> Arrays.asList(x), (y, z) -> {

y.addAll(z);

returny;

}));

System.out.println(collect);

}

Optional

1、Optional 是为核心类库新设计的一个数据类型,用来替换 null 值。

2、人们对原有的 null 值有很多抱怨,甚至连发明这一概念的Tony Hoare也是如此,他曾说这是自己的一个“价值连城的错误”

3、用处很广,不光在lambda中,哪都能用

4、Optional.of(T),T为非空,否则初始化报错

5、Optional.ofNullable(T),T为任意,可以为空

6、isPresent,相当于 !=null

7、ifPresent(T), T可以是一段lambda表达式 ,或者其他代码,非空则执行

publicstaticvoid main(String[] args) {

PersonModel personModel= newPersonModel;

//对象为空则打出 –

Optional o = Optional.of(personModel);

System.out.println(o.isPresent?o.get: “-“);

//名称为空则打出 –

Optional name = Optional.ofNullable(personModel.getName);

System.out.println(name.isPresent?name.get: “-“);

//如果不为空,则打出xxx

Optional.ofNullable( “test”).ifPresent(na->{

System.out.println(na+ “ifPresent”);

});

//如果空,则返回指定字符串

System.out.println(Optional.ofNullable( null).orElse( “-“));

System.out.println(Optional.ofNullable( “1”).orElse( “-“));

//如果空,则返回 指定方法,或者代码

System.out.println(Optional.ofNullable( null).orElseGet(->{

return”hahah”;

}));

System.out.println(Optional.ofNullable( “1”).orElseGet(->{

return”hahah”;

}));

//如果空,则可以抛出异常

System.out.println(Optional.ofNullable( “1”).orElseThrow(->{

thrownewRuntimeException( “ss”);

}));

// Objects.requireNonNull(null,”is null”);

//利用 Optional 进行多级判断

EarthModel earthModel1 = newEarthModel;

//old

if(earthModel1!= null){

if(earthModel1.getTea!= null){

//…

}

}

//new

Optional.ofNullable(earthModel1)

.map(EarthModel::getTea)

.map(TeaModel::getType)

.isPresent;

// Optional earthModel = Optional.ofNullable(new EarthModel);

// Optional> personModels = earthModel.map(EarthModel::getPersonModels);

// Optional> stringStream = personModels.map(per -> per.stream.map(PersonModel::getName));

//判断对象中的list

Optional.ofNullable( newEarthModel)

.map(EarthModel::getPersonModels)

.map(pers->pers

.stream

.map(PersonModel::getName)

.collect(toList))

.ifPresent(per-> System.out.println(per));

List models=Data.getData;

Optional.ofNullable(models)

.map(per -> per

.stream

.map(PersonModel::getName)

.collect(toList))

.ifPresent(per-> System.out.println(per));

}

并发

1、stream替换成parallelStream或 parallel

2、输入流的大小并不是决定并行化是否会带来速度提升的唯一因素,性能还会受到编写代码的方式和核的数量的影响

3、影响性能的五要素是:数据大小、源数据结构、值是否装箱、可用的CPU核数量,以及处理每个元素所花的时间

//根据数字的大小,有不同的结果

privatestaticintsize= 10000000;

publicstaticvoidmain(String[] args){

System. out.println( “———–List———–“);

testList;

System. out.println( “———–Set———–“);

testSet;

}

/**

* 测试list

*/

publicstaticvoidtestList(){

List list = newArrayList<>(size);

for(Integer i = 0; i < size; i++) {

list. add( newInteger(i));

}

List temp1 = newArrayList<>(size);

//老的

longstart=System.currentTimeMillis;

for(Integer i: list) {

temp1. add(i);

}

System. out.println(+System.currentTimeMillis-start);

//同步

longstart1=System.currentTimeMillis;

list.stream.collect(Collectors.toList);

System. out.println(System.currentTimeMillis-start1);

//并发

longstart2=System.currentTimeMillis;

list.parallelStream.collect(Collectors.toList);

System. out.println(System.currentTimeMillis-start2);

}

/**

* 测试set

*/

publicstaticvoidtestSet(){

List list = newArrayList<>(size);

for(Integer i = 0; i < size; i++) {

list. add( newInteger(i));

}

Set temp1 = newHashSet<>(size);

//老的

longstart=System.currentTimeMillis;

for(Integer i: list) {

temp1. add(i);

}

System. out.println(+System.currentTimeMillis-start);

//同步

longstart1=System.currentTimeMillis;

list.stream.collect(Collectors.toSet);

System. out.println(System.currentTimeMillis-start1);

//并发

longstart2=System.currentTimeMillis;

list.parallelStream.collect(Collectors.toSet);

System. out.println(System.currentTimeMillis-start2);

}

调试

1、list.map.fiter.map.xx 为链式调用,最终调用collect(xx)返回结果

2、分惰性求值和及早求值

3、判断一个操作是惰性求值还是及早求值很简单:只需看它的返回值。如果返回值是 Stream,那么是惰性求值;如果返回值是另一个值或为空,那么就是及早求值。使用这些操作的理想方式就是形成一个惰性求值的链,最后用一个及早求值的操作返回想要的结果。

4、通过peek可以查看每个值,同时能继续操作流

privatestaticvoidpeekTest{

List data = Data.getData;

//peek打印出遍历的每个per

data.stream. map(per->per.getName).peek(p->{

System.out.println(p);

}).collect(toList);

}

●编号1057,输入编号直达本文

责任编辑:

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/181736.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号