Java集合—Set集和Map集

一、Set集合

  1.概述

    Set集合无序的、不可重复的元素(无序是指索引)

    Set集合不按照特定的方法进行排序,只是将元素放在集合中。

    下面介绍一下Set集合的HashSet和TreeSet两个实现类。

  2.HashSet集合

  概述:

    public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable

    Hash(哈希算法) 哈希函数定义的好坏决定了哈希算法的好坏,可以避免数据倾斜。

    HashCode  哈希值

    (1)使用equals()方法  可以判断两个元素的HashCode值是否相同

    (2)如果HashCode值相同,继续与集合的元素作比较,

        如果元素值也相同则视为同一个对象,将不保存在HashSet中;

        如果元素值相同即不是同一个对象,理论上要存储该元素,但比较麻烦,应避免出现该情况!

    (3)如果HashCode值不相同,直接把元素存储在该元素的HashCode位置。

  构造方法:

    HashSet<E> hashSet = new HashSet<E>();

  主要方法:

    boolean add(E e) -->将指定的元素添加到此集合(如果尚未存在)。 
      boolean contains(Object o) -->如果此集合包含指定的元素,则返回 true 。
      int hashCode() -->返回此集合的哈希码值。

  注意:在使用HashSet存储自定义数据类型的元素时,要在封装类中重写HashCode、equals、toString方法!!!

/**
 * @ author: PrincessHug
 * @ date: 2019/2/10, 3:17
 * @ Blog: https://www.cnblogs.com/HelloBigTable/
 */
public class Student {
    private String id;
    private String name;
    private char sex;

    public Student() {
    }

    public Student(String id, String name, char sex) {
        this.id = id;
        this.name = name;
        this.sex = sex;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {return true;}
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id &&
                sex == student.sex &&
                name.equals(student.name);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    @Override
    public String toString() {
        return "id=" + id + ", name='" + name + '\'' + ", sex=" + sex;
    }
}


public class HashSetDemo {
    public static void main(String[] args) {
        HashSet<Student> hs = new HashSet<Student>();
        Student s1 = new Student("20190201", "Wyh", '男');
        Student s2 = new Student("20190202", "Fyh", '男');
        Student s3 = new Student("20190203", "Zyq", '男');
        System.out.println("添加Wyh是否成功"+hs.add(s1));
        System.out.println("添加Fyh是否成功"+hs.add(s2));
        System.out.println("添加Wyh是否成功"+hs.add(s3));
        for (Student s:hs){
            System.out.println(s.hashCode());
        }
    }
}

  

二、Map集合

  1.概述

    Set与List都属于Collection,而Map与Collection平行。

    Map类型每个元素的值都包含两个对象:key-value键值对

    key不能够重复,唯一的key可以对应多个value;

    Map集合中不存储索引,key值即相当于索引。

  2.HashMap

  概述:

    public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

    允许null值和null键,基于哈希表实现的Map接口。

  构造方法:

    HashMap<key,value> hm = new HashMap<key,value>();

  主要方法:

   V put(K key, V value) =>将指定的值与此映射中的指定键相关联。
     Set<K> keySet() =>返回此地图中包含的键的Set视图。
     V get(Object key) =>返回到指定键所映射的值,或 null如果此映射包含该键的映射。
     boolean containsKey(Object key) =>如果此映射包含指定键的映射,则返回 true  
     boolean containsValue(Object value) =>如果此地图将一个或多个键映射到指定值,则返回 true 。 
     Set<Map.Entry<K,V>> entrySet() =>返回此地图中包含的映射的Set视图。

  遍历HashMap的两种方法:

    Set<K> keySet() =>返回map中包含的键的Set视图

    Set<Map.Entry<K,V>> entrySet() => 返回map中包含的映射Set视图

