Stream流

ArrayList arrayList = new ArrayList<>(List.of(“张三丰”, “张三”, “李四”, “王二麻”));
ArrayList arrayList1 = new ArrayList<>();
for (String s : arrayList) {
if(s.startsWith(“张”)){
arrayList1.add(s);
}
}
System.out.println(arrayList1);
ArrayList arrayList2 = new ArrayList<>();
for (String s : arrayList1) {
if(s.length()==3){
arrayList2.add(s);
}
}
System.out.println(arrayList2);

//上面等价于下面
arrayList.stream().filter(s->s.startsWith(“张”)).filter(s->s.length()==3).forEach(s-> System.out.println(s));

    //Stream流的三类方法:

    //1、获取Stream流:创建一条流水线,并把数据放到流水线上准备进行操作

    //1)单列集合:可以使用Collection 接口中的默认方法stream()生成流
    ArrayList<String> arrylist = new ArrayList<>(List.of("A","B","C"));
    Stream<String> stream = arrylist.stream();
    stream.forEach(s -> System.out.println(s));

    //2)双列集合:间接生成流,先通过keySet或者entrySet获取一个Set集合,再获取stream流
    HashMap<String, Integer> stringIntegerHashMap = new HashMap<>(Map.of("zhangsan",23,"lisi",24,"wangwu",25));
    Set<String> keyset = stringIntegerHashMap.keySet();
    Stream<String> stream1 = keyset.stream();
    stream1.forEach(s -> System.out.println(s));
    //等价于keyset.stream().forEach(s -> System.out.println(s));

    Set<Map.Entry<String, Integer>> entries = stringIntegerHashMap.entrySet();
    entries.stream().forEach(s-> System.out.println(s));

    //3)数组:Arrays中的静态方法stream生成流
    int[] arr = {1,2,3,4,5};
    Arrays.stream(arr).forEach(s-> System.out.println(s));
    
    //4)同种数据类型的多个数据(单独存在,不放入数组集合):Stream.of(可变参数)生成流
    Stream.of(1,2,3,4).forEach(s-> System.out.println(s));

