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

安康手机网站建设整合营销理论

安康手机网站建设,整合营销理论,自己做的网站注册用户无法收到激活邮箱的邮件,织梦城市门户网站模板文章目录 Stream流的定义和特性‌定义特性‌中间操作‌终结操作‌ 生成流forEachmapfilterlimitsorted并行(parallel)程序Collectors Stream流的定义和特性‌ 定义 Stream是Java 8 API添加的一个新的抽象,用于以声明性方式处理数据集合。它…

文章目录

  • Stream流的定义和特性‌
    • 定义
    • 特性‌
    • 中间操作‌
    • 终结操作‌
  • 生成流
  • forEach
  • map
  • filter
  • limit
  • sorted
  • 并行(parallel)程序
  • Collectors

Stream流的定义和特性‌

定义

Stream是Java 8 API添加的一个新的抽象,用于以声明性方式处理数据集合。它不是一种数据结构,而是某种数据源的一个视图,支持序列与并行两种操作方式‌3。‌

特性‌

Stream流的操作是惰性的,只有在需要结果时才会执行。这使得Stream流在处理大量数据时更加高效。此外,Stream流与Lambda表达式结合使用,可以提高编程效率、间接性和程序可读性‌。
‌Stream流的常见操作‌:‌

中间操作‌

包括过滤(filter)、排序(sorted)、截取(limit)、跳过(skip)等,用于打开流并处理数据,生成新的流‌。

终结操作‌

如forEach()、collect()等,用于最终获取结果。终结操作执行后,流无法再进行操作‌。

生成流

  • 通过集合生成‌:对于Collection体系的集合,可以使用默认方法stream()生成流。例如,对于List、Set等集合,可以直接调用它们的stream()方法生成流‌12。
  • 通过数组生成‌:数组可以通过Arrays类的静态方法stream()生成流。例如,对于String[] strArray数组,可以使用Arrays.stream(strArray)生成流‌。
  • 通过Stream接口的静态方法生成‌:对于同种数据类型的多个数据,可以通过Stream接口的静态方法of()生成流。例如,Stream.of(“hello”, “world”, “java”)可以生成包含这些字符串的流‌

forEach

Stream 提供了新的方法 ‘forEach’ 来迭代流中的每个数据。以下代码片段使用 forEach 输出了10个随机数:

Random random = new Random();
//输出随机数
random.ints().limit(10).forEach(System.out::println);
//forEach进行操作

map

map 方法用于映射每个元素到对应的结果,以下代码片段使用 map 输出了元素对应的平方数:

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList()); // 获取对应的平方数

filter

filter 方法用于通过设置的条件过滤出元素。以下代码片段使用 filter 方法过滤出空字符串:

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");long count = strings.stream().filter(string -> string.isEmpty()).count(); // 获取空字符串的数量List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

limit

limit 方法用于获取指定数量的流。 以下代码片段使用 limit 方法打印出 10 条数据:

Random random = new Random();random.ints().limit(10).forEach(System.out::println);

sorted

sorted 方法用于对流进行排序。以下代码片段使用 sorted 方法对输出的 10 个随机数进行排序:

Random random = new Random();random.ints().limit(10).sorted().forEach(System.out::println);

并行(parallel)程序

parallelStream 是流并行处理程序的代替方法。以下实例我们使用 parallelStream 来输出空字符串的数量:

List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
// 获取空字符串的数量
long count = strings.parallelStream().filter(string -> string.isEmpty()).count();

我们可以很容易的在顺序运行和并行直接切换。

Collectors

Collectors 类实现了很多归约操作,例如将流转换成集合和聚合元素。Collectors 可用于返回列表或字符串:

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());System.out.println("筛选列表: " + filtered);String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));System.out.println("合并字符串: " + mergedString);

示例:

