count 与 foreach为终结方法 其余的为延迟方法 可以使用链式编程
_forEach
package com.mujiachao.stream;
import java.util.stream.Stream;
/**
* foreach方法
*/
public class Demo4 {
public static void main(String[] args) {
Stream<String> stringStream = Stream.of("张三", "李四", "王五", "李六", "田七", "媾女", "盛安");
stringStream.forEach(name-> System.out.println(name));
}
}
filter方法
package com.mujiachao.stream;
import java.util.stream.Stream;
/**
* filter方法
*/
public class Demo5 {
public static void main(String[] args) {
Stream<String> stringStream = Stream.of("张三", "李四1", "王五11", "李六", "田七111", "媾女", "盛安");
Stream<String> stringStream1 = stringStream.filter(name -> name.length() == 2);
stringStream1.forEach(name-> System.out.println(name));
}
}
stream 流的特点只能使用一次
package com.mujiachao.stream;
import java.util.stream.Stream;
/**
* filter方法
*/
public class Demo5 {
public static void main(String[] args) {
Stream<String> stringStream = Stream.of("张三", "李四1", "王五11", "李六", "田七111", "媾女", "盛安");
Stream<String> stringStream1 = stringStream.filter(name -> name.length() == 2);
stringStream1.forEach(name-> System.out.println(name));
}
}
只能使用一次
map
package com.mujiachao.stream;
import java.util.stream.Stream;
/**
* map方法
*/
public class Demo6 {
public static void main(String[] args) {
Stream<String> stringStream = Stream.of("2", "4", "8", "10", "20", "7", "15");
stringStream.map(name->Integer.parseInt(name)).forEach(name-> System.out.println(name));
}
}
count 是一个终结方法
package com.mujiachao.stream;
import java.util.stream.Stream;
/**
* count方法
*/
public class Demo7 {
public static void main(String[] args) {
Stream<String> stringStream = Stream.of("2", "4", "8", "10", "20", "7", "15");
long count = stringStream.count();
System.out.println(count);
}
}
limit
package com.mujiachao.stream;
import java.util.stream.Stream;
/**
* limit方法
*/
public class Demo8 {
public static void main(String[] args) {
Stream<String> stringStream = Stream.of("2", "4", "8", "10", "20", "7", "15");
stringStream.limit(3).forEach(name-> System.out.println(name));
}
}
skip
package com.mujiachao.stream;
import java.util.stream.Stream;
/**
* skip方法
*/
public class Demo9 {
public static void main(String[] args) {
Stream<String> stringStream = Stream.of("2", "4", "8", "10", "20", "7", "15");
stringStream.skip(6).forEach(name-> System.out.println(name));
}
}
concat
package com.mujiachao.stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.stream.Stream;
/**
* concat方法
*/
public class Demo10 {
public static void main(String[] args) {
Stream<String> stringStream = Stream.of("2", "4", "8", "10", "20", "7", "15");
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "张三丰", "张无忌", "张浪", "乔峰");
Stream<String> stream = list.stream();
Stream.concat(stringStream,stream).forEach(name-> System.out.println(name));
}
}
集合元素传统做法
package com.mujiachao.stream;
import java.util.ArrayList;
import java.util.Collections;
/**
* 传统做法 操作集合
*/
public class Demo11 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"喜洋洋", "美羊羊", "懒洋洋", "灰太狼", "红太羊", "紫太羊");
ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1, "张三丰", "张无忌", "张浪", "乔峰");
ArrayList<String> list2 = new ArrayList<>();
for (String s : list) {
if (s.endsWith("羊")){
list2.add(s);
}
}
ArrayList<String> list3 = new ArrayList<>();
for (int i = 0; i < 2; i++) {
list3.add(list2.get(i));
}
ArrayList<String> list4 = new ArrayList<>();
for (String s : list1) {
if (s.startsWith("张")){
list4.add(s);
}
}
ArrayList<String> list5 = new ArrayList<>();
for (String s : list4) {
if (s.length()==3){
list5.add(s);
}
}
ArrayList<String> list6 = new ArrayList<>();
ArrayList<Person> list7 = new ArrayList<>();
list6.addAll(list5);
list6.addAll(list3);
for (String s : list6) {
list7.add(new Person(s));
}
System.out.println(list7);
}
}
集合元素stream做法
package com.mujiachao.stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.stream.Stream;
/**
* 传统做法 操作集合
*/
public class Demo12 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "喜洋洋", "美羊羊", "懒洋洋", "灰太狼", "红太羊", "紫太羊");
ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1, "张三丰", "张无忌", "张浪", "乔峰");
ArrayList<String> list2 = new ArrayList<>();
Stream<String> stream = list.stream().filter(name -> name.endsWith("羊")).limit(2);
Stream<String> stringStream = list1.stream().filter(name -> name.startsWith("张")).filter(name -> name.length() == 3);
Stream.concat(stream, stringStream).map(name -> new com.mujiachao.stream.Person(name)).forEach(name -> System.out.println(name));
}
}
把stream流转为集合
List<String> collect = stream.collect(Collectors.toList());
for (String s : collect) {
System.out.println(s);
}