Java中HashMap常用方法示例、使用Map和HashMap定义的区别及定义ArrayList及HashMap等数据结构时 HashMap<K,V> should be parameterized

一、Java中HashMap常用方法示例以及使用Map和HashMap定义的区别

1. Java  中HashMap常用的方法列表如下:

put() 将键/值对添加到 hashMap 中
putAll() 将所有键/值对添加到 hashMap 中
putIfAbsent() 如果 hashMap 中不存在指定的键,则将指定的键/值对插入到 hashMap 中。
get() 获取指定 key 对应对 value
getOrDefault() 获取指定 key 对应对 value,如果找不到 key ,则返回设置的默认值
replace() 替换 hashMap 中是指定的 key 对应的 value。
replaceAll() 将 hashMap 中的所有映射关系替换成给定的函数所执行的结果。
containsKey() 检查 hashMap 中是否存在指定的 key 对应的映射关系。
containsValue() 检查 hashMap 中是否存在指定的 value 对应的映射关系。
remove() 删除 hashMap 中指定键 key 的映射关系
clear() 删除 hashMap 中的所有键/值对
isEmpty() 判断 hashMap 是否为空
size() 计算 hashMap 中键/值对的数量
keySet() 返回 hashMap 中所有 key 组成的集合视图。
values() 返回 hashMap 中存在的所有 value 值。
merge() 添加键值对到 hashMap 中
forEach() 对 hashMap 中的每个映射执行指定的操作。

2.Java  中HashMap常用的方法使用示例如下:

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Struct {

	public static void main(String[] args) {
	
		//HashMap的定义,使用Map定义
		Map<String, String> map1 = Map.of("name","04007", "age","100", "sex","man");

		//HashMap的定义,使用HashMap定义
		HashMap<Integer, String> map2 = new HashMap<Integer, String>();
		map2.put(1, "company");
		map2.put(2,"Technology");
		
		//输出方式1:使用Arrays.asList
		System.out.println(Arrays.asList(map1));
		System.out.println(Arrays.asList(map2));
		
		//输出方式2:迭代输出
		for(String key: map1.keySet()) {
			System.out.println(key +":"+map1.get(key));
		}
		for(Integer key: map2.keySet()) {
			System.out.println(key +":"+ map2.get(key));
		}
		
		//输出方式3:直接打印hashmap
		System.out.println(map1);
		System.out.println(map2);
		
		//输出方式4:Collections.singletonList
		System.out.println(Collections.singletonList(map1));
		System.out.println(Collections.singletonList(map2));
		
		//计算hashmap大小
		System.out.println(map1.size());
		System.out.println(map2.size());
		
		//取得keys
		System.out.println(map2.keySet());
		
		//替换元素
		map2.replace(1, "first");
		System.out.println(map2);
		
		//map合并putall
		HashMap<Integer, String> map3 = new HashMap<Integer, String>();
		map3.put(5, "history");
		map3.put(9, "sport");
		map3.putAll(map2);
		System.out.println(map3);
		
		//健值存在性判断
		System.out.println(map3.containsKey(9));
		System.out.println(map3.containsValue("sport"));
		
		//删除元素
		map2.remove(1);
		System.out.println(map2);
		
		//清空
		map2.clear();
		System.out.println(map2);
		
		//map判空
		System.out.println(map2.isEmpty());
		
		//使用map定义
		Map<String, String> map4 = new HashMap<String, String>();
		map4.put("apple", "fruit1");
		map4.put("orance", "fruit2");
		
		//putIfAbsent只有在不存在key的时候生效。
		map4.putIfAbsent("banana", "fruit3");
		System.out.println(map4);
		map4.putIfAbsent("apple", "fruit4");
		System.out.println(map4);
		
		//返回所有的values
		System.out.println(map4.values());
		
		//forEach方法遍历
		map4.forEach((key, value) -> {
			System.out.println("key:" + key + " value:" + value);
		});
		
		//getOrDefault方法返回时设置默认值
		System.out.println(map4.getOrDefault("orange", "orange not exists."));	
	}
}

3. 上述JAVA代码 运行结果如下:

[{name=04007, sex=man, age=100}]
[{1=company, 2=Technology}]
name:04007
sex:man
age:100
1:company
2:Technology
{name=04007, sex=man, age=100}
{1=company, 2=Technology}
[{name=04007, sex=man, age=100}]
[{1=company, 2=Technology}]
3
2
[1, 2]
{1=first, 2=Technology}
{1=first, 2=Technology, 5=history, 9=sport}
true
true
{2=Technology}
{}
true
{banana=fruit3, apple=fruit1, orance=fruit2}
{banana=fruit3, apple=fruit1, orance=fruit2}
[fruit3, fruit1, fruit2]
key:banana value:fruit3
key:apple value:fruit1
key:orance value:fruit2
orange not exists.

4.Map和HashMap定义的区别

        JAVA中,Map是一个interface, HashMap是一个class 是Map的一个实现,两种定义的数据大多功能是通用的。new实现都是用的HashMap,所以在堆内存中的对象是一样的,只是引用类型不同,占用的空间是一样的,但调用的方法是hashmap的Map的功能方法HashMap都有,但HashMap有的Map不一定有,因为其无法调用HashMap中特有的方法,即HashMap未override的方法。为什么推荐用map不用hashmap?主要是面向接口编程的思想,定义为Map之后更灵活。

二、java中定义ArrayList及HashMap等数据结构时 HashMap<K,V> should be parameterized

    java连接mysql读取得到的数据集结构太不友好,更没有好的工具能方便地打印出来展示查看。因此我想写个通用的方法读取数据库数据后统一转化成一个ArrayList,而每行数据以一个HaspMap形式存储在ArrayList中,而我在使用方法将数据集按照逐行根据列名取得数据时却总遇到定义报错、警告。如下:

ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized
HashMap is a raw type. References to generic type HashMap<K,V> should be parameterized
Multiple markers at this line - Type safety: The expression of type ArrayList needs unchecked conversion to conform to ArrayList<HashMap<String,String>> - ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized
Multiple markers at this line - HashMap is a raw type. References to generic type HashMap<K,V> should be parameterized - Type mismatch: cannot convert from ArrayList<HashMap<String,String>> to ArrayList<HashMap>

        这些报错有时也能运行成功,有时又不能成功运行,真是烦人啊。折腾了解了一下,终于明白为什么报错了,豁然开朗舒服。ArrayList初始化的时候最好加上你存储的数据类型,而且这里要求的不仅仅是第一层的数据类型。要一直定义到最深层的数据,比如我的ArrayList里面存的是HashMap,而HashMap里存的格式是<String,String>,定义的时候就需要把这些全部写好。每个地方都 按照这样的方式严格填写的话,就不会有喝下了,如下所示:

ArrayList<HashMap<String,String>> arr = new ArrayList<HashMap<String,String>>();

        因为ArrayList存储时采用了java泛型这个概念,如果你想在ArrayList中存储String类型,那么初始化的时候应该写成ArrayList<String> data = new ArrayList<String>(); 一般建议采用接口和子类继承方式初始化,即List<String> temp = new ArrayList<String>();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林戈的IT生涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值