iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java常用集合之Set和Map的用法详解
  • 616
分享到

Java常用集合之Set和Map的用法详解

2024-04-02 19:04:59 616人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

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

常用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方法。

以上就是Java常用集合之Set和Map的用法详解的详细内容,更多关于Java Set Map集合的资料请关注编程网其它相关文章

--结束END--

本文标题: Java常用集合之Set和Map的用法详解

本文链接: https://www.lsjlt.com/news/153590.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作