需求:按照下面的要求完成集合的创建和遍历
import java.util.ArrayList;
public class StreamDemo {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<String>();
list.add("伦伦");
list.add("壮壮");
list.add("昊昊");
list.add("灿灿");
list.add("灿灿灿");
ArrayList<String> cList=new ArrayList<>();
for (String s:list){
if(s.startsWith("灿"))
cList.add(s);
}
System.out.println(cList);
ArrayList<String> twoList=new ArrayList<>();
for (String s:cList){
if(s.length()==2)
twoList.add(s);
}
for (String s:
twoList) {
System.out.println(s);
}
System.out.println("--------");
//Stream改进
list.stream().filter(s->s.startsWith("灿")).filter(s->s.length()==2).forEach(System.out::println);
}
}
default Stream stream ()
public class StreamDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
Stream<String> listStream = list.stream();
Set<String> set=new HashSet<String>();
Stream<String> setStream = set.stream();
}
}
public class StreamDemo {
public static void main(String[] args) {
Map<String,Integer> map=new HashMap<String,Integer>();
Stream<String> keyStream = map.keySet().stream();
Stream<Integer> valueStream = map.values().stream();
Stream<Map.Entry<String, Integer>> entryStream = map.entrySet().stream();
}
}
public class StreamDemo {
public static void main(String[] args) {
String[] strArray={"Hello","world","java!"};
Stream<String> strArrayStream = Stream.of(strArray);
Stream<String> strArrayStream2 = Stream.of("Hello","world","java!");
Stream<Integer> intArrayStream = Stream.of(1,2,3);
}
}
用于对流中的数据进行过滤
public class StreamDemo {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<>();
list.add("李日天");
list.add("朱大壮");
list.add("涂狗子");
list.stream().filter(s->s.startsWith("李")).forEach(System.out::println);
list.stream().filter(s->s.length()==3).forEach(System.out::println);
list.stream().filter(s->s.startsWith("李")).filter(s->s.length()==3).forEach(System.out::println);
}
}
Streamlimit(long maxSize): 返回此流中的元素组成的流,截取前指定参数个数的数据
Streamskip(long n):跳过指定参数个数的数据,返回由该流的剩余元素组成的流
public class StreamDemo {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<>();
list.add("李日天");
list.add("朱大壮");
list.add("涂狗子");
list.add("杨天花");
list.stream().limit(3).forEach(System.out::println);
list.stream().skip(3).forEach(System.out::println);
}
}
public class StreamDemo {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<>();
list.add("李日天");
list.add("朱大壮");
list.add("涂狗子");
list.add("杨天花");
Stream<String> s1=list.stream().limit(3);
Stream<String> s2=list.stream().skip(1);
// Stream.concat(s1,s2).forEach(System.out::println);
Stream.concat(s1,s2).distinct().forEach(System.out::println);
}
}
public class StreamDemo {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<>();
list.add("lhao");
list.add("zdzhaung");
list.add("tgouzi");
list.add("ytianhua");
list.stream().sorted().forEach(System.out::println);
list.stream().sorted((s1,s2)->{
int num=s1.length()-s2.length();
int num2=num==0?s1.compareTo(s2):num;
return num2;
}).forEach(System.out::println);
}
}
public class StreamDemo {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<>();
list.add("10");
list.add("20");
list.add("30");
list.add("40");
list.stream().map(s->Integer.parseInt(s)).forEach(System.out::println);
list.stream().map(Integer::parseInt).forEach(System.out::println);
list.stream().mapToInt(Integer::parseInt).forEach(System.out::println);//返回IntStream流
int res=list.stream().mapToInt(Integer::parseInt).sum();//IntStream仅有的方法,sum
System.out.println(res);
}
}
public class StreamDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("lhao");
list.add("zdzhaung");
list.add("tgouzi");
list.add("ytianhua");
list.stream().forEach(System.out::println);
long count = list.stream().filter(s -> s.startsWith("y")).count();
System.out.println(count);
}
}
R collect(Collector collector)