      Map.Entry见最后补充!

/**
 * @ author: PrincessHug
 * @ date: 2019/2/10, 9:29
 * @ Blog: https://www.cnblogs.com/HelloBigTable/
 */
public class HashMapDemo {
    public static void main(String[] args) {
        HashMap<String, Student> hm = new HashMap<>();
        Student s1 = new Student("20180201", "Wyh", '男');
        Student s2 = new Student("20180202", "Gam", '女');
        Student s3 = new Student("20180203", "Wqs", '男');
        hm.put("000001",s1);
        hm.put("000002",s2);
        hm.put("000003",s3);

        System.out.println("遍历HashMap集合的方法一:entrySet()");
        Iterator<Map.Entry<String, Student>> it = hm.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Student> entry = (Map.Entry<String,Student>)it.next();
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        System.out.println("遍历HashMap集合的方法二:keySet()");
        Iterator<String> it2 = hm.keySet().iterator();
        while (it2.hasNext()){
            String key = it2.next();
            System.out.println("key:" + key + "value:" + hm.get(key));
        }
    }
}

  2.Hashtable

   概述:

    Hashtable实现了一个哈希表,它将键映射到值。任何非null对象都可以作为键或值

    为了成功在Hashtable中存储和获取对象,用作键的对象必须实现HashCode和equals

   构造方法:

    Hashtable<String,Strudent> ht =  new Hashtable<String,Strdent>();

   主要方法:

    void get(Object key) =>返回指定键所映射的值

    void put(K key,V value) =>将指定的键映射的值设为value

   遍历Hashtable的三种方法:

    Enumeration<K> keys() => 返回次散列表中键的枚举

    Set<Map,Entry<K,V>> entrySet() =>返回次地图中包含的映射的Set视图

    Set<K> keySet() =>返回此map中包含的key的Set视图

      Enumeration见最后补充!

/**
 * @ author: PrincessHug
 * @ date: 2019/2/10, 9:49
 * @ Blog: https://www.cnblogs.com/HelloBigTable/
 */
public class HashtableDemo {
    public static void main(String[] args) {
        Hashtable<String, Student> ht = new Hashtable<>();
        Student s1 = new Student("20180101", "Wyh", '男');
        Student s2 = new Student("20180102", "Gam", '女');
        Student s3 = new Student("20180203", "Wqs", '男');
        ht.put("0001",s1);
        ht.put("0002",s2);
        ht.put("0003",s3);

        System.out.println("遍历Hashtable的方法一:keys()");
        Enumeration<String> keys = ht.keys();
        while (keys.hasMoreElements()){
            String key = keys.nextElement();
            System.out.println("key:" + key + ",value:" + ht.get(key));
        }

        System.out.println("遍历Hashtable的方法二:entrySet()");
        Iterator<Map.Entry<String, Student>> it = ht.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<String, Student> entry = (Map.Entry<String, Student>) it.next();
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        System.out.println("遍历Hashtable的方法三:keySet()");
        Iterator<String> it1 = ht.keySet().iterator();
        while (it1.hasNext()){
            String key1 = it1.next();
            System.out.println("key:" + key1 + ",value:" + ht.get(key1));
        }
    }
}

  ***补充:

  1.Map.Entry

  public static interface Map.Entry<K,V>

  它属于Map接口(里面包含键值对),Set(Map.Entry<K,V>)方法返回地图的集合视图,其元素属于此接口类,获取对映射条目的引用的唯一方法是从该集合视图的迭代器。

  主要方法:

    K getKey() 

    V getValue()

  2.Enumeration

  public interface Enumeration<E>

  实现枚举接口的对象生成一系列元素,一次一个;

  类似于Iterator接口对集合进行迭代

  主要方法:

    boolean hasMoreElements() =>测试此枚举是否包含更多元素。

    E nextElement() =>如果此枚举对象至少有一个要提供的元素,则返回此枚举的下一个元素。

 

转载于:https://www.cnblogs.com/HelloBigTable/p/10401879.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值