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

java中关于Map的三种遍历方法机putAll的用法详解

2016-01-20 20:22 1891 查看
map的三种遍历方法!

集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~

复制代码 代码如下:

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package cn.tsp2c.liubao;

import java.util.Collection;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

import java.util.TreeMap;

/**

*

* @author Administrator

*/

public class TestMap {

public static void main(String[] args) {

Map<String, Student> map = new HashMap<String, Student>();

Student s1 = new Student("宋江", "1001", 38);

Student s2 = new Student("卢俊义", "1002", 35);

Student s3 = new Student("吴用", "1003", 34);

map.put("1001", s1);

map.put("1002", s2);

map.put("1003", s3);

Map<String, Student> subMap = new HashMap<String, Student>();

subMap.put("1008", new Student("tom", "1008", 12));

subMap.put("1009", new Student("jerry", "1009", 10));

map.putAll(subMap);

work(map);

workByKeySet(map);

workByEntry(map);

}

//最常规的一种遍历方法,最常规就是最常用的,虽然不复杂,但很重要,这是我们最熟悉的,就不多说了!!

public static void work(Map<String, Student> map) {

Collection<Student> c = map.values();

Iterator it = c.iterator();

for (; it.hasNext();) {

System.out.println(it.next());

}

}

//利用keyset进行遍历,它的优点在于可以根据你所想要的key值得到你想要的 values,更具灵活性!!

public static void workByKeySet(Map<String, Student> map) {

Set<String> key = map.keySet();

for (Iterator it = key.iterator(); it.hasNext();) {

String s = (String) it.next();

System.out.println(map.get(s));

}

}

  //比较复杂的一种遍历在这里,呵呵~~他很暴力哦,它的灵活性太强了,想得到什么就能得到什么~~

public static void workByEntry(Map<String, Student> map) {

Set<Map.Entry<String, Student>> set = map.entrySet();

for (Iterator<Map.Entry<String, Student>> it = set.iterator(); it.hasNext();) {

Map.Entry<String, Student> entry = (Map.Entry<String, Student>) it.next();

System.out.println(entry.getKey() + "--->" + entry.getValue());

}

}

}

Set<Map.Entry<String,ArrayList>> set = map.entrySet();
for (Iterator<Map.Entry<String,ArrayList>> it = set.iterator(); it.hasNext();) {
Map.Entry entry =  it.next();
List<String> list=(List<String>)entry.getValue();
for(String str:list){
System.out.println(str);
}
System.out.println(entry.getKey() + "--->" + entry.getValue());
}

s

class Student {

private String name;

private String id;

private int age;

public Student(String name, String id, int age) {

this.name = name;

this.id = id;

this.age = age;

}

@Override

public String toString() {

return "Student{" + "name=" + name + "id=" + id + "age=" + age + '}';

}

}

Map.putAll()用法

该方法用来追加另一个Map对象到当前Map集合对象,它会把另一个Map集合对象中的所有内容添加到当前Map集合对象。

语法 putAll(Map<? extends K,? extends V> m)

m:一个Map集合对象。

import java.util.HashMap;

public class Map_putAllTest {

public static void main(String[] args){

//两个map具有不同的key

HashMap map1=new HashMap();

map1.put("1", "A");

HashMap map2 = new HashMap();

map2.put("2", "B");

map2.put("3", "C");

map1.putAll(map2);

System.out.println(map1);

//两个map具有重复的key

HashMap map3=new HashMap();

map3.put("1", "A");

HashMap map4 = new HashMap();

map4.put("1", "B");

map4.put("3", "C");

map3.putAll(map4);

System.out.println(map3);

}

}

/* 输出结果:

* {3=C, 2=B, 1=A}

* {3=C, 1=B}

* 结论:putAll可以合并两个MAP,只不过如果有相同的key那么用后面的覆盖前面的

* */

典型应用 本示例创建一个Map集合对象,为它添加一些内容并输出该集合的大小,然后创建第二个Map集合对象,也添加一些内容,输出集合大小,最后把第二个Map集合添加到第一个Map集合对象,再次输出第一个集合的大小。运行结果如图1.28所示。



本示例的关键代码如下:

public static void main(String[] args) {

Map map1 = new HashMap(); //定义Map集合对象

map1.put("apple", "新鲜的苹果"); //向集合中添加对象

map1.put("computer", "配置优良的计算机");

map1.put("book", "堆积成山的图书");

System.out.println("第一个Map集合大小为:"+map1.size()) //输出集合长度

Map map2 = new HashMap(); //定义Map集合map2

map2.put("apple2", "新鲜的苹果"); //向集合中添加对象

map2.put("computer2", "配置优良的计算机");

map2.put("book", "堆积成山的图书");

System.out.println("第二个Map集合大小为:"+map2.size()); //输出集合长度

System.out.println("把第二个Map集合添加到第一个Map集合中");

map1.putAll(map2); //将map2中的对象添加到map1中

System.out.println("整合后的第一个Map集合大小为:"+map1.size());

}

整合后的Map集合大小是5而不是6,那是因为两个Map集合中有一个重复的键名“book”,Map集合的键名是不能重复的,所以新的“book”键值取代了旧的“book”键值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: