JAVA基础 day10 集合Set,Map

Set

Set接口
特点:1.无序,没有下标 2.元素不能重复(添加重复只会没有,不会报错)

利用 Set 元素唯一的特性,可以快速对一个集合进行去重操作,避免使用 List 的
contains 方法进行遍历、对比、去重操作

遍历方式
1.增强for 2.迭代器iterator

方法全部继承与Collections

使用list为set赋值,会清除其中的重复元素

HashSet

存储结构:哈希表
用的是hashMap,用key来存储元素。
存储依据:基于HashCode,equals实现元素不重复。

存储过程:
根据hashCOde计算储存的位置,如果此位置没有元素,则添加
如果此位置有元素,再比较equals,如果equals相同,则拒绝添加

存储:hashtable(本质数组+链表)
JDK1.7(数组+链表) JDK1.8(数组+链表+红黑树)

system.identityHashCode();得到原始的HashCode值

LinkedHashSet

有序,没有下标,不能重复
Linked:链表保存添加元素的顺序

TreeSet

存储结构是红黑树,(前,中,后序遍历)
特点:
1.无序(不按添加顺序),没有下标 2不能重复 3.对元素进行排序

重复依据:
Comparable接口中的comapareTo(),如果该方法返回0,则为重复元素,拒绝添加;

用的是TreeMap,用key存储元素。
在这里插入图片描述
存入的引用类型都必须实现comparable接口,并重写其中的comparaTo方法,不用重写equals和hashCode了

class Student1 implements Comparable<Student1>{//对该接口使用泛型
	@Override
    public int compareTo(Student1 o) {
        int n1=this.age-o.getAge();
        int n2=this.name.compareTo(o.getName());
        return n1==0?n2:n1;
    }
}

数字可以相减比较,字符串用自带comparaTo比较;先比较年龄,在比较名字,年龄相同,再比较名字

比较器Comparator接口
比较器的优先级高于元素的排序规则(重写的comparaTo方法)

public static void main(String[] args) {
        TreeSet<String> treeSet=new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int n1=o1.length()-o2.length();
                int n2=o1.compareTo(o2);
                return n1==0?n2:n1;
            }
        });
        treeSet.add("chichi");
        treeSet.add("yan");
        treeSet.add("he");
        treeSet.add("ce");
        System.out.println(treeSet.toString());
    }

Map

在这里插入图片描述

在这里插入图片描述
键不可重复,值可重复
在这里插入图片描述
删除:remove(key);
清空:clear()
Map遍历
keySet()遍历:

 		Set<Student> set = map.keySet();
        for (Student student : set) {
            System.out.println(student.toString()+map.get(student));
        }

entrySet()遍历:

		Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            System.out.println(entry.getKey()+"------"+entry.getValue());
        }

containsKey(key):是否包含key
contiansValue(value):是否包含value

HashMap

存储结构hashtable
key不能重复的依据:hashCode+equals

线程不安全速率快,;允许用null 作为key或是value

存储:hashtable(本质数组+链表)
JDK1.7(数组+链表) JDK1.8(数组+链表+红黑树)

LinkedHashMap 有序,使用链表

HashMap源码分析:
在这里插入图片描述

hashcode的计算
拿高16位,与低16进行异或运算

在这里插入图片描述
每次扩容变为2倍 16-》32 (从概念上讲,这样扩容上图中的元素的一半元素不需要改动)

TreeSet

存储结构:红黑树

Hashtable:

JDK1.0版本,线程安全,运行效率慢;不允许null作为key或是value。

Properties

1.Hashtable的子类,要求key和value都是String。通常用于配置文件的读取
2.没有泛型,存储的是属性名和属性值
3.和流有关
添加不用put,用setProperty();
remove(删除元素)

写配置文件时,要注意不能有引号,也不能有空格

独有遍历方法(加上Map的,一共三个):

		Properties properties = new Properties();
        properties.setProperty("1","chi");//元素添加
        properties.setProperty("2","yan");
        properties.setProperty("3","he");
        Set<String> strings = properties.stringPropertyNames();//获得键名
        for (String s : strings) {
            System.out.println(s+"--"+properties.getProperty(s));
        }

获取系统属性

		Properties properties1 = System.getProperties();
        Set<String> strings1 = properties1.stringPropertyNames();
        for (String s : strings1) {
            System.out.println(s+"---"+properties1.getProperty(s));
        }

总结

hashSet,hashMap重写hashCode和equals,实现去重;
TreeSet,TreeMap实现Comparable接口,实现去重与比较大小
ArrayList():重写equals()方法
LinkedList():重写equals()方法
HashSet():重写hashCode()方法和equals()方法
LinkedSet():重写hashCode()方法和equals()方法
TreeSet():实现Comparable接口,重写compareTo()方法。
hashMap():重写hashCode()方法和equals()方法
TreeMap():实现Comparable接口,重写compareTo()方法。
重写了这些方法,才能进行添加和删除。要不会出现添加和删除的问题。

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值