数据结构之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());
        } 
    }
}

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值