Java中HashMap和HashTable的常用方法

一.HashMap

①.put(K key, V value) 将键(key)/值(value)映射存放到Map集合中。

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("Tom", 100);//向HashMap中添加元素  
    }
}


②.get(Object key) 返回指定键所映射的值,没有该key对应的值则返回 null,即获取key对应的value。

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("Tom", 100);
        map.put("Tom", 0);
        int score = map.get("Tom");// 获取key对应的value
        System.out.println(score);// key不允许重复,若重复,则覆盖已有key的value
    }
}


③. size()  返回Map集合中数据数量,准确说是返回key-value的组数。

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("Tom", 100);
        map.put("Jim", 90);
        map.put("Sam", 91);
        System.out.println(map.size());
    }
}


④:clear() 清空Map集合。

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("Tom", 100);
        map.put("Jim", 90);
        map.put("Sam", 91);
        map.clear();// 清空map中的key-value
        System.out.println(map.size());
    }
}


⑤:isEmpty () 判断Map集合中是否有数据,如果没有则返回true,否则返回false。

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("Tom", 100);
        map.put("Jim", 90);
        map.put("Sam", 91);
        map.clear();// 清空map中的key-value
        System.out.println(map.isEmpty());
    }
}


⑥:remove(Object key) 删除Map集合中键为key的数据并返回其所对应value值。

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("Tom", 100);
        map.put("Jim", 90);
        map.put("Sam", 91);
        map.remove("Tom");
        System.out.println(map);
    }
}


⑦:containsKey(Object key) Hashmap判断是否含有key。

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
         /*boolean*///判断map中是否存在这个key
         System.out.println(map.containsKey("DEMO"));//false
         map.put("DEMO", 1);
         System.out.println(map.containsKey("DEMO"));//true
    }
}


⑧:containsValue(Object value) Hashmap判断是否含有value。

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///判断map中是否存在这个value
        System.out.println(map.containsValue(1));//false
        map.put("DEMO", 1);
        System.out.println(map.containsValue(1));//true
    }
}


⑨:putAll()Hashmap添加另一个同一类型的map下的所有数据。

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        HashMap<String, Integer> map1=new HashMap<>();
        /*void*///将同一类型的map添加到另一个map中
        map1.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO2=2}
        map.putAll(map1);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
    }
}


⑩:replace()Hashmap替换这个key的value。

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*value*///判断map中是否存在这个key
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
        System.out.println(map.replace("DEMO2", 1));//2
        System.out.println(map);//{DEMO1=1, DEMO2=1}
    }
}

二.HashTable

2.创建 Hashtable
使用默认构造函数创建一个空的 Hashtable。
可以指定初始容量和负载因子。

import java.util.Hashtable;

public class HashtableExample {
    public static void main(String[] args) {
        // 创建一个空的 Hashtable
        Hashtable<String, Integer> hashtable = new Hashtable<>();

        // 创建具有初始容量的 Hashtable
        Hashtable<String, Integer> hashtableWithCapacity = new Hashtable<>(10);

        // 创建具有初始容量和负载因子的 Hashtable
        Hashtable<String, Integer> hashtableWithLoadFactor = new Hashtable<>(10, 0.75f);
    }
}


3.添加元素
使用 put() 方法添加键值对。
键和值都不能为 null,否则会抛出 NullPointerException。

hashtable.put("Apple", 10);
hashtable.put("Banana", 20);
hashtable.put("Cherry", 30);
System.out.println(hashtable);


4.访问元素
使用 get() 方法通过键访问值。
如果键不存在,返回 null。

int quantity = hashtable.get("Banana");
System.out.println("Banana quantity: " + quantity); 


5.删除元素
使用 remove() 方法通过键删除键值对。

hashtable.remove("Cherry");
System.out.println("After removal: " + hashtable); 


6.检查键或值是否存在
使用 containsKey() 方法检查是否包含指定键。
使用 containsValue() 方法检查是否包含指定值。

boolean containsApple = hashtable.containsKey("Apple");
System.out.println("Contains Apple: " + containsApple); // 输出: Contains Apple: true

boolean containsValue30 = hashtable.containsValue(30);
System.out.println("Contains value 30: " + containsValue30); // 输出: Contains value 30: false


7.获取大小
使用 size() 方法获取键值对的数量。

int size = hashtable.size();
System.out.println("Size: " + size);


8. 遍历 Hashtable
使用 Enumeration 遍历键或值。
使用 entrySet() 遍历键值对。

// 使用 Enumeration 遍历键
Enumeration<String> keys = hashtable.keys();
while (keys.hasMoreElements()) {
    String key = keys.nextElement();
    System.out.println("Key: " + key + ", Value: " + hashtable.get(key));
}

// 使用 entrySet() 遍历键值对
for (Map.Entry<String, Integer> entry : hashtable.entrySet()) {
    System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}


9. 清空 Hashtable
使用 clear() 方法清空所有键值对。

hashtable.clear();
System.out.println("After clear: " + hashtable);

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值