数据结构之Set和Map

1、HashSet:

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:

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:

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:

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:

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());
        } 
    }
}

相关推荐

  1. 数据结构SetMap

    2024-04-05 04:40:02       16 阅读
  2. 数据结构MapSet

    2024-04-05 04:40:02       19 阅读
  3. 【ES6】SetMap数据结构

    2024-04-05 04:40:02       16 阅读
  4. 【09】ES6:Set Map 数据结构

    2024-04-05 04:40:02       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-05 04:40:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-05 04:40:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-05 04:40:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-05 04:40:02       20 阅读

热门阅读

  1. Mybatis中如何将多个查询结果封装为一个对象列表

    2024-04-05 04:40:02       17 阅读
  2. 开源项目-CSDN专栏

    2024-04-05 04:40:02       13 阅读
  3. bash例子-source进程替换、alias不生效处理

    2024-04-05 04:40:02       15 阅读
  4. Bash相关

    2024-04-05 04:40:02       13 阅读
  5. sqlalchemy的Session

    2024-04-05 04:40:02       13 阅读
  6. 中医肝胆笔记

    2024-04-05 04:40:02       18 阅读
  7. uniapp路由传参存在数据类型失真的问题

    2024-04-05 04:40:02       18 阅读
  8. 企业为什么选择高防服务器?

    2024-04-05 04:40:02       15 阅读