数据结构之Set和Map

1、HashSet:

java 复制代码
package com.datastructure.set;

import java.util.HashSet;
//不允许重复元素
public class HashSetDemo {
    public static void main(String[] args) {
        HashSet<String> set = new HashSet<>();

        // 添加元素
        set.add("apple");
        set.add("banana");
        set.add("orange");
        set.add("mango");
        set.add("mango");
        System.out.println("HashSet: " + set); // 输出: HashSet: [apple, banana, orange, mango]

        // 检查元素是否存在
        boolean containsApple = set.contains("apple");
        System.out.println("Contains 'apple': " + containsApple); // 输出: Contains 'apple': true

        // 删除元素
        set.remove("banana");
        System.out.println("HashSet: " + set); // 输出: HashSet: [apple, orange, mango]

        // 获取元素数量
        int size = set.size();
        System.out.println("Size of HashSet: " + size); // 输出: Size of HashSet: 3

        // 清空集合
        set.clear();
        System.out.println("HashSet: " + set); // 输出: HashSet: []
    }
}

2、TreeSet:

java 复制代码
package com.datastructure.set;
import java.util.TreeSet;
//TreeSet是有序的,输出结果会按照升序排列。
//TreeSet还提供了一些便捷的方法来获取小于等于、严格小于、大于等于、严格大于给定元素的最值元素,
//分别是floor(), lower(), ceiling()和higher()方法。
//使用TreeSet,可以方便地存储一组有序的不重复元素,并且可以高效地执行判断、添加、删除等操作。
public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet<Integer> set = new TreeSet<>();

        // 添加元素
        set.add(5);
        set.add(2);
        set.add(8);
        set.add(3);
        System.out.println("TreeSet: " + set); // 输出: TreeSet: [2, 3, 5, 8]

        // 检查元素是否存在
        boolean contains5 = set.contains(5);
        System.out.println("Contains 5: " + contains5); // 输出: Contains 5: true

        // 删除元素
        set.remove(8);
        System.out.println("TreeSet: " + set); // 输出: TreeSet: [2, 3, 5]

        // 获取第一个和最后一个元素
        int first = set.first();
        int last = set.last();
        System.out.println("First element: " + first); // 输出: First element: 2
        System.out.println("Last element: " + last); // 输出: Last element: 5

        // 获取小于等于给定元素的最大元素
        int floor = set.floor(4);
        System.out.println("Floor of 4: " + floor); // 输出: Floor of 4: 3

        // 获取严格小于给定元素的最大元素
        int lower = set.lower(4);
        System.out.println("Lower of 4: " + lower); // 输出: Lower of 4: 3

        // 获取大于等于给定元素的最小元素
        int ceiling = set.ceiling(4);
        System.out.println("Ceiling of 4: " + ceiling); // 输出: Ceiling of 4: 5

        // 获取严格大于给定元素的最小元素
        int higher = set.higher(4);
        System.out.println("Higher of 4: " + higher); // 输出: Higher of 4: 5

        // 清空集合
        set.clear();
        System.out.println("TreeSet: " + set); // 输出: TreeSet: []
    }
}

3、HashMap:

java 复制代码
package com.datastructure.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
//HashMap可以存储不重复的键和对应的值,并且可以根据键来获取对应的值。
//首先创建一个HashMap对象,并使用put()方法来添加键值对。键的类型通常是String,值可以是任意类型。
//使用get()方法根据键来获取对应的值,使用containsKey()方法来检查键是否存在,使用remove()方法来删除键值对。
//还可以使用entrySet()方法来获取键值对的Set视图,然后通过遍历Set视图的方式来遍历HashMap。
//在遍历过程中,可以使用getKey()和getValue()方法分别获取键和值。
//HashMap并不保证键值对的顺序,如果需要有序的存储和访问,可以考虑使用LinkedHashMap。
public class HashMapDemo {
    public static void main(String[] args) {
        // 创建HashMap对象
        HashMap<String, Integer> map = new HashMap<>();

        // 添加键值对
        map.put("apple", 5);
        map.put("banana", 2);
        map.put("orange", 3);

        // 获取值
        int appleCount = map.get("apple");
        System.out.println("Apple count: " + appleCount); // 输出: Apple count: 5
        
        // 检查键是否存在
        boolean containsBanana = map.containsKey("banana");
        System.out.println("Contains banana: " + containsBanana); // 输出: Contains banana: true
        
        // 删除键值对
        map.remove("orange");
        System.out.println("HashMap: " + map); // 输出: HashMap: {apple=5, banana=2}
        
        // 遍历HashMap
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            int value = entry.getValue();
            System.out.println(key + ": " + value);
        }
        /*
        输出:
        apple: 5
        banana: 2
        */
        // 使用迭代器,遍历linkedHashMap,并打印键值对
        Iterator<Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, Integer> entry = iterator.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        } 
        // 清空HashMap
        map.clear();
        System.out.println("HashMap: " + map); // 输出: HashMap: {}
    }
}