//2、中间方法:流水线上的操作,可以多次操作

    ArrayList<String> list = new ArrayList<String>(List.of("王二麻子", "苦茶子", "苦茶子", "慧慧子", "奥特曼", "李春花"));

    //1)filter
    //1>filter里面写匿名内部类
    //filter方法获取流中的每一个数据,而test方法中的s就依次表示流中每一个数据
    //如果判断为true,当前数据则留下,判断为false则不要
    list.stream().filter(new Predicate<String>() {
        @Override
        public boolean test(String s) {
            boolean result = s.startsWith("慧");
            return result;
        }
    }).forEach(s -> System.out.println(s));
    System.out.println("============================");
    //2>因为Predicate接口中只有一个抽象方法test,所以filter里面写lambda表达式
    list.stream().filter(s -> s.startsWith("慧")).forEach(s -> System.out.println(s));
    System.out.println("============================");

    //2)limit:截取指定参数个数的数据
    list.stream().limit(4).forEach(s -> System.out.println(s));
    System.out.println("============================");

    //3)skip:跳过指定参数个数的数据
    list.stream().skip(3).forEach(s -> System.out.println(s));
    System.out.println("============================");

    //4)Stream.concat(Stream a,Stream b):合并a和b两个流,看结果似乎自动去重
    ArrayList<String> list1 = new ArrayList<>(List.of("李四", "王五"));
    Stream<String> s1 = list.stream();
    Stream<String> s2 = list1.stream();
    Stream<String> concatStream = Stream.concat(s1, s2);
    concatStream.forEach(s -> System.out.println(s));
    //等价于Stream.concat(list.stream(), list1.stream()).forEach(s -> System.out.println(s));
    System.out.println("===========================");
    //5)distinct:去除重复元素,以来hashCode和equals方法
    list.stream().distinct().forEach(s -> System.out.println(s));
    System.out.println("===========================");

    //3、终结方法:一个Stream流只能有一个终结方法,是流水线上的最后一个操作

    //1)forEach(Consumer action):对流的每个元素操作
    //1>匿名内部类方式
    //Consumer接口的方法accept:对给定的参数执行操作
    //在forEach方法底层,会循环获取流中每一个数据,并调用accept方法,把每一个数据传给accept,s就代表每一个数据,在accept里写处理代码即可
    list.stream().forEach(new Consumer<String>() {
        @Override
        public void accept(String s) {
            System.out.println(s);
        }
    });
    System.out.println("==========================");
    //lambda表达式方式
    list.stream().forEach(s -> System.out.println(s));
    System.out.println("==========================");

    //2)count():返回流中的元素个数
    long count = list.stream().count();
    System.out.println(count);
    System.out.println("==========================");

    //注意:Stream上对数据的操作仅限于在流上,对于集合或数组里面的数据并不影响,无法改变,要想把操作的数据保存,那么就要用到收集方法
    //3)collect()方法
    //collect只负责收集数据,但不负责创建容器,也不负责把数据添加到容器中
    //创建容器并添加数据:Collectors.toList()或者Collectors.toSet()或Collectors.toMap()
    List<String> tolist = list.stream().filter(s -> s.startsWith("苦")).collect(Collectors.toList());
    System.out.println(tolist);
    Set<String> tolist1 = list.stream().filter(s -> s.startsWith("苦")).collect(Collectors.toSet());
    System.out.println(tolist1);
    System.out.println("==========================");

    ArrayList<String> list2 = new ArrayList<String>(List.of("zhangsan,23", "lisi,24", "wangwu,25"));

    Map<String, Integer> map = list2.stream().filter(s -> {
        String[] split = s.split(",");
        int age = Integer.parseInt(split[1]);
        return age >= 24;
    }).collect(Collectors.toMap(
            s -> {
                return s.split(",")[0];
            }
            ,
            s -> {
                return Integer.parseInt(s.split(",")[1]);
            }

    ));
    System.out.println(map);        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
 在C#中创建的文件选择对话框其实就是OpenFileDialog类的实例。通过对设定 OpenFileDialog类的属性来定制文件选择对话框的式样和功能。OpenFileDialog类的主要属性如下表: 属性 说明 AddExtension 该值指示如果用户省略扩展名对话框是否自动在文件名中添加扩展名。 CheckFileExists 该值指示如果用户指定不存在的文件名对话框是否显示警告。 CheckPathExists 该值指示如果用户指定不存在的路径对话框是否显示警告。 DefaultExt 获取或设置默认文件扩展名。 DereferenceLinks 该值指示对话框是否返回快捷方式引用的文件的位置或者是否返回快捷方式 FileName 字符串,获取或设置一个包含在文件对话框中选定的文件名的字符串。 FileNames 字符串数组,获取对话框中所有选定文件的文件名。 Filter 字符串,获取或设置当前文件名筛选器字符串该字符串决定对话框的"另存为文件类型"或"文件类型"框中出现的选择内容。 FilterIndex 整型,获取或设置文件对话框中当前选定筛选器的索引。 InitialDirectory 字符串,获取或设置文件对话框显示的初始目录。 Multiselect 布尔型,该值指示对话框是否允许选择多个文件。 ReadOnlyChecked 布尔型,该值指示是否选定只读复选框。 RestoreDirectory 布尔型该值指示对话框在关闭前是否还原当前目录。 ShowHelp 布尔型,该值指示文件对话框中是否显示"帮助"按钮。 ShowReadOnly 布尔型,该值指示对话是否包含只读复选框。 Title 字符串,获取或设置文件对话框标题。   C#通过调用OpenFileDialog类实例的ShowDialog方法显示创建的实例,这样一个文件选择对话框就显示出来了。   下列代码的作用在C#中通过创建一个OpenFileDialog实例,并设定此实例的各个属性值,来定制一个可以选择多个文件的文件选择对话框,并且把使用此对话框选择的多个文件名称通过提示框显示出来。请各位读者注意具体的实现方法: DialogResult d = openFileDialog1.ShowDialog(); if (d == DialogResult.OK) { string[] fileNames = openFileDialog1.FileNames; string fileNameString = string.Empty; foreach (string s in fileNames) { fileNameString = fileNameString + s + "\n"; } MessageBox.Show(fileNameString); }
Over a decade ago (nearly eternity in Internet Time), Randal Schwartz wrote the first edition of Learning Perl. In the intervening years, Perl itself has grown substantially from a "cool" scripting language used primarily by Unix system administrators to a robust object-oriented programming (OOP) language that runs on practically every computing platform known to mankind. Throughout its four editions, Learning Perl remained the same size (about 300 pages) and continued to cover much of the same material to remain compact and accessible to the beginning programmer. But there is much more to learn about Perl now than when that first book was written. Randal called the first edition of this book Learning Perl Objects, References, and Modules, and now it's Intermediate Perl, but we like to think of it as just Learning More Perl. [*] This is the book that picks up where Learning Perl leaves off. We show you how to use Perl to write larger programs. [*] Don't ask why it isn't called that. We must have had 300 emails on the subject. Okay, ask, since we know you're going to anyway. You never really stop learning Perl, so Learning More Perl doesn't really tell you much about the book. Our editor chose the name, which tells you what to expect. As in Learning Perl, we designed each chapter to be small enough to read in just an hour or so. Each chapter ends with a series of exercises to help you practice what you've just learned, and the answers are in the appendix for your reference. And like Learning Perl, we've developed the material in this book for a teaching environment and used it in that setting, including for our own use at Stonehenge Consulting Services, as we conduct on-site and open-enrollment trainings. You don't have to be a Unix guru, or even a Unix user, to benefit from this book. Unless otherwise noted, everything in this book applies equally well to Windows ActivePerl from ActiveState and all other modern implementations of Perl. To us

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值