Java常用Set集合和常用Map集合

博客 动态
0 163
羽尘
羽尘 2022-07-06 09:01:26
悬赏:0 积分 收藏

Java 常用Set集合和常用Map集合

目录
  • 常用Set集合
    • Set集合的特点
    • HashSet
    • 创建对象
    • 常用方法
    • 遍历
  • 常用Map集合
    • Map集合的概述
  • HashMap
    • 创建对象
    • 常用方法
    • 遍历
  • HashMap的key去重原理

常用Set集合

Set集合的特点

? Set接口下的集合都会有以下特点

  • 不能存储重复元素
  • 没有索引

HashSet

HashSet集合的特点

  • 底层数据结构是哈希表
  • 存储元素的顺序和遍历获取出来的顺序可能不一致
  • 没有索引
  • 集合中不能存储重复元素

创建对象

HashSet<元素数据类型> set = new HashSet<>();
public static void main(String[] args) {    HashSet<String> set = new HashSet<>();}

常用方法

方法解释
boolean add(E e)添加元素,如果元素添加不成功 返回值代表是否添加成功
boolean remove(Object o)删除元素 ,返回值代表删除元素是否成功
boolean contains(Object o)判断元素是否存在
int size()获取集合的大小
    public static void main(String[] args) {        HashSet<String> set = new HashSet<>();        //添加元素        boolean f = set.add("愚");        set.add("生");        set.add("浅");        set.add("末");        System.out.println(f);    }

我们打断点调试一下:

可以看到愚生浅末四个字符已经装入set,且f为true证明添加成功。

我们再试试删除:

 public static void main(String[] args) {        HashSet<String> set = new HashSet<>();        //添加元素        set.add("愚");        set.add("生");        set.add("浅");        set.add("末");        boolean f = set.remove("生");    }

可以看到set已经没有生了,且f为true代表删除成功。

判断是否存在:

    public static void main(String[] args) {        HashSet<String> set = new HashSet<>();        //添加元素        set.add("愚");        set.add("生");        set.add("浅");        set.add("末");        boolean f = set.contains("末");    }


末是存在于set的,所以返回值为true。
获取集合的大小:

 public static void main(String[] args) {        HashSet<String> set = new HashSet<>();        //添加元素        set.add("愚");        set.add("生");        set.add("浅");        set.add("末");         //获取集合的大小        int size = set.size();


添加了愚生浅末四个字符,所以可以得到size是4.

遍历

1.转换为数组遍历

 public static void main(String[] args) {        HashSet<String> set = new HashSet<>();        set.add("愚");        set.add("生");        set.add("浅");        set.add("末");        String[] strings = set.toArray(new String[0]);        for (int i = 0; i < strings.length; i++) {            System.out.println(strings[i]);        }    }

结果:

前面说过:存储元素的顺序和遍历获取出来的顺序可能不一致。

2.使用迭代器遍历

    public static void main(String[] args) {        HashSet<String> set = new HashSet<>();        set.add("愚");        set.add("生");        set.add("浅");        set.add("末");        Iterator<String> it = set.iterator();        while (it.hasNext()){            String s = it.next();            System.out.println(s);        }    }

结果:

3.foreach遍历

 public static void main(String[] args) {        HashSet<String> set = new HashSet<>();        set.add("愚");        set.add("生");        set.add("浅");        set.add("末");        for (String s : set) {            System.out.println(s);        }    }

结果:

常用Map集合

Map集合的概述

Map接口是双列集合的顶层接口,下面是Map接口的定义

interface Map<K,V>  K:键的类型;V:值的类型

? 存储的数据必须包含key和value。

? key和value在Map集合中是一一对应的关系。一个key对应一个value。

? key在map集合中是不会重复的。

HashMap

HashMap集合的特点

  • 底层数据结构是哈希表
  • 存储元素的顺序和遍历获取出来的顺序可能不一致
  • key不会重复

创建对象

HashMap<key的数据类型,value的数据类型> map = new HashMap<>();

例如:

    public static void main(String[] args) {        HashMap<String,String> map = new HashMap<>();        HashMap<String,Integer> map = new HashMap<>();    }

常用方法

方法解释
V put(K key, V value)添加元素,如果key不存在就添加,如果key
V get(Object key)根据key获取对应的value值返回。如果key不存在就返回null
V remove(Object key)根据key删除map中对应的键值对。并且把删除的value返回
boolean containsKey(Object key)判断key是否存在
int size()集合中键值对的对数
void clear()清空集合中的所有键值对
    public static void main(String[] args) {        HashMap<String,String> map = new HashMap<>();//        map.put()        //添加元素        map.put("name", "愷龍");        map.put("age", "20");        String v = map.put("name", "愚生浅末");//将原来的愷龍替换为愚生浅末        String name = map.get("name");//获取名字:愷龍        String age = map.get("age");//获取age:20        //删除元素        String delV = map.remove("age");//返回值为20        //判断key是否存在        if(map.containsKey("name")){            String agea = map.get("name");//null            System.out.println(agea.length());        }        //size        int size = map.size();        map.clear();    }

遍历

1.使用entrySet遍历

? map集合的entrySet方法可以获取一个Set集合,集合中存放的是Entry对象,一个Entry对象相当于一个键值对。我们可以遍历set集合拿到Entry对象,然后获取出里面的键和值。

使用迭代器遍历entrySet

    public static void main(String[] args) {        HashMap<String,String> map = new HashMap<>();        map.put("name","愷龍");        map.put("age","20");        Set<Map.Entry<String, String>> entries = map.entrySet();        //使用迭代器遍历entrySet        Iterator<Map.Entry<String, String>> it = entries.iterator();        while (it.hasNext()){            Map.Entry<String, String> entry = it.next();            System.out.println(entry.getKey()+"="+entry.getValue());        }    }

结果:

使用foreach遍历entrySet

    public static void main(String[] args) {        HashMap<String,String> map = new HashMap<>();        map.put("name","愷龍");        map.put("age","20");        Set<Map.Entry<String, String>> entries = map.entrySet();        //使用foreach遍历entrySet        for (Map.Entry<String, String> entry : entries) {            System.out.println(entry.getKey()+"="+entry.getValue());        }    }

结果:

2.使用keySet遍历

? map集合的keySet方法可以获取一个Set集合,集合中存放的是所有的key。我们可以遍历set集合拿到key对象,然后通过key获取对应的value。

    public static void main(String[] args) {        HashMap<String,String> map = new HashMap<>();        map.put("name","愷龍");        map.put("age","20");        Set<String> keys = map.keySet();        for (String key : keys) {            System.out.println(key+"="+map.get(key));        }    }

结果:

HashMap的key去重原理

? HashMap在添加元素的时候会判断集合中是否有key和本次存入的key相同。判断的时候主要是通过hashCode方法和equals方法来进行判断的。hashCode相同,并且equals判断也相同就会认为是同一个key。

? 所以如果我们要存储到HashMap中的key是一个自定义的类型。就需要根据情况判断下是否需要重写下hashCode方法和equals方法。重写的时候使用IDEA的提示即可。

public class Student {    private int age;    private String name;    public String getName(){        return name = this.name;    }    public void setName(String name){        this.name = name;    }    public int getAge(){        return age = this.age;    }    public void setAge(int age){        this.age = age;    }    @Override    public boolean equals(Object o) {        if (this == o) return true;        if (o == null || getClass() != o.getClass()) return false;        Student student = (Student) o;        return age == student.age &&                Objects.equals(name, student.name);    }    @Override    public int hashCode() {        return Objects.hash(age, name);    }}

? 注意:HashSet存储数据其实也是使用了HashMap。所以如果往HashSet中存储自定义对象也要看情况是否需要重写hashCode方法和equals方法。

如图片失效等情况请参阅公众号文章:https://mp.weixin.qq.com/s/NLKGamezcB3nnQ3BgEDc9g

欢迎关注我的公众号:愚生浅末,一起交流学习。

posted @ 2022-07-06 08:33 愚生浅末 阅读(28) 评论(0) 编辑 收藏 举报
回帖
    羽尘

    羽尘 (王者 段位)

    2335 积分 (2)粉丝 (11)源码

     

    温馨提示

    亦奇源码

    最新会员