1、获取字符串中每一个字母出现的次数。
  比如"aababcabcdabcde",结果为:a(5)b(4)c(3)d(2)e(1)
分析如下:

package mapexercise;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
/**
* Created by gao on 15-12-22.
*/
/*
* 需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
*
*/
public class TreeMapDemo {
public static void main(String[] args) {
// 定义一个字符串(可以改进为键盘录入)
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
// 定义一个TreeMap集合
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
//把字符串转换为字符数组
char[] chs = line.toCharArray();
//遍历字符数组,得到每一个字符
for (char ch : chs) {
//拿刚才得到的字符作为键到集合中去找值,看返回值
Integer i = tm.get(ch);
//是null:说明该键不存在,就把该字符作为键,1作为值存储
if (i == null) {
tm.put(ch, 1);
} else {
i++;
tm.put(ch, i);
}
}
//定义字符串缓冲区变量
StringBuffer sb = new StringBuffer();
//遍历集合,得到键和值,进行按照要求拼接
Set<Character> set = tm.keySet();
for (Character key : set) {
Integer value = tm.get(key);
sb.append(key).append("(").append(value).append(")");
}
//把字符串缓冲区转换为字符串输出
String result = sb.toString();
System.out.println("result:" + result);
}
}
输出结果:
请输入一个字符串:
aababcabcdabcde
result:a(5)b(4)c(3)d(2)e(1)
 
2、集合的嵌套遍历
    1)HashMap嵌套HashMap
package mapexercise;
import java.util.HashMap;
import java.util.Set;
/**
* Created by gao on 15-12-22.
*/
/*
* HashMap嵌套HashMap
*/
public class HashMapDemo01 {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, HashMap<String, Integer>> hm = new HashMap<String, HashMap<String, Integer>>();
// 创建班级1集合对象
HashMap<String, Integer> ghm1 = new HashMap<String, Integer>();
// 添加元素
ghm1.put("小明", 22);
ghm1.put("小红", 23);
// 把班级1添加到大集合
hm.put("Grade1", ghm1);
// 创建班级2集合对象
HashMap<String, Integer> ghm2 = new HashMap<String, Integer>();
// 添加元素
ghm2.put("小青", 28);
ghm2.put("小宝", 23);
// 把班级2添加到大集合
hm.put("Grade2", ghm2);
//遍历集合
Set<String> gradeSet = hm.keySet();
for (String gradeKey : gradeSet) {
System.out.println(gradeKey + ":");
HashMap<String, Integer> gradeValue = hm.get(gradeKey);
Set<String> studentSet = gradeValue.keySet();
for (String key : studentSet) {
Integer value = gradeValue.get(key);
System.out.println(key + "---" + value);
}
}
}
}
输出结果:
Grade2:
小宝---23
小青---28
Grade1:
小明---22
小红---23
 
  2)HashMap嵌套ArrayList

package mapexercise;
/**
* Created by gao on 15-12-22.
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/**
* HashMap集合的元素是ArrayList
*/
public class HashMapDemo02 {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
// 创建元素集合1
ArrayList<String> array1 = new ArrayList<String>();
array1.add("吕布");
array1.add("周瑜");
hm.put("三国演义", array1);
// 创建元素集合2
ArrayList<String> array2 = new ArrayList<String>();
array2.add("令狐冲");
array2.add("林平之");
hm.put("笑傲江湖", array2);
// 创建元素集合3
ArrayList<String> array3 = new ArrayList<String>();
array3.add("郭靖");
array3.add("杨过");
hm.put("神雕侠侣", array3);
//遍历集合
Set<String> set = hm.keySet();
for (String key : set) {
System.out.println(key + ":");
ArrayList<String> value = hm.get(key);
for (String s : value) {
System.out.println("\t" + s);
}
}
}
}
输出结果:
三国演义:
吕布
周瑜
笑傲江湖:
令狐冲
林平之
神雕侠侣:
郭靖
杨过
 
  3)ArrayList嵌套HashMap
