您的位置:首页 > 编程语言 > Java开发

Java-Map类

2017-11-21 17:24 211 查看
Map类
Map:双列集合,主要用来存储有键值对应关系的数据,是一个接口.

Map的几个子实现类:HashMap,Hashtable,LinkedHashMap,TreeMap

HashMap,Hashtable区别:HashMap非同步的,效率高,数据不安全,Hashtable同步的,效率低,数据安全.

Map集合的数据结构只跟键有关.

HashMap:底层数据结构哈希表,特点:元素无序且唯一,是靠重写equals和hashcode方法.

常用方法:

public V put(key,value):添加数据,第一次添加返回值为null,再添加返回值为上一次的值,键相同,值覆盖.

public int size():获取集合长度

public boolean isEmpty():判断集合是否为空.

public boolean containsKey(Key):判断集合是否包含指定键.

public boolean containsValue(Value):判断集合是否包含指定值.

public V get(key):根据键值获取指定值.

public Set<K> keySet():获取所有键的Set集合.

public Set<Map,Entry<K,V>> entrySet():获取所有键值对,对象集合Entry<Object,Object>有两个方法:getKey和getValue.

public Collection<V> values():获取所有值的集合.

public void clear():清除所有元素.

public V remove(Key):根据键值删除一个键值对.

相关代码:

import java.util.HashMap;

public class HashMapDemo {
public static void main(String[] args) {
HashMap<String,String> hm=new HashMap<String, String>();
//添加
hm.put("音无", "立华奏");
hm.put("公生", "小薰");
//获取长度
System.out.println("size:"+hm.size());
//判断集合是否为空
System.out.println("isEmpty:"+hm.isEmpty());
//判断集合有没有这个键
System.out.println("containsKey:"+hm.containsKey("立华奏"));
//判断集合有没有这个值
System.out.println("containsValue:"+hm.containsValue("公生"));
//根据键获取对应值
System.out.println("get:"+hm.get("音无"));
//获取所有键的Set集合
System.out.println("keySet:"+hm.keySet());
//获取所有值的集合
System.out.println("values:"+hm.values());
//获取所有键值对,对象为Entry<Object,Object>
System.out.println("entrySet:"+hm.entrySet());
//根据键移除一对元素
System.out.println("remove:"+hm.remove("公生"));
//清除所有元素
hm.clear();
System.out.println(hm.entrySet());
}
}
结果为:

size:2

isEmpty:false

containsKey:false

containsValue:false

get:立华奏

keySet:[公生, 音无]

values:[小薰, 立华奏]

entrySet:[公生=小薰, 音无=立华奏]

remove:小薰

[]

LinkedHashMap:底层数据结构是链表和哈希表,特点:元素唯一且有序,有序指定是存储和取出一致.

相关代码:

import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;

public class LinkedHashMapDemo {
public static void main(String[] args) {
LinkedHashMap<String, String> lhm=new LinkedHashMap<String, String>();
lhm.put("音无", "立华奏");
lhm.put("公生", "小薰");
//map集合的遍历
//方法1:获取键的Set集合
Set<String> keySet = lhm.keySet();
for(String key:keySet){
System.out.println(key+"---"+lhm.get(key));
}
System.out.println("---------------------");

//方法2:获取键值对的Set集合
Set<Entry<String, String>> entrySet = lhm.entrySet();
for(Entry<String, String> entry:entrySet){
System.out.println(entry.getKey()+"---"+entry.getValue());
}
}
}


结果为:

音无---立华奏

公生---小薰

---------------------

音无---立华奏

公生---小薰

TreeSet:底层是红黑树结构,特点:元素唯一,而且还对数据进行排序,自然排序和比较器排序,要求实现comparable<T>接口,重写compareTo方法.但TreeSet不能添加null键.

相关代码:

import java.util.Set;
import java.util.TreeMap;
public class TreeSetDemo {
public static void main(String[] args) {
TreeMap<String, String> tm=new TreeMap<String, String>();
tm.put("立华奏", "音无");
tm.put("小薰", "公生");
tm.put("1", null);
//String里重写了compareTo的方法
Set<String> keySet = tm.keySet();
for(String key:keySet){
System.out.println(key+"---"+tm.get(key));
}
}
}
结果为:
1---null

小薰---公生

立华奏---音无

import java.util.TreeMap;
public class TreeSetDemo {
public static void main(String[] args) {
TreeMap<Student, String> tm=new TreeMap<Student, String>();
tm.put(new Student("立华奏",20), "音无");
tm.put(new Student("小薰",20), "公生");
System.out.println(tm);
}
}
结果为:

{Student [name=小薰, age=20]=公生, Student [name=立华奏, age=20]=音无}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: