可变参数
-
概述
参数个数可变,用作方法的形参出现,那么方法参数个数就是可变的
-
定义格式
修饰符 返回值类型 方法名(数据类型… 变量名) { }
-
注意
- 这里的变量其实是一个数组
- 如果一个方法有多个参数,包含可变参数,可变参数要放在最后
-
代码
public class Demo01 { public static void main(String[] args) { int sum = getSum(1,2,3,4,5,6,7,8,9,10); System.out.println(sum); } private static int getSum(int... arr) { int sum = 0 ; for (int i : arr) { sum+=i; } return sum; } }
1. 创建不可变集合
-
概述
- 在List、Set、Map接口中,都存在of方法,可以创建一个不可变的集合
- 不能增删改
- 主要用来结合集合的带参构造,实现集合的批量添加
- 在Map接口中,还有一个ofEntries方法可以提高代码的阅读性
- 首先会把键值对封装成一个Entry对象,再把这个Entry对象添加到集合当中
- 在List、Set、Map接口中,都存在of方法,可以创建一个不可变的集合
-
代码
public class Demo02 { public static void main(String[] args) { List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7); list.add(8);//UnsupportedOperationException Set<Integer> set = Set.of(1, 2, 3, 4, 5); set.remove(1);//UnsupportedOperationException Map<String, String> map = Map.of("A", "a", "B", "b"); map.put("C","c");//UnsupportedOperationException } }
Stream流
1. Stream流常见生成方法
-
三类方法
- 获取Stream流
- 中间方法
- 终结方法
2. 获取方法
-
单列集合
-
使用默认方法stream() 生成流
-
default Stream<E> stream()
-
-
双列集合
- 把Map转为Set集合,间接生成流
-
数组
- 通过Arrays中的静态方法stream生成流
-
同种数据类型的多个数据
- 通过Stream接口的静态方法of(T…values) 生成流
-
代码
public class StreamDemo01 { public static void main(String[] args) { //单列集合 ArrayList<String> arrayList = new ArrayList<>(); Collections.addAll(arrayList,"aaa","bbb","ccc"); arrayList.stream().forEach(s-> System.out.println(s)); //双列集合 HashMap<String,String> hashMap = new HashMap<>(); hashMap.put("001","zs"); hashMap.put("002","ls"); hashMap.put("003","ww"); hashMap.keySet().stream().forEach(s -> System.out.println(s)); hashMap.keySet().stream().forEach(s->{ System.out.println(hashMap.get(s)); }); hashMap.entrySet().stream().forEach(s-> System.out.println(s)); //数组 int[] arrs = {1,2,3,4,5}; Arrays.stream(arrs).forEach(s-> System.out.println(s)); //多个数据 Stream.of(1,2,3,4,5,6,7).forEach(s-> System.out.println(s)); } }
3. 中间方法
方法名 | 说明 |
---|---|
Stream filter(Predicate predicate) | 用于对流中的数据进行过滤(Predicate接口的实现,可以使用lambda表达式来讲话) |
Stream limit(long maxSize) | 返回此流中的元素组成的流,截取前指定参数个数的数据 |
Stream skip(long n) | 跳过指定参数个数的数据,返回由该流的剩余元素组成的流 |
static Stream concat(Stream a, Stream b) | 合并a和b两个流为一个流 |
Stream distinct() | 去重,去除流中重复的元素。(根据hashCode和equals方法) |
-
代码
public class StreamDemo06 { public static void main(String[] args) { ArrayList<String> list1 = new ArrayList<>(); Collections.addAll(list1,"a","b","c","d","e","e","e"); ArrayList<String> list2 = new ArrayList<>(); Collections.addAll(list2,"q","w","e","r","d","f"); //Stream<T> filter(Predicate predicate) list1.stream().filter(s -> !(s.equals("e"))) .forEach(s -> System.out.print(s+" "));//a b c d System.out.println(); System.out.println("================="); //Stream<T> limit(long maxSize) list2.stream().limit(4) .forEach(s -> System.out.print(s+" "));//q w e r System.out.println(); System.out.println("================="); //Stream<T> skip(long n) list2.stream().skip(4) .forEach(s -> System.out.print(s+" "));//d f System.out.println(); System.out.println("================="); //static <T> Stream<T> concat(Stream a, Stream b) //Stream<T> distinct() Stream.concat(list1.stream(),list2.stream()) .distinct().forEach(s -> System.out.print(s+" ")); //a b c d e q w r f } }
4. 终结方法
方法名 | 说明 |
---|---|
void forEach(Consumer action) | 对此流的每个元素执行操作 |
long count() | 返回此流中的元素数 |
5. 收集方法
-
概念
对数据使用Stream流的方式操作完毕后,可以把流中的数据收集到集合中
-
常用方法
方法名 说明 R collect(Collector collector) 把结果收集到集合中 - collect负责收集数据,但不负责创建容器和添加
-
工具类Collectors提供了具体的收集方式
方法名 说明 public static Collector toList() 把元素收集到List集合中 public static Collector toSet() 把元素收集到Set集合中 public static Collector toMap(Function keyMapper,Function valueMapper) 把元素收集到Map集合中 - Collector toList() :在底层会创建一个List集合,并把所有的数据添加到List集合中,并返回
- Collector toSet() :在底层会创建一个Set集合,并把所有的数据添加到Set集合中,并返回
- Collector toMap:创建一个map集合,并将数据添加到集合当中
- Function keyMapper :如何获取键
- Function valueMapper:如何获取值
- 可以使用lambda表达式
-
代码
public class StreamDemo07 { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); Collections.addAll(list,1,2,3,4,4,4,4,5); //public static <T> Collector toList() List<Integer> ListCollect = list.stream().filter(num -> num % 2 == 0) .collect(Collectors.toList()); System.out.println(ListCollect);//[2, 4, 4, 4, 4] //public static <T> Collector toSet() Set<Integer> SetCollect = list.stream().collect(Collectors.toSet()); System.out.println(SetCollect);//[1, 2, 3, 4, 5] ArrayList<String> list1 = new ArrayList<>(); Collections.addAll(list1,"k1,v1","k2,v2","k3,v3"); //public static Collector toMap(Function keyMapper,Function valueMapper) Map<String, String> MapCollect = list1.stream().collect(Collectors.toMap( s -> s.split(",")[0], s -> s.split(",")[1] )); System.out.println(MapCollect);//{k1=v1, k2=v2, k3=v3} } }