package mapexercise;
/**
* Created by gao on 15-12-22.
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/**
* ArrayList集合嵌套HashMap集合并遍历。
*/
public class HashMapDemo03 {
public static void main(String[] args) {
// 创建集合对象
ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
// 创建元素1
HashMap<String, String> hm1 = new HashMap<String, String>();
hm1.put("周瑜", "小乔");
hm1.put("吕布", "貂蝉");
// 把元素添加到array里面
array.add(hm1);
// 创建元素2
HashMap<String, String> hm2 = new HashMap<String, String>();
hm2.put("郭靖", "黄蓉");
hm2.put("杨过", "小龙女");
// 把元素添加到array里面
array.add(hm2);
// 创建元素3
HashMap<String, String> hm3 = new HashMap<String, String>();
hm3.put("令狐冲", "任盈盈");
hm3.put("林平之", "岳灵珊");
// 把元素添加到array里面
array.add(hm3);
// 遍历
for (HashMap<String, String> hm : array) {
Set<String> set = hm.keySet();
for (String key : set) {
String value = hm.get(key);
System.out.println(key + "--" + value);
}
}
}
}
输出结果:
周瑜--小乔
吕布--貂蝉
郭靖--黄蓉
杨过--小龙女
令狐冲--任盈盈
林平之--岳灵珊
 
  4)HashMap嵌套HashMap嵌套HashMap(三层嵌套)
package cn.itcast_06;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/*
* 为了更符合要求:
* 这次的数据就看成是学生对象。
*
* 传智播客
* bj 北京校区
* jc 基础班
* 林青霞 27
* 风清扬 30
* jy 就业班
* 赵雅芝 28
* 武鑫 29
* sh 上海校区
* jc 基础班
* 郭美美 20
* 犀利哥 22
* jy 就业班
* 罗玉凤 21
* 马征 23
* gz 广州校区
* jc 基础班
* 王力宏 30
* 李静磊 32
* jy 就业班
* 郎朗 31
* 柳岩 33
* xa 西安校区
* jc 基础班
* 范冰冰 27
* 刘意 30
* jy 就业班
* 李冰冰 28
* 张志豪 29
*/
public class HashMapDemo {
public static void main(String[] args) {
// 创建大集合
HashMap<String, HashMap<String, ArrayList<Student>>> czbkMap = new HashMap<String, HashMap<String, ArrayList<Student>>>();
// 北京校区数据
HashMap<String, ArrayList<Student>> bjCzbkMap = new HashMap<String, ArrayList<Student>>();
ArrayList<Student> array1 = new ArrayList<Student>();
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
array1.add(s1);
array1.add(s2);
ArrayList<Student> array2 = new ArrayList<Student>();
Student s3 = new Student("赵雅芝", 28);
Student s4 = new Student("武鑫", 29);
array2.add(s3);
array2.add(s4);
bjCzbkMap.put("基础班", array1);
bjCzbkMap.put("就业班", array2);
czbkMap.put("北京校区", bjCzbkMap);
// 晚上可以自己练习一下
// 上海校区数据自己做
// 广州校区数据自己做
// 西安校区数据
HashMap<String, ArrayList<Student>> xaCzbkMap = new HashMap<String, ArrayList<Student>>();
ArrayList<Student> array3 = new ArrayList<Student>();
Student s5 = new Student("范冰冰", 27);
Student s6 = new Student("刘意", 30);
array3.add(s5);
array3.add(s6);
ArrayList<Student> array4 = new ArrayList<Student>();
Student s7 = new Student("李冰冰", 28);
Student s8 = new Student("张志豪", 29);
array4.add(s7);
array4.add(s8);
xaCzbkMap.put("基础班", array3);
xaCzbkMap.put("就业班", array4);
czbkMap.put("西安校区", xaCzbkMap);
// 遍历集合
Set<String> czbkMapSet = czbkMap.keySet();
for (String czbkMapKey : czbkMapSet) {
System.out.println(czbkMapKey);
HashMap<String, ArrayList<Student>> czbkMapValue = czbkMap
.get(czbkMapKey);
Set<String> czbkMapValueSet = czbkMapValue.keySet();
for (String czbkMapValueKey : czbkMapValueSet) {
System.out.println("\t" + czbkMapValueKey);
ArrayList<Student> czbkMapValueValue = czbkMapValue
.get(czbkMapValueKey);
for (Student s : czbkMapValueValue) {
System.out.println("\t\t" + s.getName() + "---"
+ s.getAge());
}
}
}
}
}

3、模拟斗地主洗牌和发牌(未排序)

