HashMap和HashSet

  • HashMap的键值对
    HashMap储存数据的方式是—— 键值对
    package collection;
       
    import java.util.HashMap;
       
    public class TestCollection {
        public static void main(String[] args) {
            HashMap<String,String> dictionary = new HashMap<>();
            dictionary.put("adc", "物理英雄");
            dictionary.put("apc", "魔法英雄");
            dictionary.put("t", "坦克");
             
            System.out.println(dictionary.get("t"));
        }
    }
  • 键不能重复,值可以重复
    对于HashMap而言,key是唯一的,不可以重复的。 
    所以,以相同的key 把不同的value插入到 Map中会导致旧元素被覆盖,只留下最后插入的元素。 
    不过,同一个对象可以作为值插入到map中,只要对应的key不一样
    package collection;
      
    import java.util.HashMap;
      
    import charactor.Hero;
      
    public class TestCollection {
        public static void main(String[] args) {
            HashMap<String,Hero> heroMap = new HashMap<String,Hero>();
             
            heroMap.put("gareen", new Hero("gareen1"));
            System.out.println(heroMap);
             
            //key为gareen已经有value了,再以gareen作为key放入数据,会导致原英雄,被覆盖
            //不会增加新的元素到Map中
            heroMap.put("gareen", new Hero("gareen2"));
            System.out.println(heroMap);
             
            //清空map
            heroMap.clear();
            Hero gareen = new Hero("gareen");
             
            //同一个对象可以作为值插入到map中,只要对应的key不一样
            heroMap.put("hero1", gareen);
            heroMap.put("hero2", gareen);
             
            System.out.println(heroMap);
             
        }
    }
  • HashSet
    Set中的元素,不能重复
    package collection;
      
    import java.util.HashSet;
      
    public class TestCollection {
        public static void main(String[] args) {
             
            HashSet<String> names = new HashSet<String>();
             
            names.add("gareen");
             
            System.out.println(names);
             
            //第二次插入同样的数据,是插不进去的,容器中只会保留一个
            names.add("gareen");
            System.out.println(names);
        }
    }
  • 没有顺序
    Set中的元素,没有顺序。 
    严格的说,是没有按照元素的插入顺序排列

    HashSet的具体顺序,既不是按照插入顺序,也不是按照hashcode的顺序
    以下是HasetSet源代码中的部分注释
     
    /**
     * It makes no guarantees as to the iteration order of the set; 
     * in particular, it does not guarantee that the order will remain constant over time. 
    */
     
    不保证Set的迭代顺序; 确切的说,在不同条件下,元素的顺序都有可能不一样
    换句话说,同样是插入0-9到HashSet中, 在JVM的不同版本中,看到的顺序都是不一样的。 所以在开发的时候,不能依赖于某种臆测的顺序,这个顺序本身是不稳定的

    package collection;
     
    import java.util.HashSet;
     
    public class TestCollection {
        public static void main(String[] args) {
            HashSet<Integer> numbers = new HashSet<Integer>();
     
            numbers.add(9);
            numbers.add(5);
            numbers.add(1);
     
            // Set中的元素排列,不是按照插入顺序
            System.out.println(numbers);
     
        }
    }
  • 遍历
    Set不提供get()来获取指定位置的元素 
    所以遍历需要用到迭代器,或者增强型for循环
    package collection;
      
    import java.util.HashSet;
    import java.util.Iterator;
      
    public class TestCollection {
        public static void main(String[] args) {
            HashSet<Integer> numbers = new HashSet<Integer>();
             
            for (int i = 0; i < 20; i++) {
                numbers.add(i);
            }
             
            //Set不提供get方法来获取指定位置的元素
            //numbers.get(0)
             
            //遍历Set可以采用迭代器iterator
            for (Iterator<Integer> iterator = numbers.iterator(); iterator.hasNext();) {
                Integer i = (Integer) iterator.next();
                System.out.println(i);
            }
             
            //或者采用增强型for循环
            for (Integer i : numbers) {
                System.out.println(i);
            }
             
        }
    }
  • HashSet和HashMap的关系
    通过观察HashSet的源代码(如何查看源代码)
    可以发现HashSet自身并没有独立的实现,而是在里面封装了一个HashMap.
    HashSet是作为HashMap的key而存在的
    而value是一个命名为PRESENT的static的Object对象,因为是一个类属性,所以只会有一个。
     
    private static final Object PRESENT = new Object();
    package collection;
     
    import java.util.AbstractSet;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Set;
     
    public class HashSet<E>
        extends AbstractSet<E>
        implements Set<E>, Cloneable, java.io.Serializable
    {
        //HashSet里封装了一个HashMap
        private  HashMap<E,Object> map;
     
        private static final Object PRESENT = new Object();
     
        //HashSet的构造方法初始化这个HashMap
        public HashSet() {
            map = new HashMap<E,Object>();
        }
     
        //向HashSet中增加元素,其实就是把该元素作为key,增加到Map中
        //value是PRESENT,静态,final的对象,所有的HashSet都使用这么同一个对象
        public boolean add(E e) {
            return map.put(e, PRESENT)==null;
        }
     
        //HashSet的size就是map的size
        public int size() {
            return map.size();
        }
     
        //清空Set就是清空Map
        public void clear() {
            map.clear();
        }
         
        //迭代Set,就是把Map的键拿出来迭代
        public Iterator<E> iterator() {
            return map.keySet().iterator();
        }
     
    }





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值