使用ArryList集合作为key值,出现key值全部一样或者被统一清空,因为,此时ArryList只创建了一个对象,具备同样的地址。所以将ArryList集合作为key值会出现一样的清空,不符合hashmap集合中key值的要求
1 package com.oracle.demo01; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 8 public class DouDiZhu2 { 9 public static void main(String[] args) { 10 //定义map集合 11 HashMap<ArrayList<Integer>,String> big= 12 new HashMap<ArrayList<Integer>,String>(); 13 ArrayList<Integer> arr=new ArrayList<Integer>(); 14 //封装数据,通过内外循环,拼接字符串 15 String[] arr1={"♣","◇","♠","♥"}; 16 String[] arr2={"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; 17 //拼接字符串,并存入map集合中 18 int index=2; 19 for(int i1=0;i1<arr1.length;i1++){ 20 for(int i2=0;i2<arr2.length;i2++){ 21 arr.add(index); 22 //必须创建不同的对象,进行存储 23 for(int i=0;i<arr.size()-1;i++){ 24 arr.remove(i); 25 } 26 //对arr进行整体的清空 27 big.put(arr,arr1[i1]+arr2[i2]); 28 index++; 29 } 30 } 31 //封装大小王 32 ArrayList<Integer> arrx=new ArrayList<Integer>(0); 33 ArrayList<Integer> arrd=new ArrayList<Integer>(1); 34 big.put(arrx, "大王"); 35 big.put(arrd, "小王"); 36 //此时为一个对象,会出现值覆盖问题 37 /*big.put(new ArrayList<Integer>(0), "大王"); 38 big.put(new ArrayList<Integer>(1), "小王");*/ 39 //测试是否存储成功,map自己调用tostring() 40 System.out.print(big); 41 } 42 }