package collectiondemos;
import java.util.ArrayList;
import java.util.Collections;
/**
* Created by gao on 15-12-22.
*/
/*
* 模拟斗地主洗牌和发牌
*
* 分析:
* A:创建一个牌盒
* B:装牌
* C:洗牌
* D:发牌
* E:看牌
*/
public class PokerDemo01 {
public static void main(String[] args) {
// 创建一个牌盒
ArrayList<String> array = new ArrayList<String>();
// 装牌
// 黑桃A,黑桃2,黑桃3,...黑桃K
// 红桃A,...
// 梅花A,...
// 方块A,...
// 定义一个花色数组
String[] colors = {"?", "?", "?", "?"};
// 定义一个点数数组
String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K"};
// 装牌
for (String color : colors) {
for (String number : numbers) {
array.add(color.concat(number));
}
}
array.add("小王");
array.add("大王");
// 洗牌
Collections.shuffle(array);
// 发牌
ArrayList<String> fengQingYang = new ArrayList<String>();
ArrayList<String> linQingXia = new ArrayList<String>();
ArrayList<String> liuYi = new ArrayList<String>();
ArrayList<String> diPai = new ArrayList<String>();
for (int x = 0; x < array.size(); x++) {
if (x >= array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
}
}
// 看牌
lookPoker("风清扬", fengQingYang);
lookPoker("林青霞", linQingXia);
lookPoker("刘意", liuYi);
lookPoker("底牌", diPai);
}
public static void lookPoker(String name, ArrayList<String> array) {
System.out.print(name + "的牌是:");
for (String s : array) {
System.out.print(s + " ");
}
System.out.println();
}
}

4、模拟斗地主洗牌和发牌(排序)

package cn.itcast_04;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
/*
* 思路:
* A:创建一个HashMap集合
* B:创建一个ArrayList集合
* C:创建花色数组和点数数组
* D:从0开始往HashMap里面存储编号,并存储对应的牌
* 同时往ArrayList里面存储编号即可。
* E:洗牌(洗的是编号)
* F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
* G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
*/
public class PokerDemo {
public static void main(String[] args) {
// 创建一个HashMap集合
HashMap<Integer, String> hm = new HashMap<Integer, String>();
// 创建一个ArrayList集合
ArrayList<Integer> array = new ArrayList<Integer>();
// 创建花色数组和点数数组
// 定义一个花色数组
String[] colors = { "♠", "♥", "♣", "♦" };
// 定义一个点数数组
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A", "2", };
// 从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。
int index = 0;
for (String number : numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(index, poker);
array.add(index);
index++;
}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
// 洗牌(洗的是编号)
Collections.shuffle(array);
// 发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
TreeSet<Integer> linQingXia = new TreeSet<Integer>();
TreeSet<Integer> liuYi = new TreeSet<Integer>();
TreeSet<Integer> diPai = new TreeSet<Integer>();
for (int x = 0; x < array.size(); x++) {
if (x >= array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
}
}
// 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
lookPoker("风清扬", fengQingYang, hm);
lookPoker("林青霞", linQingXia, hm);
lookPoker("刘意", liuYi, hm);
lookPoker("底牌", diPai, hm);
}
// 写看牌的功能
public static void lookPoker(String name, TreeSet<Integer> ts,
HashMap<Integer, String> hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String value = hm.get(key);
System.out.print(value + " ");
}
System.out.println();
}
}
5、面试题
  1)HashMap和Hashtable的区别
      HashMap用来替代Hashtable
        · Hashtable:线程安全,效率低。不允许null键和null值
       ·  HashMap:线程不安全,效率高。允许null键和null值
package mapexercise;
import java.util.HashMap;
import java.util.Hashtable;
/**
* Created by gao on 15-12-22.
*/
public class HashMapDemo04 {
public static void main(String[] args) {
//HashMap<String,String> hm = new HashMap<String, String>(); //{null=hello, java=null, it001=world}
Hashtable<String,String> ht = new Hashtable<String, String>();
ht.put("it001","world");
ht.put(null,"hello"); //NullPointerException
ht.put("java",null); //NullPointerException
System.out.println(ht);
}
}
 
  2)List,Set,Map等接口是否都继承自Map接口
    · List,Set不是继承自Map接口,它们继承自Collection接口
    · Map接口本身就是一个顶层接口
 
  3)Collection和Collections的区别?
    · Collection:是单列集合的顶层接口,有子接口List和Set。
    · Collections:是针对集合操作的工具类,有对集合进行排序和二分查找的方法
 
 
 