4、TreeMap:

java 复制代码
package com.datastructure.map;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
//TreeMap是实现了SortedMap接口的集合,它基于红黑树的数据结构来存储数据.
//并且可以根据键的自然顺序或自定义顺序进行排序。
//首先创建一个TreeMap对象,并使用put()方法来添加键值对。
//与HashMap不同的是,TreeMap会根据键的自然顺序进行排序,如果键是自定义类型,则需要实现Comparable接口或提供Comparator对象来指定排序规则。
//然后,使用get()方法根据键来获取对应的值,使用containsKey()方法来检查键是否存在,使用remove()方法来删除键值对。
//还可以使用entrySet()方法来获取键值对的Set视图,然后通过遍历Set视图的方式来遍历TreeMap。最后,使用clear()方法来清空TreeMap。
//TreeMap是一种有序的集合,它通过红黑树结构来存储数据,可以快速地插入、删除和查找元素,同时还可以根据键的顺序进行排序。
//由于红黑树结构的存在,TreeMap的插入、删除和查找操作的时间复杂度是O(logN),相对于HashMap的O(1)会稍微慢一些。
public class TreeMapDemo {
    public static void main(String[] args) {
        // 创建TreeMap对象
        TreeMap<String, Integer> map = new TreeMap<>();

        // 添加键值对
        map.put("apple", 5);
        map.put("banana", 2);
        map.put("orange", 3);

        // 获取值
        int appleCount = map.get("apple");
        System.out.println("Apple count: " + appleCount); // 输出: Apple count: 5
        
        // 检查键是否存在
        boolean containsBanana = map.containsKey("banana");
        System.out.println("Contains banana: " + containsBanana); // 输出: Contains banana: true
        
        // 删除键值对
        map.remove("orange");
        System.out.println("TreeMap: " + map); // 输出: TreeMap: {apple=5, banana=2}
        
        // 遍历TreeMap
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            int value = entry.getValue();
            System.out.println(key + ": " + value);
        }
        /*
        输出:
        apple: 5
        banana: 2
        */
     // 使用迭代器,遍历TreeMap,并打印键值对
        Iterator<Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, Integer> entry = iterator.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        } 
        // 清空TreeMap
        map.clear();
        System.out.println("TreeMap: " + map); // 输出: TreeMap: {}
    }
}

5、LinkedHashMap:

java 复制代码
package com.datastructure.map;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
//LinkedHashMap会保持插入顺序,因此输出结果的顺序与添加键值对的顺序一致。
public class LinkedHashMapExample {
    public static void main(String[] args) {
        // 创建一个LinkedHashMap实例
        LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<>();

        // 向linkedHashMap中添加键值对
  
        linkedHashMap.put(1, "One");
        linkedHashMap.put(2, "Two");
        linkedHashMap.put(3, "Three");
        linkedHashMap.put(4, "Four");
        linkedHashMap.put(5, "Five");

        // 遍历linkedHashMap,并打印键值对
        for (Map.Entry<Integer, String> entry : linkedHashMap.entrySet()) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }
        // 使用迭代器,遍历linkedHashMap,并打印键值对
        Iterator<Map.Entry<Integer, String>> iterator = linkedHashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, String> entry = iterator.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        } 
    }
}
相关推荐
苹果酱056719 分钟前
一文读懂SpringCLoud
java·开发语言·spring boot·后端·中间件
掐指一算乀缺钱40 分钟前
SpringBoot 数据库表结构文档生成
java·数据库·spring boot·后端·spring
晚睡早起₍˄·͈༝·͈˄*₎◞ ̑̑1 小时前
苍穹外卖学习笔记(七)
java·windows·笔记·学习·mybatis
就这个java爽!1 小时前
JAVA网络编程【基于TCP和UDP协议】超详细!!!
java·开发语言·网络·tcp/ip·udp·eclipse·idea
一叶飘零_sweeeet1 小时前
为什么 Feign 要用 HTTP 而不是 RPC?
java·网络协议·http·spring cloud·rpc·feign
天下无贼!1 小时前
2024年最新版Vue3学习笔记
前端·vue.js·笔记·学习·vue
Jiaberrr1 小时前
JS实现树形结构数据中特定节点及其子节点显示属性设置的技巧(可用于树形节点过滤筛选)
前端·javascript·tree·树形·过滤筛选
赵啸林1 小时前
npm发布插件超级简单版
前端·npm·node.js
懒洋洋大魔王1 小时前
7.Java高级编程 多线程
java·开发语言·jvm
茶馆大橘1 小时前
【黑马点评】已解决java.lang.NullPointerException异常
java·开发语言