Bootstrap

Java --- Collections.EMPTY_LIST 和 Collections.emptyList() 用法

    程序中使用集合时,如果 new 一个集合对象,就会初始化一个内存空间,占用内存资源,积少成多会造成内存浪费。Collections 中的空集合对象是一个静态常量,在内存中只会存在一份,能够节省内存资源。
    下面小编展示一下这两个的用法:

1,Collections.EMPTY_LIST 用法展示:

/**
 * @author: 
 * @description:
 * @date: 2022/3/25
 **/
public class CollectionListTest {

    public static void main(String[] args) {
    	//1, 首先声明两个 List
        List emptyList = Collections.EMPTY_LIST;
        List emptyList2 = Collections.EMPTY_LIST;
        //2, 试着 add() 元素,看看效果。运行后发现会报错。
        // Collections.EMPTY_LIST 此时只是返回了一个空集合,是静态的,
        // 不能使用 add() ,put() 等方法。
        emptyList.add("a");
        emptyList2.add("a");
    }
}

输出:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(AbstractList.java:148)
	at java.util.AbstractList.add(AbstractList.java:108)
	at CollectionListTest.main(CollectionListTest.java:15)

Process finished with exit code 1

对 emptyList emptyList2 赋值,及查看hashcode():

    public static void main(String[] args) {
        List emptyList = Collections.EMPTY_LIST;
        List emptyList2 = Collections.EMPTY_LIST;

		//1,查看 emptyList emptyList2 的哈希码值。会在下面的输出中发现,二者值一样,
		//可以判断,这两个引用指向的是同一处。
        System.out.println("====emptyList hashcode: " + emptyList.hashCode());
        System.out.println("====emptyList2 hashcode: " + emptyList2.hashCode());

		//2,声明两个普通的 ArrayList。并赋值,查看二者的哈希值,会发现不一样。
        List list = new ArrayList();
        List list2 = new ArrayList();
        list.add("b");
        list2.add("c");
        System.out.println("===list hashcode: " + list.hashCode());
        System.out.println("===list2 hashcode: " + list2.hashCode());

		//3,Collections.EMPTY_LIST 声明的list,只能直接赋值,无法进行add()等操作。
		//这里直接把list赋值给 emptyList。
        emptyList = list;
        emptyList2 = list2;

		//4,查看赋值后的 emptyList, emptyList2,  输出中哈希值不一样,引用改变成不一样了。
        System.out.println("====emptyList hashcode: " + emptyList.hashCode());
        System.out.println("====emptyList2 hashcode: " + emptyList2.hashCode());
    }

输出:

"C:\Program Files (x86)\Java\jdk1.8.0_31\bin\java.exe"
====emptyList hashcode: 1
====emptyList2 hashcode: 1
===list hashcode: 129
===list2 hashcode: 130
====emptyList hashcode: 129
====emptyList2 hashcode: 130

Process finished with exit code 0

2,Collections.emptyList() 用法展示:(同理类似)

/**
 * @author: 
 * @description:
 * @date: 2022/3/25
 **/
public class CollectionListTest {

    public static void main(String[] args) {
    	//1, 声明两个 Collections.emptyList(),都无法进行 add()等操作。查看两个哈希值,会发现一样
        List<String> emptyList = Collections.emptyList();
        List<String> emptyList2 = Collections.emptyList();
        System.out.println("===emptyList hashcode: " + emptyList.hashCode());
        System.out.println("===emptyList2 hashcode: " + emptyList2.hashCode());

		//2, 声明两个 new ArrayList<>(),赋值操作,查看哈希值
        List<String> list = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        list.add("a");
        list2.add("b");
        System.out.println("===list hashcode: " + list.hashCode());
        System.out.println("===list2 hashcode: " + list2.hashCode());

		//3,直接赋值 Collections.emptyList()。引用会变更
        emptyList = list;
        emptyList2 = list2;
        System.out.println("===emptyList hashcode: " + emptyList.hashCode());
        System.out.println("===emptyList2 hashcode: " + emptyList2.hashCode());
    }
}

输出:

===emptyList hashcode: 1
===emptyList2 hashcode: 1
===list hashcode: 128
===list2 hashcode: 129
===emptyList hashcode: 128
===emptyList2 hashcode: 129

Process finished with exit code 0
;