您的位置:首页 > 职场人生

黑马程序员----java基础之Map集合

2015-08-20 21:55 555 查看

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流!
-------

Map集合

Map集合是双列集合,是将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值。
Map和Collection的区别:
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set元素唯一
Map的数据结构是针对键的,和值无关。Collection的数据结构是针对元素有效地

Map的功能概述:
a、添加功能:
V put(K key , V Value)添加元素,将指定的值与次映射中的指定的键关联
其实此方法还有另外一个功能:替换,如果键是第一次储存,就直接储存元素,返回null,如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值。
b、删除功能:
void clear():移除所有的键值对元素(慎重使用)
V remove(Object key):根据键删除键值对元素,并把值返回
c、判断功能:

boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue (Objectvaule):判断集合是否包含指定的值
boolean isEmply():判断集合是否为空
d、获取功能:
Set<Map.entry<K,V>>entrySet():返回一个键值对的Set集合
V get(Object key):根据键获取值
Set<K> keySet():获取集合中所有键的集合
Collection<V>vaule():获取集合中的所有值的集合
e、长度功能:
intsize():返回集合中键值对的对数
遍历集合:
public class MapDemo {

public static void main(String[] args) {

Map<String , String> m = new HashMap<String , String>();

m.put("1", "10");

m.put("2", "20");

m.put("3", "30");

Set<Entry<String , String>> en = m.entrySet();

for(Entry<String , String> e : en){

String value = e.getValue();

String key = e.getKey();

System.out.println(key + "---" + value);

}

}

}

运行结果:
3---30

2---20

1---10

一、HashMap
HashMap底层的数据结构是哈希表,Map的数据结构只针对键有效和值没有关系
线程不安全,效率高,并且允许使用null键和null值
代码演示:
import java.util.HashMap;

import java.util.Map.Entry;

import java.util.Set;

public class HashMap01 {

public static void main(String[] args) {

HashMap<String , String> hm = new HashMap<String , String>();

hm.put("1", "10");

hm.put("2", "20");

hm.put("3", "30");

//第一种遍历方式

Set<String> keyset = hm.keySet();

for(String s : keyset){

System.out.println(s + "===" + hm.get(s));

}

System.out.println("--------------------------");

//第二种遍历方式

Set<Entry<String , String>> e = hm.entrySet();

for(Entry<String , String> en : e){

String key = en.getKey();

String value = en.getValue();

System.out.println(key + "===" + value);

}

}

}

运行结果:
3===30

2===20

1===10

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

3===30

2===20

1===10

解释:用了两种遍历方式,第一种的思路是通过keySet方法获取所有键的值,然后再遍历set集合再用get方法获取每一个键对应的值。第二种的思路是entrySet方法获取所有的键值对集合,然后遍历集合获取每一个键值对对象,然后用getKey和getValue方法获取键和值

二、LinkedHashMap
LinkedHashMap是实现map接口的子体系,底层结构是链表和哈希表,具有可预知的迭代顺序,元素有序,并且唯一
元素有序依赖于链表,唯一依赖于哈希表
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> lm = new LinkedHashMap<String , String>();

lm.put("1", "10");

lm.put("2", "20");

lm.put("3", "30");

Set<Entry<String , String>> en = lm.entrySet();

for(Entry<String , String> e : en){

String key = e.getKey();

String value = e.getValue();

System.out.println(key + "---" + value);

}

}

}

运行结果:
1---10

2---20

3---30

解释:通过entrySet方法得到键值对对象集合,然后遍历集合,通过getKey和getValue方法获得键和值,其他的因为LinkedHashMap是实现了Map接口的类,很多方法功能都相同,在此不一一列举。

三、TreeMap
TreeMap底层是二叉树数据结构:
可以对元素进行排序:
1、自然排序,但是排序的对象必须实现Coparable接口,重写compareTo方法
2、比较器排序
代码实现:自然排序
Student类:
public class Student implements Comparable<Student> {//实现Comparable

private String name;

private int age;

public Student() {

super();

}

public Student(String name, int age) {

super();

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override
//重写compareTo方法

public int compareTo(Student o) {

//对年龄进行比较

int num = this.age - o.age;

//对名字进行排序

int num2 = (num == 0)? this.name.compareTo(o.name) : num ;

return num2;

}

}

TreeMap类:
import java.util.Map.Entry;

import java.util.Set;

import java.util.TreeMap;

public class TreeMapDemo01 {

public static void main(String[] args) {

TreeMap<Student , String> tm = new TreeMap<Student , String>();

tm.put(new Student("小明",23), "第一名");

tm.put(new Student("小王",26), "第二名");

tm.put(new Student("小强",21), "第三名");

tm.put(new Student("小红",22), "第四名");

tm.put(new Student("小王",26), "第五名");

Set<Entry<Student , String>> en = tm.entrySet();

for(Entry<Student , String> e : en){

Student keys = e.getKey();

String values = e.getValue();

System.out.println(keys.getName()+"---"+keys.getAge()+"---"+values);

}

}

}

运行结果:
小强---21---第三名

小红---22---第四名

小明---23---第一名

小王---26---第五名

解释:保证了元素唯一并且排序了,首先在Student类重写了compareTo方法,如果不重写,编译会通过但运行时会报错。在这里我们先从年龄进行排序,然后是姓名,并且添加了键相同的元素,结果键值对是唯一的,键相同的但是值不同会被新的值覆盖。

代码实现:比较器排序
import java.util.Comparator;

import java.util.Map.Entry;

import java.util.Set;

import java.util.TreeMap

public class TreeMapDemo02 {

public static void main(String[] args) {

TreeMap<Student , String> tm = new TreeMap<Student , String>(new Comparator<Student>(){

@Override

public int compare(Student s1, Student s2) {

int num = s1.getAge() - s2.getAge();

int num2 = (num == 0)?s1.getName().compareTo(s2.getName()) : num ;

return num2;

}

});

tm.put(new Student("小明",29), "第一名");

tm.put(new Student("小王",28), "第二名");

tm.put(new Student("小强",28), "第三名");

tm.put(new Student("小红",26), "第四名");

tm.put(new Student("小王",26), "第五名");

Set<Entry<Student , String>> en = tm.entrySet();

for(Entry<Student , String> e : en){

Student s = e.getKey();

System.out.println(s.getName() + "---" + s.getAge() + "---" + e.getValue());

}

}

}

运行结果:
小王---26---第五名

小红---26---第四名

小强---28---第三名

小王---28---第二名

小明---29---第一名

解释:首先声明,这个测试类的Student没有实现Comparable接口重写compareTo方法,这是在创建TerrMap对象时的在构造方法中加入了比较器,用匿名内部类的方式,也是按照先比较年龄后比较名字。
注意:结果中虽然有两个小王,但是年龄不同,是两个不同的对象,所以键不一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: