您的位置:首页 > 其它

Map集合的子集及用法

2015-02-07 14:33 211 查看
package javaStudy;
/**
*
* @author zhaocx1
*
*/
/*
* Map 集合重点:该集合存储键值对,一对一对往里存,而且要保证键的唯一性。
*     1.添加。
*        put(K key,V value)
*        putAll(Map<? extends K,? extends V>)
*     2.删除。
*        clear()
*        remove(Object key)
*     3.判断。
*        containsValue(Object value)
*        containsKey(Object key)
*        isEmpty()
*     4.获取。
*        value get(Object key)
*        size()
*        values()
*
*        entrySet()
*        keySet()
*
* Map
*  --HashTable:底层是哈希表数据结构,不可以存入null键和null值。该集合是线程同步的。JDK1.0,效率低。
*  --HashMap:底层是哈希表数据结构,允许使用null值和null键,该集合是线程不同步的。   JDK1.2,效率高。
*  --TreeMap:底层是二叉树数据结构,线程不同步。可以用于给map集合中的键进行排序。
*
* 和Set很像。
* 其实Set底层就是使用了Map集合。
*
*/

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class MapDemo1 {
public static void main(String[] args) {
//定义一个Map集合,并且指定泛型,Map集合中的元素是成对出现的
Map<String, String> map = new HashMap<String, String>();

// 添加元素,如果出现添加相同键时,那么后添加键的值会覆盖原有键对应的值。并put方法会返回被覆盖的值。
map.put("02", "zhangsan2");
map.put("03", "zhangsan3");

//判断集合中是否包含某一个键
System.out.println("containsKey:" + map.containsKey("023"));
//通过某一个键删除相对应的值
System.out.println("remove:" + map.remove("02"));
//通过某一个键获取某一个值
System.out.println("get:"+map.get("02"));

//map.put("04", null);
//System.out.println("get:"+map.get("04"));
//可以通过get方法的返回值来判断一个键是否存在。通过返回null来判断。

//Map集合中可以通过values获取所有的值
Collection<String> coll=map.values();

//输出打印所有的值
System.out.println(coll);
//输出打印集合中的所有键值
System.out.println(map);
}

}
<pre name="code" class="java">package javaStudy;
/**
*
* @author zhaocx1
* Map集合中的第一种取出元素的方法:
*/
import java.util.HashMap;//导入集合中的包类
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
* map集合的两种取出方式。
* 1.Set<K> keySet:将map中所有的键存入到set集合。因为set具备迭代器所有可以迭代的方式取出所有的键,再根据get方法获取每一个键对应的值。
*
*        Map集合的取出原理:将map集合转成set集合,再通过迭代器取出。
*
* 2.Set<Map.Entry<K.V>>  entrySet:将map集合中的映射关系存入到了set集合中,而这个关系的数据类型就是:Map.Entry.
*
*
*/
public class MapDemo2 {
public static void main(String[] args) {
//定义一个集合容器用于存储集合,并指定泛型为字符串类型
Map<String, String> map = new HashMap<String, String>();

//添加元素,必须是成对添加
map.put("02", "zhangsan2");
map.put("03", "zhangsan3");
map.put("01", "zhangsan1");
map.put("04", "zhangsan4");

//定义一个Set集合用于存储所有的键,通过keySet()方法进行获取
Set<String> keySet = map.keySet();

// 有了Set集合,就可以获取其迭代器。
Iterator<String> it = keySet.iterator();

while (it.hasNext()) {
String key = it.next();
// 有了键就可以通过map集合的get方法获取其对应的值。
// System.out.println("key:"+key);
String value = map.get(key);
//输出打印所有的键和相对应的值
System.out.println("key:" + key + ",value:" + value);
}
}

}
<pre name="code" class="java">package javaStudy;

/**
*
* @author zhaocx1
* Map集合中的第二种取出元素的方法:
*/
import java.util.HashMap;//导入集合中的类包
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
* map集合的两种取出方式。
*
* 2.Set<Map.Entry<K.V>>  entrySet:将map集合中的映射关系存入到了set集合中,而这个关系的数据类型就是:Map.Entry.
*
* 将Map中的映射关系取出。
* 这个关系就是Map.Entry类型。那么关系对象Map.Entry获取后,就可以通过Map.Entry中getKey和getValue方法后去关系中的键和值。
*
*/
public class MapDemo3 {
public static void main(String[] args) {
// 定义一个集合,用于存储集合中的元素,并指定集合中的元素类型为字符串类型
Map<String, String> map = new HashMap<String, String>();
// 添加元素
map.put("02", "zhangsan2");
map.put("03", "zhangsan3");
map.put("01", "zhangsan1");
map.put("04", "zhangsan4");

// 定义一个Set集合,将集合中的映射关系取出,存入到Set集合中。
Set<Map.Entry<String, String>> entrySet = map.entrySet();

// 通过迭代器获取Set集合中的映射关系
Iterator<Map.Entry<String, String>> it1 = entrySet.iterator();

while (it1.hasNext()) {
Map.Entry<String, String> me = it1.next();

// 通过Map.Entry中getKey和getValue方法后去关系中的键和值
String key = me.getKey();
String value = me.getValue();

// 输出打印集合中的所有键和值
System.out.println(key + "::" + value);

}

}
}

