map和hashmap

map

Map集合的特点

  • 1.能够存储唯一的列的数据(唯一,不可重复) Set
  • 2.能够存储可以重复的数据(可重复) Collection
  • 3.值的顺序取决于键的顺序
  • 4.键和值都是可以存储null元素的
  • 5.键和值是多对一的关系

常用功能

  • 1.添加功能
    V put(K key, V value)
    当key相同时,第一次添加元素,返回结果为null,第二次添加返回上一次的键对应的值,返回 “前任”;
    void putAll(Map<? extends K,? extends V> m)
		Map<String,String> map=new HashMap();
		System.out.println(map.put("zx", "12"));
		System.out.println(map.put("zx", "123"));
		System.out.println(map.put("zx", "124"));
		//设置值
		//当key相同时,第一次添加元素,返回结果为null,第二次添加返回上一次的键对应的值,返回 "前任";
		//打印结果:null  	12   123

		Map<String,String> map2=new HashMap();
		map2.put("33", "ewr");
		map2.put("34", "sdfb");
		map.putAll(map2);//添加map集合
		System.out.println(map);
  • 2.删除功能
    V remove(Object key)
    void clear()
		System.out.println(map.remove("34"));//sdfb
		//删除后将返回values:sdfb
		System.out.println(map);
		map.clear();
  • 3.遍历功能
    Set keySet()
    Collection values()
    Set<Map.Entry<K,V>> entrySet()
在这里插入代码片
  • 4.获取功能
    V get(Object key)
System.out.println(map.get("34"));//sdfb
  • 5.判断功能
    boolean containsKey(Object key)
    boolean containsValue(Object value)
    boolean isEmpty()
		System.out.println(map.containsKey("34"));//true
		System.out.println(map.containsValue("124"));//true	
  • 6.修改功能
    V put(K key, V value)
    void putAll(Map<? extends K,? extends V> m)
		System.out.println(map.put("zx", "123"));//返回上一次的values
		System.out.println(map.put("zx", "124"));//返回123
		
  • 7.长度功能
  • int size()
 System.out.println(map.size());

注意

  • 1.键相同表示修改,键不同表示添加
  • 2.第一次添加元素,返回结果为null,第二次添加返回上一次的键对应的值,返回 “前任”;
  • 3.键是唯一的,无序的,类似于Set接口,值是可重复的,顺序取决于键的顺序,类似于Collection
  • 4.并且键和值存在映射关系,建立了集合之间的联系

WeakHashMap

表示以弱键形式存在的哈希表

弱键: 垃圾对象作为键
数据结构是哈希表

如果键是弱键形式存在,会被垃圾回收器回收该键,导致该值也被移除

public static void main(String[] args) {
	WeakHashMap<String, String> whm = new WeakHashMap<>();
	whm.put(new String("abc"), "123");
	whm.put(new String("efg"), "456");
	whm.put(new String("hij"), "789");
	whm.put("opq", "789");
	
	System.gc();//启动垃圾回收器
	System.runFinalization();
	
	System.out.println(whm);//key作为匿名对象,全被回收,只留下一个opg-》789
}

HashMap类

特点:

  • 1.底层数据结构是 哈希表
  • 2.元素无序 , 唯一
  • 3.数据结构仅针对键有效,和值无关
  • 4.保证元素唯一依赖的hashCode和equals方法
  • 5.如果键是系统类,一般都有重写 hashCode和equals方法,如果键是自定义类,需要自己重写
  • entry<key类型,value类型>是map的内部接口:
HashMap<Bos,String> map2 = new HashMap<Bos,String>();
		map2.put(new Bos("张胜男",15), "张胜男");
		map2.put(new Bos("张胜男2",18), "张胜男2");
		map2.put(new Bos("张胜男",15), "张胜男");
		map2.put(new Bos("张胜男3",19), "张胜男3");
		//方法一:
		Set<Entry<Bos, String>> entrySet = map2.entrySet();
		Iterator<Entry<Bos, String>> iterator = entrySet.iterator();
		//简写如下
		Iterator<Entry<Bos,String>> i=map2.entrySet().iterator();
		while(i.hasNext()){
			Entry<Bos,String> b=i.next();
			System.out.println(b.getKey()+" "+b.getValue());
		}
		System.out.println("---------------------------------");
		//方法2:	
		for(Entry<Bos,String> en:map2.entrySet()){
			System.out.println(en.getValue()+" "+en.getKey().age);
		}
  • Hashtable 类是旧版的 HashMap

  • ArrayList 和 Vector

1) Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的同步必然要影响性能,因此,ArrayList的性能比Vector好。
2) 当Vector或ArrayList中的元素超过它的初始大小时,Vector会将它的容量翻倍,而ArrayList只增加50%的大小,这样,ArrayList就有利于节约内存空间

  • Map 和 Dictionary

  • 面试题: Hashtable 类 旧版的 HashMap

  • HashMap和Hashtable底层数据结构都是哈希表

  • HashMap线程不安全,效率高,可以存储 null 值 null键

  • Hashtable是旧版的HashMap,线程安全,效率低 , 不可以存储 null 值 null键

HashMap<String, String> hashmap = new HashMap<String,String>();
		hashmap.put("zd", "qwe8");
		hashmap.put("zd1", "qwe7");
		hashmap.put("zd1", "qwe4");
		hashmap.put("zd2", "qwe5");	
		hashmap.put("jk", "qwe1");		
		hashmap.put("zd", "qwe2");
		Set<String> c=hashmap.keySet();
		for (String string : c) {
			System.out.println(string+" "+hashmap.get(string));
		}

打印结果:	
		jk qwe1
		zd1 qwe4
		zd2 qwe5
		zd qwe2
 体现了无序性和唯一性-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值