集合框架——HashSet集合、Map集合

红黑树(了解)

1.对于每一个节点,都有一个颜色属性要么是黑色,要么是红色
2.根节点是黑色的
3.对于任意一个节点没有子节点或者没有父节点,我们就把它的子节点称之为叶子节点(Nil)
4.对于一个红色的节点,它的子节点必须是黑色
5.对于任意一个节点,到其叶子节点的简单路径的黑色节点数目相同

添加元素的默认颜色:
	1.默认节点的颜色为红色
  • 添加元素,如果打破了红黑规则,如何在恢复红黑规则。

在这里插入图片描述

HashSet集合

HashSet集合元素是没有索引的,元素是无序的,元素不能重复

HashSet<String> st=new HashSet<>(); 
//添加
st.add("java");
st.add("world");
st.add("hello");

//遍历输出
for(String s: st){
    System.out.println(s);
}

HashSet底层原理(JDK1.7)

底层数据结构:哈希表结构(数组+链表)

当往HashSet集合中添加元素时,底层做了哪些事情?
	1.计算元素的哈希值,底层会自动调用hashCode()方法
	2.通过哈希值计算在数组中的存储位置,并判断这个位置是否为nul
		如果为null,直接存储
		如果不为null, 继续使用equals方法判断元素的equals是否相同
	3.如果hashCode值相同,但是equals不同,以链表的形式存储在数组的同一个索引位置。
	  如果hashCode值相同,但是equals也相同,就认为元素重复【不存储】

HashSet底层原理(JDK1.8)

底层数据结构:哈希表结构(数组+链表+红黑树)

当往HashSet集合中添加元素时,底层做了哪些事情?
	1.计算元素的哈希值,底层会自动调用hashCode()方法
	2.通过哈希值计算在数组中的存储位置,并判断这个位置是否为nul
		如果为null,直接存储
		如果不为null, 继续使用equals方法判断元素的equals是否相同
	3.如果hashCode值相同,但是equals不同,以链表的形式存储在数组的同一个索引位置。
		但是如果链表的长度超过8位,就转换为红黑树存储(提高查找的效率)
	  如果hashCode值系统,但是equals也相同,就认为元素重复【不存储】

Map集合

Map集合存储的元素是以【键值对】形式存在的;其中键不能重复,值可以重复。

HashMap<String,String>  map = new HashMap<String,String>();

//添加元素
map.put("张三","20");
map.put("李四","30");
map.put("王五","40");

System.out.println(map);

Map集合的常见方法

public V put(K key, V value)  
    往Map集合中添加键和值
    注意:如果键重复,会用新的值替换旧的值。
public V get(Object key) 
    获取Map集合中键对应的值
public V remove(Object key)  
    根据键移除Map集合中的键值对
    注意:被移除的值返回
public boolean containsKey(Object key)
    判断是否包含键
public boolean containsValue(Object key)
    判断是否包含值
public void clear()
    清空集合中所有的键值对
public boolean isEmpty()
    判断集合是否为空
public  int size()  
    获取集合的长度

Map集合的遍历

public Set<K> keySet()  
    获取键的集合
public Collection<V> values()  
    获取值的集合
public Set<Map.Entry<K,V>> entrySet()  
    获取键值对组成的集合
  • 根据键找值方式,遍历Map集合
HashMap<String,String> map=new HashMap<>();

map.put("王宝强","马蓉");
map.put("贾乃亮","李小璐");
map.put("李晨","范冰冰");

//获取键的集合
Set<String> keys = map.keySet();
//遍历获取每一个键
for (String key : keys) {
    //通过键找值
    String value = map.get(key);
    System.out.println(key+"..."+value);
}
  • 获取所有的【键值对】对象,获取键值对的键,获取键值对的值。
HashMap<String,String> map=new HashMap<>();
map.put("王宝强","马蓉");
map.put("贾乃亮","李小璐");
map.put("李晨","范冰冰");

//获取【键值对Entry】组成的Set集合
Set<Map.Entry<String, String>> entrys = map.entrySet();

//遍历Set集合,获取每一个Entry对象
for (Map.Entry<String, String> entry : entrys) {
    //获取Entry对象的键
    String key = entry.getKey();
    //获取Entry对象的值
    String value = entry.getValue(); 
    System.out.println(key+"...."+value);
}
  • HashMap练习
public class Demo5 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String str = sc.next();

        //键:字符
        //值:字符的个数
        HashMap<Character,Integer> map=new HashMap<>();

        //遍历字符串中的字符
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            //判断是否包含键
            if(map.containsKey(ch)){
                //获取值+1,再存回去
                Integer value = map.get(ch);
                map.put(ch,value+1);
            }else {
                map.put(ch,1);
            }
        }

        //遍历map集合
        Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
        for (Map.Entry<Character, Integer> entry : entrys) {
            Character key = entry.getKey();
            Integer value = entry.getValue();
            System.out.print(key+"("+value+")"+" "); 
        } 
    }
}

TreeMap集合

TreeMap底层数据结构其实就是二叉树结构,可以对键进行排序。 键需要实现Comparable接口才能进行排序

public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

    @Override
    public int compareTo(Student o) {
        //按照年龄的升序排列
        return this.age-o.age;
    }
}
TreeMap<Student, String> map = new TreeMap<>();

map.put(new Student("张三",20),"武汉");
map.put(new Student("李四",27),"南京");
map.put(new Student("王五",32),"上海");
map.put(new Student("赵六",18),"广州");
map.put(new Student("张三",20),"深圳");

//获取所有的【键值对】
Set<Map.Entry<Student, String>> entrys = map.entrySet();

for (Map.Entry<Student, String> entry : entrys) {
    Student key = entry.getKey();
    String value = entry.getValue();
    System.out.println(key+"..."+value);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值