/*
* Map.Entry其实Entry也是一个接口,踏实Map接口中的一个内部接口。
*/

interface Map1

{
public static interface Entry {
public abstract Object getKey();

public abstract Object getValue();

}
}

class HashMap1 implements Map1 {
class Haha implements Map1.Entry {
public Object getKey() {
return null;
}

public Object getValue() {
return null;
}
}

}
<pre name="code" class="java">package javaStudy;
import java.util.HashMap;//导入集合中的类包
import java.util.Iterator;
/**
*
* @author zhaocx1
*
*/
/*
* map扩展知识:
* map集合被使用是因为具备映射关系。
*/
public class MapDemo4 {
public static void main(String[] args) {

//定义集合用于存储集合中的元素,并指定集合中元素的类型
HashMap<String, String> yure = new HashMap<String, String>();
HashMap<String, String> jiuye = new HashMap<String, String>();

//定义集合用于存储集合中的元素,并指定集合中元素的类型,该集合中值是一个Map集合
HashMap<String, HashMap<String, String>> czbk = new HashMap<String, HashMap<String, String>>();

//添加元素,Map集合中添加元素用的是put方法
yure.put("01", "zhangsan");
yure.put("02", "lisi");

jiuye.put("01", "wangwu");
jiuye.put("02", "zhaoliu");

czbk.put("yureban", yure);
czbk.put("jiuyeban", jiuye);

// 遍历czbk集合,获取所有的键。
Iterator<String> it = czbk.keySet().iterator();
while (it.hasNext()) {
//获取集合中所有的键
String roomName = it.next();
//通过键获取集合中相对应的值
HashMap<String, String> room = czbk.get(roomName);
//输出打印集合中所有的键
System.out.println(roomName);
//调用静态方法,获取集合中所有的键和值
getStudentInfo(room);
}

getStudentInfo(yure);
}
//定义一个静态方法用于获取集合中所有的值
public static void getStudentInfo(HashMap<String, String> roomMap) {
//通过迭代器获取集合中所有的键
Iterator<String> it = roomMap.keySet().iterator();
while (it.hasNext()) {
String id = it.next();
//通过键获取集合中相对应的值
String name = roomMap.get(id);
//输出打印集合中所有的键和值
System.out.println(id + "::" + name);
}

}
}
package javaStudy;
/**
*
* @author zhaocx1
* Map集合练习
*/
/*
* 每一个学生都有对应的归属地。
* 学生Student,地址String.
* 学生属性,年龄。
* 注意:姓名和年龄相同视为同一个学生。
* 保证学生的唯一性。
*
* 1.描述学生。
* 2.定义map容器,将学生作为键,地址作为值,存  入。
* 3.获取map集合中的元素。
*/
import java.util.HashMap;//导入集合中的类包
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
//定义一个类实现Comparable接口让类中的对象具备比较性
class Student implements Comparable<Student> {
private String name;//私有name属性
private int age;//私有age属性

Student(String name, int age) {// 对类进行构造函数,并将属性传递作为参数
this.name = name;// 调用本类属性
this.age = age;
}

//覆写Comparable接口中的compareTo方法,并将学生对象作为参数传递进去
public int compareTo(Student s) {
//先对age属性进行比较
int num = new Integer(this.age).compareTo(new Integer(s.age));
//如果age相等再判断name属性
if (num == 0)
return this.name.compareTo(s.name);
return num;
}

public int hasCode() {
return name.hashCode() + age * 34;
}
// 定义一个方法判断Object类是否包含Person类,如果包含向下转型,从而Object类可以接收Person类
public boolean equals(Object obj) {
if (!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
// 向下转型
Student s = (Student) obj;
// 返回本类中的name和age属性
return this.name.equals(s.name) && this.age == age;

}

public String getName() {// 定义一个获取name属性的方法
return name;// 返回值为name
}

public int getAge() {
return age;
}

public String toString() {//定义一个返回字符串类型的方法
return name + "::" + age;//返回本类中的姓名和年龄
}

}

public class MapTestDemo1 {
public static void main(String[] args) {
//定义集合容器,用于存储集合中的元素
HashMap<Student, String> hm = new HashMap<Student, String>();
//集合中添加元素
hm.put(new Student("lisi", 21), "beijing");
hm.put(new Student("lisi", 22), "tianjin");
hm.put(new Student("lisi", 23), "shanghai");
hm.put(new Student("lisi", 24), "wuhan");

// 第一种取出方式keySet
Set<Student> keySet = hm.keySet();
Iterator<Student> it = keySet.iterator();
while (it.hasNext()) {
Student stu = it.next();
String addr = hm.get(stu);
System.out.println(stu + ".." + addr);
}
// 第二种取出方式 entrySet
Set<Map.Entry<Student, String>> entrySet = hm.entrySet();
Iterator<Map.Entry<Student, String>> iter = entrySet.iterator();

while (iter.hasNext()) {
Map.Entry<Student, String> me = iter.next();

Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu + "......" + addr);

}

}

}
package javaStudy;
/**
*
* @author zhaocx1
* Map集合练习
*/
/*
*需求:对学生对象的年龄进行升序排序。
*因为数据是以键值对形式存在的。
*所以要使用可以排序的Map集合,TreeMap.
*/
import java.util.Comparator;//导入集合中的类
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