Map集合案例的更多相关文章

  1. java代码中后台向前台传递list或map集合案例

    导入jar包 新建一个servert传递map集合 ajax.java代码: package servlet; import java.io.IOException; import java.io.P ...

  2. Java基础知识强化之集合框架笔记57:Map集合之HashMap集合(HashMap<Student,String>)的案例

    1. HashMap集合(HashMap<Student,String>)的案例 HashMap<Student,String>键:Student      要求:如果两个对象 ...

  3. Java基础知识强化之集合框架笔记56:Map集合之HashMap集合(HashMap<String,Student>)的案例

    1. HashMap集合(HashMap<String,Student>)的案例 HashMap是最常用的Map集合,它的键值对在存储时要根据键的哈希码来确定值放在哪里. HashMap的 ...

  4. 计算一个字符串的每个字符出现的次数案例——Map集合

    其中,字符的包装类是Character;字符串包装类是String: 遍历字符串转换的数组,每个元素都是一个字符,看创建的这个集合有木有,一开始肯定是没有的其实,字符作为key,所以判断的是这个创建的 ...

  5. Map集合

    1:Map (1)将键映射到值的对象. 一个映射不能包含重复的键:每个键最多只能映射到一个值. 键值对的方式存在 (2)Map和Collection的区别? A:Map 存储的是键值对形式的元素,键唯 ...

  6. Map集合、HashMap集合、LinkedHashMap集合、Hashtable集合、Collections工具类和模拟斗地主洗牌和发牌

    1.Map集合概述和特点 * A:Map接口概述  * 查看API可以知道:          * 将键映射到值的对象          * 一个映射不能包含重复的键          * 每个键最多 ...

  7. 牛客网Java刷题知识点之Java 集合框架的构成、集合框架中的迭代器Iterator、集合框架中的集合接口Collection(List和Set)、集合框架中的Map集合

    不多说,直接上干货! 集合框架中包含了大量集合接口.这些接口的实现类和操作它们的算法. 集合容器因为内部的数据结构不同,有多种具体容器. 不断的向上抽取,就形成了集合框架. Map是一次添加一对元素. ...

  8. Collections工具类、Map集合、HashMap、Hashtable(十八)

    1.Map集合概述和特点 * A:Map接口概述 * 去重复, * 查看API可以知道, * 将键映射到值的对象, * 一个映射不能包含重复的键, * 每个键最多只能映射到一个值.* B:Map接口和 ...

  9. 红黑树规则,TreeSet原理,HashSet特点,什么是哈希值,HashSet底层原理,Map集合特点,Map集合遍历方法

    ==学习目标== 1.能够了解红黑树 2.能够掌握HashSet集合的特点以及使用(特点以及使用,哈希表数据结构) 3.能够掌握Map集合的特点以及使用(特点,常见方法,Map集合的遍历) 4.能够掌 ...

随机推荐

  1. 1093. Count PAT's (25)

    The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and ...

  2. 未能加载文件或程序集“Report.Basic”或它的某一个依赖项。试图加载格式不正确的程序

    出现问题如下: 解决办法: 这是由于没有开启32位程序兼容模式 具体操作如下:找到对应的程序池--------高级设置-------修改“启用32位应用程序”状态修改为true

  3. mapreduce 实现矩阵乘法

    import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...

  4. C++实现数字媒体二维图像变换

    C++实现数字媒体二维图像变换 必备环境 glut.h 头文件 glut32.lib 对象文件库 glut32.dll 动态连接库 程序说明 C++实现了用glut画正方形,画三角形的功能.并附带放大 ...

  5. NOIP2015-stone(二分答案)

    这道题在考试时二分答案写炸了,结果得了20分.....同学有用贪心写的(对,贪心!!)都得了30,我感到了深深的恶意.这段时间在忙转语言,现在重新整理一下NOIP的题. 题目来源:vijos 题目如下 ...

  6. 两行代码搞定UITableView无数据无网络显示-b

    不知是否有像我一样的,每次写TableView在监听网络和无数据源时逻辑处理提示视图都是一堆代码,很繁琐也很重复的垃圾代码(可能就只有我这样

  7. 屌丝IT男

    偶尔翻到豆瓣里一篇对中国屌丝的批评,突然想到当年美国那个垮掉的一代,吸毒,淫乱,绝望的生存,而如今我们苦逼的80后自诩为屌丝的时候,也不想想每一个堕落的时代还是有牛逼的人存在,中国的大学,绝大部分在逃 ...

  8. NopCommerce——Web层中的布局页

    援引上一篇文章关于nopcommerce源代码结构的翻译:“Nop.Web也是一个MVC Web应用程序项目,一个公有区域的展示层.它就是你实际能够运行的应用程序.它是应用程序的启动项目”.对于nop ...

  9. OC 加密

    //MD5加密的结果为128位的二进制数. //所以有128 / 8 = 16字节(8位一个字节). //每八位表示两个16进制数. //MD5 有32个16进制数. //语言层次的所有摘要算法步骤类 ...

  10. BZOJ 1143: [CTSC2008]祭祀river 最大独立集

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1143 题解: 给你一个DAG,求最大的顶点集,使得任意两个顶点之间不可达. 把每个顶点v ...