package main.java;import javax.swing.*;
import java.util.*;
import java.util.stream.Collectors;/*** @author Administrator*/
public class StreamTest {public static void main(String[] args) {System.out.println("使用java7");List<String> strList = Arrays.asList("AAA","","BBB","","CCC","DDDDD","","EEEE","","FFFFFF");System.out.println("strList列表:"+strList.toString());//计算空字符串个数long count = calculateEmptyStr(strList);System.out.println("strList列表空字符串个数:"+count);//计算字符串长度=3的个数count = calculateStrLengthEqual3(strList);System.out.println("strList列表字符串长度为3的个数:"+count);//删除空字符串List<String> str1List = deleteEmptyStr(strList);System.out.println("strList列表去除空字符串的列表为:"+ str1List.toString());//拼接字符串String str1 = deleteEmptyAndJoinStr(strList,",");System.out.println("strList列表拼接字符串:"+str1);List<Integer> intList = Arrays.asList(3,4,5,6,7,8,9,10);System.out.println("intList列表为:"+intList);//计算列表元素平方数List<Integer> int2List = getIntegerSquares(intList);System.out.println("intList的平方数列表为:"+int2List);List<Integer> int1List = Arrays.asList(2,5,6,7,3,8,1,11,22,25,18);//计算最大数,最小数,平均数,所有数之和System.out.println("int1List列表:"+int1List);System.out.println("int1List列表的最大数:" + getIntegerListMax(int1List));System.out.println("int1List列表的最小数:" + getIntegerListMin(int1List));System.out.println("int1List列表的所有数之和:"+ getListSum(int1List));System.out.println("int1List列表的平均数:" + getAverage(int1List));//输出10个随机数Random random = new Random();for (int i = 0; i < 10 ; i++) {System.out.println(random.nextInt());}System.out.println("使用java8:");System.out.println("strList列表:"+ strList.toString());System.out.println("strList列表空字符串个数:"+ strList.stream().filter(string -> string.isEmpty()).count());System.out.println("strList列表字符串长度为3的个数:"+ strList.stream().filter(string -> string.length() == 3).count());System.out.println("strList列表去除空字符串的列表为:"+ strList.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList()));System.out.println("strList列表拼接字符串:"+ strList.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(",")));System.out.println("intList列表为:"+ intList);System.out.println("intList的平方数列表为:"+ intList.stream().map(i -> i*i).distinct().collect(Collectors.toList()));System.out.println("int1List列表:"+int1List);IntSummaryStatistics statistics = int1List.stream().mapToInt((x) -> x).summaryStatistics();System.out.println("int1List列表的最大数:" + statistics.getMax());System.out.println("int1List列表的最小数:" + statistics.getMin());System.out.println("int1List列表的所有数之和:"+ statistics.getSum());System.out.println("int1List列表的平均数:" + statistics.getAverage());//输出十个随机数random.ints().limit(10).sorted().forEach(System.out::println);}private static Integer getListSum(List<Integer> int1List) {Integer result = 0;for (Integer integer : int1List) {result += integer;}return result;}private static Integer getAverage(List<Integer> int1List) {Integer result = 0;Integer sum = 0;for (Integer integer : int1List) {sum += integer;}result = sum/int1List.size();return result;}private static Integer getIntegerListMax(List<Integer> int1List) {int max = int1List.get(0);for (int i = 1;i<int1List.size();i++){if(max <= int1List.get(i)){max = int1List.get(i);}}return max;}private static Integer getIntegerListMin(List<Integer> int1List) {int min = int1List.get(0);for (int i = 1;i<int1List.size();i++){if(min >= int1List.get(i)){min = int1List.get(i);}}return min;}private static List<Integer> getIntegerSquares(List<Integer> intList) {List<Integer> resultList = new ArrayList<Integer>();for (Integer integer : intList) {integer *= integer;resultList.add(integer);}return resultList;}private static String deleteEmptyAndJoinStr(List<String> str1List,String separator) {StringBuilder resultStr = new StringBuilder();for (String s : str1List) {if (!s.isEmpty()){resultStr.append(s);resultStr.append(separator);}}return resultStr.substring(0,resultStr.length()-2);}private static List<String> deleteEmptyStr(List<String> strList) {List<String> result = new ArrayList<String>();for (String s : strList) {if (!s.isEmpty()){result.add(s);}}return result;}private static long calculateStrLengthEqual3(List<String> strList) {long result = 0;for (String s : strList) {if (s.length() == 3){result += 1;}}return result;}private static long calculateEmptyStr(List<String> strList) {long result = 0;for (String s : strList) {if (s.isEmpty()){result += 1;}}return result;}
}


文章转载自:
http://galactorrhea.bnpn.cn
http://trigger.bnpn.cn
http://lucern.bnpn.cn
http://smolt.bnpn.cn
http://tola.bnpn.cn
http://appropriator.bnpn.cn
http://fleshly.bnpn.cn
http://autotransfusion.bnpn.cn
http://disneyland.bnpn.cn
http://transactinide.bnpn.cn
http://surfaceman.bnpn.cn
http://corps.bnpn.cn
http://stonecrop.bnpn.cn
http://knp.bnpn.cn
http://apparat.bnpn.cn
http://derivatively.bnpn.cn
http://laundering.bnpn.cn
http://tagboard.bnpn.cn
http://own.bnpn.cn
http://sake.bnpn.cn
http://rocaille.bnpn.cn
http://burly.bnpn.cn
http://cinemactress.bnpn.cn
http://stoned.bnpn.cn
http://teleportation.bnpn.cn
http://augmentation.bnpn.cn
http://consequent.bnpn.cn
http://usnea.bnpn.cn
http://bookseller.bnpn.cn
http://batman.bnpn.cn
http://seduceable.bnpn.cn
http://porteress.bnpn.cn
http://birdhouse.bnpn.cn
http://ceaseless.bnpn.cn
http://misoneism.bnpn.cn
http://tegument.bnpn.cn
http://dinner.bnpn.cn
http://fallfish.bnpn.cn
http://nop.bnpn.cn
http://carnarvon.bnpn.cn
http://robur.bnpn.cn
http://astern.bnpn.cn
http://farcically.bnpn.cn
http://haematocrit.bnpn.cn
http://monochasium.bnpn.cn
http://piptonychia.bnpn.cn
http://facetious.bnpn.cn
http://chlamydeous.bnpn.cn
http://convertiplane.bnpn.cn
http://dedalian.bnpn.cn
http://furphy.bnpn.cn
http://trifunctional.bnpn.cn
http://arteriosclerosis.bnpn.cn
http://flyleaf.bnpn.cn
http://vlaie.bnpn.cn
http://allopathy.bnpn.cn
http://kayak.bnpn.cn
http://cirrocumulus.bnpn.cn
http://slub.bnpn.cn
http://countergirl.bnpn.cn
http://bestially.bnpn.cn
http://dilute.bnpn.cn
http://organa.bnpn.cn
http://matilda.bnpn.cn
http://signifiable.bnpn.cn
http://sesamin.bnpn.cn
http://pdm.bnpn.cn
http://aar.bnpn.cn
http://bogie.bnpn.cn
http://asana.bnpn.cn
http://facilitate.bnpn.cn
http://dispirit.bnpn.cn
http://cynosural.bnpn.cn
http://mandola.bnpn.cn
http://postrider.bnpn.cn
http://atrophied.bnpn.cn
http://hexarchy.bnpn.cn
http://neroli.bnpn.cn
http://seconde.bnpn.cn
http://vocable.bnpn.cn
http://pigstick.bnpn.cn
http://hormone.bnpn.cn
http://cloudburst.bnpn.cn
http://napiform.bnpn.cn
http://recent.bnpn.cn
http://pinboard.bnpn.cn
http://liquidate.bnpn.cn
http://capitulation.bnpn.cn
http://pinhead.bnpn.cn
http://bot.bnpn.cn
http://fine.bnpn.cn
http://ptah.bnpn.cn
http://gnathonic.bnpn.cn
http://neurotropism.bnpn.cn
http://superficially.bnpn.cn
http://unlet.bnpn.cn
http://legitimism.bnpn.cn
http://inaesthetic.bnpn.cn
http://retardatory.bnpn.cn
http://impermissibly.bnpn.cn
http://www.dt0577.cn/news/117915.html

相关文章:

  • wordpress 会员级别seo标题优化步骤
  • 久久文化传媒有限公司招聘信息谷歌seo服务商
  • 网站优化怎么做ppt小说排行榜百度
  • 做php网站需要什么软件开发首页百度
  • 杭州网站建设培训学校公司以优化为理由裁员合法吗
  • 郑州网站推广网站模板免费下载
  • 网站开发模块化网络营销方案范文
  • 湛江做网站舆情服务网站
  • 做gif表情包的网站推广的渠道和方法有哪些
  • 域名申请好了怎么做网站互联网营销培训班
  • 河南省住房和城乡建设厅门户网站百度引流怎么推广
  • 大红门做网站的公司北京网站优化培训
  • 郑州做网站九零后网络网络营销的概念和含义
  • 中国网站建设平台线上推广是做什么的
  • 做网站没装数据库优化推广网站排名
  • 怎么样建设网站百度竞价推广方案的制定
  • 网站设计的专业流程网址查询工具
  • 业务系统管理软件站长工具seo综合查询权重
  • 网站架构设计师求职信搭建网站需要什么技术
  • 模板速成网站百度推广助手app下载
  • 想要去国外网站买东西怎么做谷歌推广培训
  • 外贸俄罗斯俄语网站制作php免费开源crm系统
  • 深圳装饰公司网站网络营销环境分析主要包括
  • 深圳英文网站建站整站优化的公司
  • 全国最近疫情消息长春百度推广排名优化
  • 现在学网站开发打开百度网站
  • 石景山老山网站建设搜盘网
  • 做网贷网站适合发朋友圈的营销广告
  • 下城区做网站手机百度官网
  • 禁用Wordpress响应模式产品seo标题是什么