//定义一个比较器实现Comparator借口并指定泛型
class StuNameComparator implements Comparator<Student> {
//覆写接口中的compare方法
public int compare(Student s1, Student s2) {
//对name属性进行比较
int num = s1.getName().compareTo(s2.getName());
//如果name相同,通过age属性进行比较
if (num ==  0) {
Integer a = new Integer(s1.getAge()).compareTo(new Integer(s2
.getAge()));
return new Integer(a);
}
return num;
}
}

public class MapTestDemo2 {
public static void main(String[] args) {
//定义集合容器用于存储集合中的元素,并将比较器作为参数传递到集合的构造函数中
TreeMap<Student, String> tm = new TreeMap<Student, String>(
new StuNameComparator());
//添加元素
tm.put(new Student("lisi", 21), "beijing");
tm.put(new Student("lisi", 23), "shanghai");
tm.put(new Student("lisi", 22), "tianjin");
tm.put(new Student("lisi", 24), "wuhan");

//用Map集合中的Map.Entry方法通过迭代器获取集合中的元素
Set<Map.Entry<Student, String>> entrySet = tm.entrySet();
Iterator<Map.Entry<Student, String>> it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry<Student, String> me = it.next();
//获取集合中的键
Student stu = me.getKey();
//获取集合中的值
String addr = me.getValue();
System.out.println(stu + ":::" + addr);
}

}

}


package javaStudy;

/**
*
* @author zhaocx1
* Map集合练习
*/
/*
* 练习:
* "asdbhcgnaahcgaahddfj"获取字符串中的字母出现的次数。
* 希望打印结果:a(3)c(2)..
*
* 通过结果发现,每一个字母都有对应的次数。
* 说明字母和次数之间都有映射关系。
*
* 注意了,当发现有映射关系时,可以选择map集合。
* 因为map集合中存放就是映射关系。
*
* 什么时候使用map集合呢?
* 当数据之间存在映射关系时,就要先想map集合。
*
* 思路:
* 1.将字符串转换成字符数组。因为要对每一个字母进行操作。
* 2.定义一个map集合,因为打印结果的字母有顺序,所以使用treeMap.
* 3.遍历字符数组。
*    将每一个字母作为键区查map集合。
*    如果返回null,将该字母和1存入到map集合中。
*    如果返回值不是null,说明该字母在map集合中已经存在并有对应的次数。
*    那么就获取该次数并进行自增,然后将该字母和自增后的次数存入到map集合中,覆盖调用原来键所对应的值。
* 4.将map集合中的数据变成指定的字符串形式返回。
*
*/
import java.util.Iterator;//导入集合中的类包
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MapTestDemo3 {

public static void main(String[] args) {
String s = charCount("asdbhcgnaahcgaahddfj");//声明一个
System.out.println(s);
}

public static String charCount(String str) {
char[] chs = str.toCharArray();//将字符串转成字符数组

//定义一个集合容器,用于存储集合中的元素,因为要求打印的字母有顺序,所以使用TreeMap
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
//对数组中的元素进行遍历
for (int x = 0; x < chs.length; x++) {
//定义一个计数器,用于记录每次遍历元素的个数
int count = 0;
//对获取字母的转化成Integer类型
Integer value = tm.get(chs[x]);

if (value != null)
count = value;
count++;
tm.put(chs[x], count);

}
//定义一个StringBuilder容器
StringBuilder sb = new StringBuilder();
//通过Map集合的Map.Entry方法获取集合中的元素
Set<Map.Entry<Character, Integer>> entrySet = tm.entrySet();
Iterator<Map.Entry<Character, Integer>> it = entrySet.iterator();

while (it.hasNext()) {
Map.Entry<Character, Integer> me = it.next();
//获取集合中的键
Character ch = me.getKey();
//获取集合中的值
Integer value = me.getValue();
//将集合中的元素存放到StringBuilder容器中
sb.append(ch + "(" + value + ")");
}
//返回字符串
return sb.toString();
}

}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: