赞
踩
Logback日志级别(从大到小)
1:error:错误
2:warn:警告
3:info:信息
4:debug:调试
5:trace:追踪(例如:追踪用户行为轨迹)
针对集合进行功能简化
Stream流通常结合Lambda表达式来使用
1:获取方法:获取流(创建一个流水线)
2:中间方法:在流水线进行操作(例如:过滤,截取)
3:终结方法:流水线上的操作结束了,要关闭流水线
可以使用Collection接口中的默认方法Stream()生成流
Returns a sequential
Stream
with this collection as its source.Stream 流对象=单列集合对象.Stream();
间接获取流对象
先通过keyset()或entryset(),获取set集合
Stream 流对象=set集合对象.stream();
Stream 流对象=Arrays.stream(数组);
static <T> Stream<T> of(T... values)
运用可变参数
Stream 流对象=Stream.of(1,2,3);
- public class test {
- public static void main(String[] args) {
- //单列集合获取流对象
- List<String>list=new ArrayList<>();
- Collections.addAll(list,"java1","java2","java3","c++1","c++2");
- Stream<String> stream = list.stream();
-
-
- //map集合获取流对象
- HashMap<Integer,String>map=new HashMap<Integer, String>();
- map.put(1,"java1");
- map.put(2,"java2");
- // Set<Integer>set=map.keySet();
- //Stream<Integer>stream1=set.stream();
-
- Set<Map.Entry<Integer, String>> entries = map.entrySet();
- Stream<Map.Entry<Integer,String>>stream1=entries.stream();
-
- //数组获取流对象
- Integer []arr={1,2,3};//得用包装类型
- Stream<Integer> stream2 = Arrays.stream(arr);
- //Stream<Integer>stream2=Arrays.stream(arr);
-
- //同一类型获取流对象
- Stream<Integer> integerStream = Stream.of(1, 2, 3);
-
- }
- }
Returns a stream consisting of the elements of this stream that match the given predicate.
- public class test2 {
- public static void main(String[] args) {
- //单列集合获取流对象
- List<String> list=new ArrayList<>();
- Collections.addAll(list,"java1","java2","java3","c++1","c++2");
- /* Stream<String> stream = list.stream();
- //获取所有java开头的元素,并打印
- *//*stream.filter(new Predicate<String>() {
- @Override
- public boolean test(String s) {
- return s.startsWith("java");
- }
- });*//*
- Stream<String>stream1=stream.filter(s->s.startsWith("java"));//获取所有以java开头的元素
- *//* stream1.forEach(new Consumer<String>() {
- @Override
- public void accept(String s) {
- }
- });*//*
- //stream1.forEach(s-> System.out.println(s));//实例方法引用
- stream1.forEach(System.out::println);//打印*/
-
- //我们可以直接链式
- list.stream()
- .filter(s->s.startsWith("java"))
- .forEach(System.out::println);
- }
- }
截取指定参数个数的数据
- public class test3 {
- public static void main(String[] args) {
- List<String> list=new ArrayList<>();
- Collections.addAll(list,"java1","java2","java3","c++1","c++2");
- list.stream()
- .limit(4)//中间方法,只截取前四个元素
- .forEach(System.out::println);
- /*java1
- java2
- java3
- c++1*/
- }
- }
跳过指定参数个数的数据
- list.stream()
- .skip(3)//中间方法,跳过n个元素
- .forEach(System.out::println);
- /* c++1
- c++2*/
合并a,b两个流变成一个流
- public class test4 {
- public static void main(String[] args) {
- List<String >list1=new ArrayList<>();
- Collections.addAll(list1,"java1","java2","java3");
- List<String>list2=new ArrayList<>();
- Collections.addAll(list2,"c++1","c++2");
-
- //获取两个流对象
- Stream<String> stream1=list1.stream();
- Stream<String>stream2=list2.stream();
-
- //合并
- Stream<String> concat = Stream.concat(stream1, stream2);
- concat.forEach(System.out::println);
- /*java1
- java2
- java3
- c++1
- c++2*/
- }
- }
去除流中重复的元素,依赖(hashCode和equals方法)
假如流中存储的是自定义对象,要在对象中重写hashCode和equals方法
- public class test5 {
- public static void main(String[] args) {
- List<String> list=new ArrayList<>();
- Collections.addAll(list,"java1","java1","java3","c++1","c++2");
- list.stream()
- .distinct()
- .forEach(System.out::println);
- /*java1
- java3
- c++1
- c++2*/
- }
- }
将流中元素类型进行转换
- class People
- {
- private String name;
-
- public People(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public String toString() {
- return "People{" +
- "name='" + name + '\'' +
- '}';
- }
- }
- class Superman
- {
- private String name;
-
- public Superman(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public String toString() {
- return "Superman{" +
- "name='" + name + '\'' +
- '}';
- }
- }
- public class test6 {
- public static void main(String[] args) {
- List<People>list=new ArrayList<>();
- list.add(new People("hhh"));//这里存的是People类型
-
- list.stream().map(new Function<People,Superman>() {
- @Override
- public Superman apply(People people) {
- return new Superman(people.getName());
- }
- })
- .forEach(System.out::println);//打印出来Superman类型,说明类型发生转换Superman{name='hhh'}
- list.stream()
- .map(people -> new Superman(people.getName()))
- .forEach(System.out::println);
-
- }
- }
-
- public class test7 {
- public static void main(String[] args) {
- List<Integer>list=new ArrayList<>();
- Collections.addAll(list,1,5,9,3,6);
- //使用sort排序
- list.stream()
- .sorted()//默认升序
- .forEach(System.out::println);
-
- /*list.stream()
- .sorted(new Comparator<Integer>() {
- @Override
- public int compare(Integer o1, Integer o2) {
- return o2-o1;
- }
- });*/
- list.stream()
- .sorted((o1,o2)->o2-o1)//降序
- .forEach(System.out::println);
- }
- }
练习:
- class Book
- {
- private String name;
-
- public Book() {
- }
-
- public Book(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- '}';
- }
- }
- public class practice {
- public static void main(String[] args) {
- ArrayList<String>list1=new ArrayList<>();
- Collections.addAll(list1,"java1","java2","java3","java4","c++1","c++2");
- ArrayList<String>list2=new ArrayList<>();
- Collections.addAll(list2,"mysql1","mysql2","mysql3","oracle1","oracle2");
- //第一个队列只要名字是5个字符,且只要前三个
- Stream<String> stream1 = list1.stream()
- .filter(s -> s.length() == 5)
- .limit(3);
- //第二个队伍只要mysql,且跳过前两个
- Stream<String> stream2 = list2.stream()
- .filter(s -> s.startsWith("mysql"))
- .skip(2);
-
- //把两个队伍合并起来
- Stream<String>stream=Stream.concat(stream1,stream2);
-
- //把stream中的内容变成Book类
- Stream<Book> bookStream = stream.map(s -> new Book(s));
-
- //验证:
- bookStream.forEach(System.out::println);
- /* Book{name='java1'}
- Book{name='java2'}
- Book{name='java3'}
- Book{name='mysql3'}*/
-
- }
- }
Performs an action for each element of this stream.
对此流的每个元素进行操作
Returns the minimum element of this stream according to the provided
Comparator
. This is a special case of a reduction.返回此流中最小元素
Returns the maximum element of this stream according to the provided
Comparator
. This is a special case of a reduction.返回此流中最大元素
Returns the count of elements in this stream.
返回此流中的元素数
- public class test8 {
- public static void main(String[] args) {
- ArrayList<Integer>list=new ArrayList<>();
- Collections.addAll(list,2,8,5,3,9);
- //求最小值
- System.out.println(list.stream()
- .min((o1, o2) -> o1 - o2));//返回的是排序后坐标最小的
-
- System.out.println(list.stream()
- .max((o1, o2) -> o1 - o2));//返回的是排序后坐标最大的
-
- //求流中偶数的个数
- long count=list.stream()
- .filter(i->i%2==0)
- .count();
- //count()是终结方法,想再次使用必须重新创建
- }
- }
把流中的数据收集到集合中
把流中的数据收集到数组中
- public class test9 {
- public static void main(String[] args) {
- ArrayList<Integer>list=new ArrayList<>();
- Collections.addAll(list,1,2,3,4,5,6,7,8,9);
-
- List<Integer> collect = list.stream()
- .filter(i -> i % 2 == 0)
- .collect(Collectors.toList());//收集流中数据到List集合
- System.out.println(collect);
-
- Integer[]arr= list.stream()
- .filter(i->i%2==0)
- .toArray(value-> new Integer[value]);//value是数组的大小
- System.out.println(Arrays.toString(arr));
-
- }
- }
- public class test10 {
- public static void main(String[] args) {
- ArrayList<String>list=new ArrayList<>();
- Collections.addAll(list,"hhh,18","xxx,20","ccc,38");
- //获取年龄大于20岁的,并将其存到Map集合
- /* list.stream()
- .filter(s->{
- String[] split = s.split(",");
- Integer age=Integer.parseInt(split[1]);
- return age>=20;
- });*/
- /* list.stream()
- .filter(s->Integer.parseInt(s.split(",")[1])>=20)
- .collect(Collectors.toMap(new Function<String, Object>() {
- @Override
- public Object apply(String s) {
- return null;
- }
- }))*/
- Map<String, String> map = list.stream()
- .filter(s -> Integer.parseInt(s.split(",")[1]) >= 20)
- .collect(Collectors.toMap(s -> s.split(",")[0], s -> s.split(",")[1]));
- System.out.println(map);//{ccc=38, xxx=20}
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。