可变参数,Collections工具类,不可变集合详解讲解注意事项,细节,练习。

11、可变参数

格式返回值类型...参数名称

int...args

底层:可变参数底层其实就是一个数组

注意事项:

  1. 在方法的形参中最多只能写一个可变参数。

public static int getSum(int...args1,int...args2);这是错的,因为方法形参中最多只能写一个可变参数。

    2. 在方法当中,如果除了可变参数以外,还有其他的形参,那么可变参数要写在最后。

public static int getSum(int a,Stirng s,int args)这是对的,因为如果还有其他形参,可变参数要写在最后

public class Main {
    public static void main(String[] args) {
        int result=getSum(1,2);
        int result1=getSum(2,2,3,4,4,4,7);
        System.out.println(result);
        System.out.println(result1);
    }
    public static int getSum(int...args) {
        System.out.println(args);
        int sum=0;
        for(int i=0;i<args.length;i++){
            sum+=args[i];
        }
        int sum1=0;
        for(int i : args) {
            sum1+=i;
        }
        return sum;
    }
;}

12、Collections

Collections
  • java.util.Collections:是集合工具类

  • 作用:Collections不是集合,而是集合的工具类

Collections常用的API

public static<T> boolean addAll(Collection<T> c,T... elements) 批量添加元素

第一个参数是单列集合,第二个参数是可变参数,即可以添加多个元素。

:这里只能对单列集合进行操作。

public static void shuffle(List<?> list) 打乱List集合元素的顺序

:这里只能对List集合进行操作。

Collections其他方法


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionsUtil {
    public static void main(String[] args){
        ArrayList<String> list = new ArrayList<>();
        //批量添加
        Collections.addAll(list,"ab","cde","fgh","ijkl","m","nopq");
        System.out.println(list);
        //打乱集合元素顺序
        Collections.shuffle(list);
        System.out.println(list);

        ArrayList<Integer> list1 = new ArrayList<>();
        Collections.addAll(list1,5,7,6,1,4,2,3);
        Collections.sort(list1);
        System.out.println(list1);

        Collections.sort(list1,new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        System.out.println(list1);

        ArrayList<Integer> list2 = new ArrayList<>();
        Collections.addAll(list2,1,2,3,4,5,6,7,8,9);
        System.out.print(Collections.max(list2));
        System.out.println(Collections.min(list2));

        ArrayList<String> list3 = new ArrayList<>();
        Collections.addAll(list3,"a","aa","aaa","aaaa");
        System.out.println(Collections.max(list3,new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length()-o2.length();
            }
        }));
        System.out.println(Collections.min(list3,new Comparator<String>(){
            @Override
            public int compare(String o1, String o2) {
                return o1.length()-o2.length();
            }
        }));
    }
}

14、不可变集合

of方法是创建不可变集合JDK9新增的。

创建不可变集合的应用场景
  • 如果某个数据不能被修改,把它防御性地拷贝到不可变集合中是个很好的实践。

  • 当集合对象被不可信的库调用时,不可变形式是安全的。

简单理解:不想让别人修改集合中的内容

创建不可变集合的书写格式

在List、Set、Map接口中,都存在静态的of方法,可以获取一个不可变的集合

static <E> List<E> of(E...elements) 创建一个具有指定元素的List集合对象

static<E> Set<E> of(E...elements) 创建一个具有指定元素的Set集合对象

Static <K, V> Map<K, V> of(E...elements) 创建一个具有指定元素的Map集合对象

注意:这个集合不能添加,不能删除,不能修改。

E...elements可变参数

细节:

  1. Set对象不重复。

  2. Map集合对象键不能重复,值可以

  3. Map里面的of方法中的参数不是可变参数,而是一堆重载,参数是有上限的,最多只能传递20个参数,10个键值对。

(1)为什么Map.of 不设计成可变参数?

因为:Map 需要「键、值交替」,而 Java 可变参数有两个死规则:

  1. 一个方法只能有一个可变参数

  2. 可变参数必须在最后

// 不合法!

public static <K,V> Map<K,V> of(K... keys, V... values)

不行:两个可变参数,违反规则 1

// 也不行

public static <K,V> Map<K,V> of(K key1, V value1, K... rest)

也会有问题:后面全是 K,没法区分哪里是键、哪里是值。

所以Map.of 不设计成可变参数

(2)那如果我们想传入超过10键值对对象,怎么办?

map里面有个Map.ofEntries(...)方法,这个才是可变参数:

public static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)

用法:

import static java.util.Map.entry;

Map<String, Integer> map = Map.ofEntries(

entry("a", 1),

entry("b", 2),

entry("c", 3),

// ... 随便多少对,没有 10 个限制

entry("z", 26)

);

  • 参数是 Entry... 可变参数

  • 数量无上限(只要内存够)

  • 超过 10 个键值对,必须用 ofEntries

讲解Map.ofEntries(...)方法

Map.ofEntries()参数是可变参数,可变参数底层是数组,所以我们可以直接传入一个Entry类型的数组即可,用到方法:

Set<Map.Entry<String, String>> entries = hm.entrySet();

Map.Entry[] arr = new Map.Entry[0];

entries.toArray(arr);

这里用到带参数的toArray(T [] a)方法,指定参数类型为Entry类型,而长度Entry[0]设为0即可,添加数据时底层会自动扩容

// toArray方法在底层会比较集合的长度跟数组的长度两者的大小

// 如果集合的长度 > 数组的长度:数据在数组中放不下,此时会根据实际数据的个数,重新创建数组

// 如果集合的长度 <= 数组的长度:数据在数组中放得下,此时不会创建新的数组,而是直接用

最终创建不可变Map集合的方法:JDK10以上

Map里面的copyof()方法

例:Map<String,String> mapp = Map.copyOf(hm);

Map接口创建不可变集合:

(1)键值对数量不超过10个用of方法即可

(2)键值对数量超过10个用ofEntries方法

(3)JDK10以后出了更简单的copyof方法

import java.util.Map;
import java.util.HashMap;
import java.util.Set;

import static java.util.Map.entry;

public class TextMap {
    public static void main(String[] args){
        Map<String, Integer> map = Map.ofEntries(
                entry("a", 1),
                entry("b", 2),
                entry("c", 3),
                // ... 随便多少对,没有 10 个限制
                entry("z", 26)
        );
        HashMap<String, String> hm = new HashMap<>();
        hm.put("张三", "南京");
        hm.put("李四", "北京");
        hm.put("王五", "上海");
        hm.put("赵六", "北京");
        hm.put("孙七", "深圳");
        hm.put("周八", "杭州");
        hm.put("吴九", "宁波");
        hm.put("郑十", "苏州");
        hm.put("刘一", "无锡");
        hm.put("陈二", "嘉兴");
        hm.put("aaa", "111");
        //利用上面的数据获取一个不可变的集合
        //获取所有的键值对对象Entry
        Set<Map.Entry<String, String>> entries = hm.entrySet();
        //把entries变成一个数组,因为ofEntries()方法里面实际上是可变参,可变参数实际上底层是一个数组。
        Map.Entry[] arr = new Map.Entry[0];
        entries.toArray(arr);

        Map.Entry[] arr1 = entries.toArray(arr);

        Map<String, Integer> m = Map.ofEntries(arr1);

        for(Map.Entry<String,Integer> entry : m.entrySet()){
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
        //最终创建Map不可变集合的方法
        Map<String,String> mapp = Map.copyOf(hm);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值