黑马程序员_集合3-Map集合派系
2013-09-24 22:08
357 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
|---Map集合--映射
一个键只能映射一个值,保证键的唯一性
常用方法
put(Key,Value),返回值为null,存储方法,重复存储返回次数
isEmpty()
boolean containsKey(键)判断集合中有没有这个键
boolean containValue(值)判断集合中有没有这个值
get(键),通过键,获取值,获取不到值,说明没有这个键,返回null
putAll(Collection)
|-- Map集合的取出方法:
1.Map集合中有一个方法,keySet(),将Map中的键,存储到Set集合中
迭代Set集合,就可以获取到键
**取出的值是有序的,TreeMap是正向排列的,HashMap是倒序排列的
根据键,获取值,get()是Map集合中的方法
2.通过Map集合中键值的映射关系来获取
Set<Map.Entry<K,V>> entrySet(),这个方法返回的是键值映射关系对
entrySet()方法返回的是键值关系,表示这个键值关系的对象是Map.Entry
然后把这个键值关系存储到Set集合中
迭代Set集合
迭代器next()返回的是Map.Entry类型
调用Map.Entry接口中的方法,getKey() getValue()
|--Map子类的特点
|--HashMap
|--底层结构是哈希表
线程是不安全的
不允许存储重复键
允许存储null值和null键
|--TreeMap
底层数据结构是红黑树
线程不安全
不允许存储重复键
作为键的存储对象,对象必须具备比较性,对象实现Comparable接口
或者让TreeMap自身具备比较性
|--Hashtable
底层数据结构也是Hash表
线程安全
不允许存储重复键
不允许null值和null键
郁郁而终
|--Hashtable的子类,
====================================================================================================================================
* 利用Set集合取出Map集合中存储的对象
*/
import java.util.*;
import cn.itcast.generic.Person;
public class HashMapDemo1 {
public static void main(String[] args) {
method_2();
}
//Map集合的键值关系,取出集合中的对象
public static void method_2(){
HashMap<Person, String> hm = new HashMap<Person, String>();
hm.put(new Person("lisi1",21), "北京市");
hm.put(new Person("lisi2",22), "天津市");
hm.put(new Person("lisi2",22), "天津市");
hm.put(new Person("lisi3",23), "叙利亚");
hm.put(new Person("lisi3",23), "叙利亚");
hm.put(new Person("lisi4",24), "美国");
//通过entrySet()方法,获取到键值关系,并存储到Set集合
Set<Map.Entry<Person,String>> set = hm.entrySet();
//迭代Set集合
Iterator<Map.Entry<Person,String>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Person, String> me=
it.next();
Person p = me.getKey();
String s = me.getValue();
System.out.println(p+"...."+s);
}
}
//Map集合存储自定义对象并取出
public static void method_1(){
HashMap<Person, String> hm = new HashMap<Person, String>();
hm.put(new Person("lisi1",21), "北京市");
hm.put(new Person("lisi2",22), "天津市");
hm.put(new Person("lisi2",22), "天津市");
hm.put(new Person("lisi3",23), "叙利亚");
hm.put(new Person("lisi3",23), "叙利亚");
hm.put(new Person("lisi4",24), "美国");
//通过Map集合中的keySet方法,将键存储到Set集合
Set<Person> set = hm.keySet();
//迭代Set集合
Iterator<Person> it = set.iterator();
while(it.hasNext()){
Person p = it.next();
String address = hm.get(p);
System.out.println(p+"...."+address);
}
}
//Map集合存储对象
public static void method(){
HashMap<String, Integer> hm = new HashMap<String, Integer>();
hm.put("a", 1);
hm.put("b", 2);
hm.put("c", 3);
//使用Map集合中的keySet()方法,将键存储到Set集合
Set<String> set = hm.keySet();
//迭代Set集合
Iterator<String> it = set.iterator();
while(it.hasNext()){
//使用Map集合中的方法,get获取值
String key = it.next();
Integer value = hm.get(key);
System.out.println(key+"......"+value);
}
}
}
====================================================================================================================================
* 实现统计字母出现次数
*/
import java.util.*;
public class TreeMapTest {
public static void main(String[] args) {
getCount("inoYloveyoumy");
}
public static void getCount(String str){
//步骤对字符串进行判空
if(str == null || "".equals(str))
throw new RuntimeException("无效的字符串");
//字符串转成字符数据
char[] ch = str.toCharArray();
//建立Map集合
TreeMap<Character,Integer> tm = new TreeMap<Character, Integer>();
//遍历数组
for (int x = 0; x < ch.length; x++) {
//将数组中的每个字符获取出来当做键,到Map中查找值
Integer i =
tm.get(ch[x]);
//对i进行判断
if(i==null){//集合中没有 存储过这个键,存储这个键,值是1
tm.put(ch[x], 1);
}else{//集合中有这个键,将获取到的值++,存回集合
//i++;
tm.put
(ch[x], ++i);
}
}
//数组遍历结束,取出集合中存储的对象
//System.out.println(tm);
Set<Map.Entry<Character, Integer>> set = tm.entrySet();
Iterator<Map.Entry<Character, Integer>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Character, Integer> me = it.next();
Character c = me.getKey();
Integer i = me.getValue();
System.out.print(c+"字母出现了"+i+"次 ");
}
}
}
|---Map集合--映射
一个键只能映射一个值,保证键的唯一性
常用方法
put(Key,Value),返回值为null,存储方法,重复存储返回次数
isEmpty()
boolean containsKey(键)判断集合中有没有这个键
boolean containValue(值)判断集合中有没有这个值
get(键),通过键,获取值,获取不到值,说明没有这个键,返回null
putAll(Collection)
|-- Map集合的取出方法:
1.Map集合中有一个方法,keySet(),将Map中的键,存储到Set集合中
迭代Set集合,就可以获取到键
**取出的值是有序的,TreeMap是正向排列的,HashMap是倒序排列的
根据键,获取值,get()是Map集合中的方法
2.通过Map集合中键值的映射关系来获取
Set<Map.Entry<K,V>> entrySet(),这个方法返回的是键值映射关系对
entrySet()方法返回的是键值关系,表示这个键值关系的对象是Map.Entry
然后把这个键值关系存储到Set集合中
迭代Set集合
迭代器next()返回的是Map.Entry类型
调用Map.Entry接口中的方法,getKey() getValue()
|--Map子类的特点
|--HashMap
|--底层结构是哈希表
线程是不安全的
不允许存储重复键
允许存储null值和null键
|--TreeMap
底层数据结构是红黑树
线程不安全
不允许存储重复键
作为键的存储对象,对象必须具备比较性,对象实现Comparable接口
或者让TreeMap自身具备比较性
|--Hashtable
底层数据结构也是Hash表
线程安全
不允许存储重复键
不允许null值和null键
郁郁而终
|--Hashtable的子类,
====================================================================================================================================
* 利用Set集合取出Map集合中存储的对象
*/
import java.util.*;
import cn.itcast.generic.Person;
public class HashMapDemo1 {
public static void main(String[] args) {
method_2();
}
//Map集合的键值关系,取出集合中的对象
public static void method_2(){
HashMap<Person, String> hm = new HashMap<Person, String>();
hm.put(new Person("lisi1",21), "北京市");
hm.put(new Person("lisi2",22), "天津市");
hm.put(new Person("lisi2",22), "天津市");
hm.put(new Person("lisi3",23), "叙利亚");
hm.put(new Person("lisi3",23), "叙利亚");
hm.put(new Person("lisi4",24), "美国");
//通过entrySet()方法,获取到键值关系,并存储到Set集合
Set<Map.Entry<Person,String>> set = hm.entrySet();
//迭代Set集合
Iterator<Map.Entry<Person,String>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Person, String> me=
it.next();
Person p = me.getKey();
String s = me.getValue();
System.out.println(p+"...."+s);
}
}
//Map集合存储自定义对象并取出
public static void method_1(){
HashMap<Person, String> hm = new HashMap<Person, String>();
hm.put(new Person("lisi1",21), "北京市");
hm.put(new Person("lisi2",22), "天津市");
hm.put(new Person("lisi2",22), "天津市");
hm.put(new Person("lisi3",23), "叙利亚");
hm.put(new Person("lisi3",23), "叙利亚");
hm.put(new Person("lisi4",24), "美国");
//通过Map集合中的keySet方法,将键存储到Set集合
Set<Person> set = hm.keySet();
//迭代Set集合
Iterator<Person> it = set.iterator();
while(it.hasNext()){
Person p = it.next();
String address = hm.get(p);
System.out.println(p+"...."+address);
}
}
//Map集合存储对象
public static void method(){
HashMap<String, Integer> hm = new HashMap<String, Integer>();
hm.put("a", 1);
hm.put("b", 2);
hm.put("c", 3);
//使用Map集合中的keySet()方法,将键存储到Set集合
Set<String> set = hm.keySet();
//迭代Set集合
Iterator<String> it = set.iterator();
while(it.hasNext()){
//使用Map集合中的方法,get获取值
String key = it.next();
Integer value = hm.get(key);
System.out.println(key+"......"+value);
}
}
}
====================================================================================================================================
* 实现统计字母出现次数
*/
import java.util.*;
public class TreeMapTest {
public static void main(String[] args) {
getCount("inoYloveyoumy");
}
public static void getCount(String str){
//步骤对字符串进行判空
if(str == null || "".equals(str))
throw new RuntimeException("无效的字符串");
//字符串转成字符数据
char[] ch = str.toCharArray();
//建立Map集合
TreeMap<Character,Integer> tm = new TreeMap<Character, Integer>();
//遍历数组
for (int x = 0; x < ch.length; x++) {
//将数组中的每个字符获取出来当做键,到Map中查找值
Integer i =
tm.get(ch[x]);
//对i进行判断
if(i==null){//集合中没有 存储过这个键,存储这个键,值是1
tm.put(ch[x], 1);
}else{//集合中有这个键,将获取到的值++,存回集合
//i++;
tm.put
(ch[x], ++i);
}
}
//数组遍历结束,取出集合中存储的对象
//System.out.println(tm);
Set<Map.Entry<Character, Integer>> set = tm.entrySet();
Iterator<Map.Entry<Character, Integer>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Character, Integer> me = it.next();
Character c = me.getKey();
Integer i = me.getValue();
System.out.print(c+"字母出现了"+i+"次 ");
}
}
}
相关文章推荐
- 黑马程序员_集合2-set集合派系
- 黑马程序员_集合1-listj集合派系
- 黑马程序员_常用API
- 黑马程序员_字符串
- 黑马程序员_多线程2
- 黑马程序员_多线程1
- 海外实习面试经历
- 黑马程序员_枚举
- 【黑马程序员】Socket编程实现内网连接的问题
- 黑马程序员——if、switch、while、for
- 黑马程序员------java5的枚举(枚举的原理、枚举的应用)
- 头文件string与string.h的区别
- 怎样准备软件工程师的面试
- 为程序员准备的7个社交网站
- DLL入门浅析(1)——如何建立DLL
- 黑马程序员------java5的一些简单新特性(静态导入、可变参数、增强for循环、自动拆箱装箱及享元)
- 黑马程序员-JavaBean
- 十五个只有程序员会乐的事情
- 面试题
- 2014腾讯校招面试之一总结