当前位置: 首页 > news >正文

社区微网站建设需求分析百度手机助手官方正版

社区微网站建设需求分析,百度手机助手官方正版,济宁网站建设top,thinkphp做网站有什么好处获取Stream流的方式 java.util.stream.Stream 是Java 8新加入的流接口。(并不是一个函数式接口) 获取一个流非常简单,有以下几种常用的方式: 所有 Collection 集合都可通过 stream 默认方法获取流(顺序流)…

获取Stream流的方式
java.util.stream.Stream 是Java 8新加入的流接口。(并不是一个函数式接口)
获取一个流非常简单,有以下几种常用的方式:

  • 所有 Collection 集合都可通过 stream 默认方法获取流(顺序流)
  • 所有 Collection 集合都可通过parallelStream获取并行流
  • Stream 接口的静态方法 of 可以获取数组对应的流。
  • Arrays的静态方法stream也可以获取流

根据Collection获取流

public static void main(String[] args) {List<String> list = new ArrayList<>();Stream<String> stream1 = list.stream();Set<String> set = new HashSet<>();Stream<String> stream2 = set.stream();Vector<String> vector = new Vector<>();// ...
}   

根据Map获取流

public static void main(String[] args) {Map<String, String> map = new HashMap<>();Stream<String> keyStream = map.keySet().stream();Stream<String> valueStream = map.values().stream();Stream<Map.Entry<String, String>> entryStream = map.entrySet().stream();
}

根据数组获取流

如果使用的不是集合或映射而是数组,由于数组对象不可能添加默认方法,所以 Stream 接口中提供了静态方法of ,使用很简单:

public static void main(String[] args) {//使用 Stream.ofString[] array = { "张无忌", "张翠山", "张三丰", "张一元" };Stream<String> stream = Stream.of(array);//使用Arrays的静态方法Arrays.stream(array)
}

Stream流中的常用方法

方法可分成两种:

  • 延迟方法:返回值类型仍然是 Stream 接口自身类型的方法,因此支持链式调用。(除终结方法外,其余方法均为延迟方法)
  • 终结方法:返回值类型不是 Stream 接口自身类型的方法,因此不再支持类似 StringBuilder 那样的链式调用。本节中,终结方法包括 count 和 forEach 方法。

1.forEach(终结方法)

用于遍历的方法,参数传入一个函数式接口:Consumer

public static void main(String[] args) {Stream<String> stream = Stream.of("张无忌", "张三丰", "周芷若");stream.forEach(name‐> System.out.println(name));
}

2.过滤:filter

可用于过滤。可通过 filter 方法将一个流转换成另一个子集流。

public static void main(String[] args) {//创建一个流Stream<String> stream = Stream.of( "刘德华", "张国荣", "彭于晏", "纳什", "吴彦祖");//对流中元素过滤,只要姓张的人Stream<String> stream2 = stream.filter(name -> {return name.startsWith("张");});//遍历过滤后的流stream2.forEach(name -> System.out.println(name));
}

3.映射(转换):map

如果需要将流中的元素映射到另一个流中,可用 map 方法。

该接口需要一个 Function 函数式接口参数

/*** stream流的map方法练习* map方法可以将流中的元素映射到另一个流中* map方法的参数是一个Function函数式接口*/@Testpublic void test(){//创建一个流,里面是字符串类型的整数Stream<String> stream1 = Stream.of("2", "32", "2", "33", "2");//把stream1流中的整数全部转成int类型Stream<Integer> stream2 = stream1.map((String s) -> {return Integer.parseInt(s);});//遍历stream2.forEach((i)-> System.out.println(i));}

4.统计个数:count(终结方法)

正如旧集合 Collection 当中的 size 方法一样,流提供 count 方法来数一数其中的元素个数:

该方法返回一个long值代表元素个数(不像旧集合那样是int值)。

public class Demo09StreamCount {public static void main(String[] args) {Stream<String> original = Stream.of("张无忌", "张三丰", "周芷若");//筛选姓张的Stream<String> result = original.filter(s ‐> s.startsWith("张"));//输出个数System.out.println(result.count()); // 2}
}

5.取用前几个(截取):limit

limit 方法可对流进行截取,只取用前n个。

参数是一个long型,如果集合当前长度大于参数则进行截取;否则不进行操作。

public class Demo10StreamLimit {public static void main(String[] args) {Stream<String> original = Stream.of("张无忌", "张三丰", "周芷若");//截取前两个Stream<String> result = original.limit(2);System.out.println(result.count()); // 2}
}

6.跳过前几个元素:skip

如果希望跳过前几个元素,可用 skip 方法获取一个截取后的新流:

如果流的当前长度大于n,则跳过前n个;否则将会得到一个长度为0的空流。

public class Demo11StreamSkip {public static void main(String[] args) {Stream<String> original = Stream.of("张无忌", "张三丰", "周芷若");//跳过前两个,返回一个新的流Stream<String> result = original.skip(2);System.out.println(result.count()); // 1}
}

7.组合(合并流):concat

如果有两个流,希望合并为一个流,可以用 Stream 接口的静态方法 concat :

public class Demo12StreamConcat {public static void main(String[] args) {Stream<String> streamA = Stream.of("张无忌");Stream<String> streamB = Stream.of("张翠山");//合并成一个新的流Stream<String> result = Stream.concat(streamA, streamB);}
}

8.筛选:distinct

去除流中重复元素(使用hashcode和equals方法来对比)

9.映射(打开后再转换):flatMap

内部传入一个Function函数式接口,跟map的区别就是这个会把流中的元素打开,再组合成一个新的流

// map和flatMap的练习
public class StreamDemo {@Testpublic void test(){List<String> list = Arrays.asList("aa","bb","cc","dd");// 练习1 (map) 输出的全是大写list.stream().map(s -> s.toUpperCase()).forEach(System.out::println);System.out.println("----------");// 练习2(map)流里还有流,需要两个遍历才行看到里面内容Stream<Stream<Character>> streamStream = list.stream().map(StreamDemo::fromStringToStream);streamStream.forEach(s -> {s.forEach(System.out::println);});System.out.println("---------");// 练习3(flatMap)流里还有流,使用flatMap可以直接把里面的流打开,一次遍历就可以了Stream<Character> characterStream = list.stream().flatMap(StreamDemo::fromStringToStream);characterStream.forEach(System.out::println);}/***  将字符串中的多个字符构成的集合转换为对应的stream* @param str* @return*/public static Stream<Character> fromStringToStream(String str){ArrayList<Character> list = new ArrayList();// 将字符串转成字符数组,并遍历加入list集合for(Character c : str.toCharArray()){list.add(c);}// 返回list集合的stream流return list.stream();}
}

10.自然排序:sorted

看下一条里的代码

11.定制排序:sorted(Comparator com)

 /*** 排序的练习*/@Testpublic void test2(){List<Integer> integers = List.of(124, 2, 15, 12, 51, -5, 5);// 按照自然排序integers.stream().sorted().forEach(System.out::println);System.out.println("---------");List<Integer> integers2 = List.of(124, 2, 15, 12, 51, -5, 5);// 定制排序(大到小),需要传入Comparator接口(如果流中的是引用类型,只能用定制排序)// 简写:integers2.stream().sorted((e1,e2) -> e2-e1).forEach(System.out::println);integers2.stream().sorted((e1,e2) -> {return e2-e1;}).forEach(System.out::println);}

12.检测匹配(终结方法):

返回一个Boolean值

是否全部匹配:allMatch

是否至少匹配一个:anyMatch

是否没有匹配的:noneMatch

 /*** 匹配的练习*/@Testpublic void test3(){List<Integer> integers = List.of(124, 2, 15, 12, 51, -5, 5);// 判断是否全部大于5boolean b = integers.stream().allMatch(i -> i > 5);// 结束输出falseSystem.out.println(b);System.out.println("-------");List<Integer> integers2 = List.of(124, 2, 15, 12, 51, -5, 5);// 检测是否匹配至少一个元素boolean b1 = integers2.stream().anyMatch(i -> i > 5);// 输出trueSystem.out.println(b1);System.out.println("-------");List<Integer> integers3 = List.of(124, 2, 15, 12, 51, -5, 5);// 检查是否没有匹配的元素boolean b2 = integers3.stream().noneMatch(i -> i > 1000);// 输出true,全部不匹配System.out.println(b2);}

13.查找元素(终结方法)

查找第一个元素:findFirst,返回Optional类型

查找其中一个元素:findAny,返回Optional类型

public void test4(){List<Integer> integers = List.of(124, 2, 15, 12, 51, -5, 5);// 输出第一个元素Optional<Integer> first = integers.stream().findFirst();// 输出结果是Optional[124]System.out.println(first);System.out.println("-------------");List<Integer> integers2 = List.of(124, 2, 15, 12, 51, -5, 5);// 返回其中一个元素Optional<Integer> any = integers2.stream().findAny();System.out.println(any);}

14.查找最大最小值(终结方法)

max(comparator c)

min(comparator c)

 /*** 查找最大最小值*/@Testpublic void test5(){List<Person> list = new ArrayList<>();list.add(new Person("马化腾",25,3000));list.add(new Person("李彦宏",27,2545));list.add(new Person("雷军",35,4515));list.add(new Person("马云",55,9877));//  查找年龄最大的人Optional<Person> max = list.stream().max((e1, e2) -> e1.getAge() - e2.getAge());// 返回马云,55岁年龄最大System.out.println(max.get());System.out.println("--------");

15.规约(终结方法)

reduce(T identity ,BinaryOperator) 第一个参数是初始值,第二个参数是一个函数式接口

reduce(BinaryOperator) 参数是一个函数式接口

/*** 归约的练习*/@Testpublic void test6(){List<Integer> integers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);// 求集合里数字的和(归约)reduce第一个参数是初始值。Integer sum = integers.stream().reduce(0, Integer::sum);System.out.println(sum);System.out.println("-------");List<Person> list = new ArrayList<>();list.add(new Person("马化腾",25,3000));list.add(new Person("李彦宏",27,2545));list.add(new Person("雷军",35,4515));list.add(new Person("马云",55,9877));// 求所有人的工资和(归约)// 不用方法引用写法:Optional<Integer> reduce = list.stream().map(person -> person.getSalary()).reduce((e1, e2) -> e1 + e2);Optional<Integer> reduce = list.stream().map(Person::getSalary).reduce(Integer::sum);// 输出Optional[19937]System.out.println(reduce);}

16.收集(终结方法)

collect(Collector c):将流转化为其他形式,接收一个Collector接口的实现

/*** 收集的练习*/@Testpublic void test7(){List<Person> list = new ArrayList<>();list.add(new Person("马化腾",25,3000));list.add(new Person("李彦宏",27,2545));list.add(new Person("雷军",35,4515));list.add(new Person("马云",55,9877));// 把年龄大于30岁的人,转成一个list集合List<Person> collect = list.stream().filter(person -> person.getAge() > 30).collect(Collectors.toList());// 遍历输出(输出雷军和马云)for (Person person : collect) {System.out.println(person);}System.out.println("----------");List<Person> list2 = new ArrayList<>();list2.add(new Person("马化腾",25,3000));list2.add(new Person("李彦宏",27,2545));list2.add(new Person("雷军",35,4515));list2.add(new Person("马云",55,9877));// 把姓马的人,转成Set集合Set<Person> set = list2.stream().filter(person -> person.getName().startsWith("马")).collect(Collectors.toSet());// 输出马云和马化腾set.forEach(System.out::println);}

17.迭代:iterate

可以使用Stream.iterate创建流值,即所谓的无限流。

//Stream.iterate(initial value, next value)Stream.iterate(0, n -> n + 1).limit(5).forEach(x -> System.out.println(x));

输出:

0
1
2
3
4   

18.查看:peek

peek接收的是一个Consumer函数,peek 操作会按照 Consumer 函数提供的逻辑去消费流中的每一个元素,同时有可能改变元素内部的一些属性

@Testpublic void test1(){List<String> collect = Stream.of("one", "two", "three", "four").filter(e -> e.length() > 3).peek(e -> System.out.println("查看一下刚过滤出的值:" + e)).map(String::toUpperCase).peek(e -> System.out.println("查看一下转大写之后的值:" + e)).collect(Collectors.toList());System.out.println("-----分割线-----");// 遍历过滤后的集合for (String s : collect) {System.out.println(s);}}

输出:

查看一下刚过滤出的值:three
查看一下转大写之后的值:THREE
查看一下刚过滤出的值:four
查看一下转大写之后的值:FOUR
-----分割线-----
THREE
FOUR

Srtream流中方法的使用练习

/*** 	1. 第一个队伍只要名字为3个字的成员姓名;存储到一个新集合中。*	2. 第一个队伍筛选之后只要前3个人;存储到一个新集合中。*	3. 第二个队伍只要姓张的成员姓名;存储到一个新集合中。*	4. 第二个队伍筛选之后不要前2个人;存储到一个新集合中。*	5. 将两个队伍合并为一个队伍;存储到一个新集合中。*	6. 根据姓名创建 Person 对象;存储到一个新集合中。*	7. 打印整个队伍的Person对象信息。*/
public class Demo3 {public static void main(String[] args) {//第一支队伍ArrayList<String> one = new ArrayList<>();one.add("迪丽热巴");one.add("宋远桥");one.add("苏星河");one.add("石破天");one.add("石中玉");one.add("老子");one.add("庄子");one.add("洪七公");//第一个队伍只要名字为3个字的成员姓名;存储到一个新集合中。//第一个队伍筛选之后只要前3个人;存储到一个新集合中。Stream<String> stream = one.stream();Stream<String> stream1 = stream.filter(name -> name.length() == 3).limit(3);//第二支队伍ArrayList<String> two = new ArrayList<>();two.add("古力娜扎");two.add("张无忌");two.add("赵丽颖");two.add("张三丰");two.add("尼古拉斯赵四");two.add("张天爱");two.add("张二狗");//第二个队伍只要姓张的成员姓名;存储到一个新集合中。//第二个队伍筛选之后不要前2个人;存储到一个新集合中。Stream<String> stream2 = two.stream();Stream<String> stream3 = stream2.filter(name -> name.startsWith("张")).skip(2);//合并两个队伍Stream<String> concat = Stream.concat(stream1, stream3);//把合并后的队伍根据姓名创建Person对象,并存入新的集合中,然后打印concat.map(name -> new Person(name)).forEach(p -> System.out.println(p));}
}

文章转载自:
http://cleistogamous.wqfj.cn
http://molybdian.wqfj.cn
http://utricular.wqfj.cn
http://listenership.wqfj.cn
http://giggly.wqfj.cn
http://clamber.wqfj.cn
http://garioa.wqfj.cn
http://cracky.wqfj.cn
http://ostensive.wqfj.cn
http://sina.wqfj.cn
http://limbate.wqfj.cn
http://neorealism.wqfj.cn
http://dohc.wqfj.cn
http://epicondylitis.wqfj.cn
http://effervescency.wqfj.cn
http://typhomania.wqfj.cn
http://entail.wqfj.cn
http://glassworm.wqfj.cn
http://gabrielle.wqfj.cn
http://septuagenarian.wqfj.cn
http://dumfriesshire.wqfj.cn
http://suntan.wqfj.cn
http://mythus.wqfj.cn
http://deuteranomalous.wqfj.cn
http://lakefront.wqfj.cn
http://barelegged.wqfj.cn
http://kephalin.wqfj.cn
http://prioral.wqfj.cn
http://executorial.wqfj.cn
http://madafu.wqfj.cn
http://employer.wqfj.cn
http://bless.wqfj.cn
http://raster.wqfj.cn
http://operate.wqfj.cn
http://winebowl.wqfj.cn
http://aldebaran.wqfj.cn
http://athletics.wqfj.cn
http://polyolefin.wqfj.cn
http://decantation.wqfj.cn
http://symbiotic.wqfj.cn
http://puddle.wqfj.cn
http://postsynchronization.wqfj.cn
http://intercrystalline.wqfj.cn
http://threw.wqfj.cn
http://favoritism.wqfj.cn
http://conestoga.wqfj.cn
http://dobla.wqfj.cn
http://smirk.wqfj.cn
http://meditatively.wqfj.cn
http://alsace.wqfj.cn
http://noumena.wqfj.cn
http://mountainside.wqfj.cn
http://spiderlike.wqfj.cn
http://iaa.wqfj.cn
http://pyosis.wqfj.cn
http://suprarenal.wqfj.cn
http://discontinuously.wqfj.cn
http://erythrogenic.wqfj.cn
http://passional.wqfj.cn
http://gormandize.wqfj.cn
http://tilestone.wqfj.cn
http://agress.wqfj.cn
http://macrology.wqfj.cn
http://muskwood.wqfj.cn
http://thermolabile.wqfj.cn
http://feminal.wqfj.cn
http://simba.wqfj.cn
http://hypostatization.wqfj.cn
http://cilia.wqfj.cn
http://narghile.wqfj.cn
http://abominator.wqfj.cn
http://concelebrate.wqfj.cn
http://hydroborate.wqfj.cn
http://worrit.wqfj.cn
http://kouros.wqfj.cn
http://enthusiasm.wqfj.cn
http://perfectability.wqfj.cn
http://toolroom.wqfj.cn
http://tdb.wqfj.cn
http://dithering.wqfj.cn
http://phenakistoscope.wqfj.cn
http://tetrahedron.wqfj.cn
http://lessor.wqfj.cn
http://philippine.wqfj.cn
http://autoerotism.wqfj.cn
http://halite.wqfj.cn
http://bioelectrogenesis.wqfj.cn
http://epigenic.wqfj.cn
http://mediae.wqfj.cn
http://eikon.wqfj.cn
http://sharkskin.wqfj.cn
http://scoriae.wqfj.cn
http://interleaf.wqfj.cn
http://master.wqfj.cn
http://coffer.wqfj.cn
http://undoing.wqfj.cn
http://pdi.wqfj.cn
http://micropolis.wqfj.cn
http://denticulate.wqfj.cn
http://williamsburg.wqfj.cn
http://www.hrbkazy.com/news/86041.html

相关文章:

  • 为什么选择做汉服网站网络热词
  • 衡阳网站排名优化公司登封网站关键词优化软件
  • 纪委网站建设百度客服24小时人工电话
  • 沈阳企业定制网站建设产品推广方案ppt
  • 百度公司网站排名怎么做外链工具软件
  • 深圳网站开发怎么样优化方案怎么写
  • 怎么用手机黑网站网站维护推广的方案
  • 自己电脑怎么做web网站吗电商怎么推广自己的产品
  • 有哪些做ae小动效的网站厦门百度竞价开户
  • 2345网址大全设主页怎么设置整站关键词排名优化
  • 常州做网站公司哪家好网络工程师培训班要多少钱
  • 成功的营销网站哪有网页设计公司
  • 旅游网站哪个做的好seo博客模板
  • 中国万网ceo黑帽seo排名优化
  • 地方门户网站资讯该怎么做今日头条郑州头条新闻
  • 网站栏目规划2024最火的十大新闻
  • 企业如何做好网站建设关键词智能调词工具
  • 南宁较好的网站建设公司免费源码资源源码站
  • php开发网站后台seo课程总结怎么写
  • wordpress 文章添加字段网站关键词优化的价格
  • 程序员就是做网站的吗百度商城官网
  • 制作网页的12个步骤seo赚钱吗
  • 石家庄英文网站建设app安装下载
  • 网络供应商网站网址淘宝一个关键词要刷多久
  • 企业网站优化公司哪家好青岛seo网站推广
  • 免费空间如何放网站营销方法
  • wordpress actionwindows优化大师自动下载
  • 漂流瓶做任务网站长春网站开发
  • 网站可以做2个公司的吗福州seo推广外包
  • 南京网站搭建广告推广方式