map集合练习
来源:互联网 发布:usb控制软件破解版 编辑:程序博客网 时间:2024/04/27 15:23
/*练习: 每一个学生都有对应的归属地。
* 学生Student,地址String。
* 学生属性:姓名,年龄。
* 注意:姓名和年龄相同的视为同一个学生。
* 保证学生的唯一性。
* Map集合练习:
* 1.描述学生。
* 2.定义map容器,将学生作为键,地址作为值,存入。
* 3.后去map集合中的元素。
*
*/
class Student implements Comparable<Student>
{
private String name;
private int age;
public Student(String name,int age) {
this.name=name;
this.age=age;
}
//元素有可能会存入二叉树中,所以要对元素进行自然排序。
public int compareTo(Student s)
{
int num=new Integer(this.age).compareTo(s.age);
if(num==0)
{
return this.name.compareTo(s.name);
}
return num;
}
public String getName()
{
return name;
}
public int getAge(){
return age;
}
public String toString()
{
return name+":"+age;
}
//元素可能回存入hash表中,所以要判断元素的唯一性,添加元素就会调用的两个方法。
public int hashCode()
{
return name.hashCode()+age*34;
}
public boolean equals (Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s=(Student)obj;
return (this.name.equals(s.name)) && (this.age==s.age);
}
}
public class MapTest {
public static void main(String[] args) {
HashMap<Student,String> hm=new HashMap<Student,String>();
hm.put(new Student("lis1",21),"beijing");
hm.put(new Student("lis3",23),"shanghai");
hm.put(new Student("lis4",24),"changsha");
hm.put(new Student("lis2",22),"nanjing");
//第一种存取方式。
Set<Student> keySet=hm.keySet();
Iterator<Student> it=keySet.iterator();
while(it.hasNext())
{
Student stu=it.next();
String addr=hm.get(stu);
sop(stu+"---"+addr);
}
//第二种取出方式entrySet
Set<Map.Entry<Student,String>> entrySet =hm.entrySet();
Iterator<Map.Entry<Student,String>> it1=entrySet.iterator();
while(it1.hasNext())
{
Map.Entry<Student,String> me= it1.next();
Student tu=me.getKey();
String addr=me.getValue();
sop(tu+"----"+addr);
}
}
public static <T> void sop(T t){
System.out.println(t);
}
}
import java.util.*;
/*
* TreeSet练习:
* 因为数据时以键值对形式存在的。所以要使用可以排序的map集合,TreeMap。
*
*/
public class MapTest2 {
public static void main(String[] args) {
//以比较器的比较为主。
TreeMap<Student,String> tm=new TreeMap<Student,String>(new MyCompare());
tm.put(new Student("blis1",21),"beijing");
tm.put(new Student("lis3",23),"shanghai");
tm.put(new Student("alis4",24),"changsha");
tm.put(new Student("alis4",24),"chang");
tm.put(new Student("slis2",22),"nanjing");
Set<Map.Entry<Student,String>> entrySet =tm.entrySet();
Iterator<Map.Entry<Student,String>> it1=entrySet.iterator();
while(it1.hasNext())
{
Map.Entry<Student,String> me= it1.next();
Student tu=me.getKey();
String addr=me.getValue();
sop(tu+"----"+addr);
}
}
public static <T> void sop(T t){
System.out.println(t);
}
}
//定义姓名比较器
class MyCompare implements Comparator<Student>
{
public int compare (Student s1,Student s2)
{
int num=s1.getName().compareTo(s2.getName());
if(num==0)
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
return num;
}
}
import java.util.*;
/*
* 练习:
* "sdfgzxcvasdfxcvdf"获取该字符串中字母出现的次数。
* 希望打印结果:a(1),c(2)....
* 因为字母和次数之间有映射关系,所以使用map集合。
* 思路:
* 1.将字符串转换成数组,因为要对每一个字母进行操作。
* 2.定义一个map集合,因为打印结果的字母有顺序,所以使用treemap集合。
* 3.遍历字符数组。
* 将每一个字母作为键去查map集合。
* 如果返回null,将字母和1存入到map集合中。
* 如果返回不是null,说明该map集合已经存在并有对应次数。
* 那么就获取该次数并进行自增,然后将字母和自增后的次数存入到map集合。
* 4.将map集合中的数据编程指定的字符串形式返回。
*/
public class MapTest3 {
public static void main(String[] args) {
String str="sdfgzxcvasdfxcvdf";
sop(charCount(str));
}
public static String charCount(String str)
{
int count=0;
// 1.调用toCharArray将字符串转换成数组,因为要对每一个字母进行操作。
char[] chs=str.toCharArray();
TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();
for(int x=0;x<chs.length;x++)
{
//用get方法判断是否包含此键,包含返回
Integer value=tm.get(chs[x]);
if(value!=null)
{
count=value;
}
count++;
tm.put(chs[x], count);
count=0;
/*if(value==null)
{
tm.put(chs[x],1);
}
else
{
value+=1;
tm.put(chs[x], value);
}
*/
}
//sop(tm);
StringBuilder sb=new StringBuilder();
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();
sb.append(ch+"("+value+")");
}
return sb.toString();
}
public static<T> void sop(T t)
{
System.out.println(t);
}
}
/*
* map扩展知识。
* map集合被使用是因为具有映射关系。
* 集合嵌套集合
*/
import java.util.*;
class Student0{
private String name;
private String num;
Student0(String num,String name)
{
this.name=name;
this.num=num;
}
public String toString()
{
return num+":"+name;
}
}
public class MapDemo4 {
public static void demo()
{
HashMap<String,List<Student0>> czbk=new HashMap<String,List<Student0>>();
List<Student0> yure=new ArrayList<Student0>();
List<Student0> jiuye=new ArrayList<Student0>();
czbk.put("yureban", yure);
czbk.put("jiuyeban",jiuye);
yure.add(new Student0("01","zhangsan"));
yure.add(new Student0("02","lisi"));
jiuye.add(new Student0("01","zhaoliu"));
jiuye.add(new Student0("02","wangwu"));
Iterator<String> it=czbk.keySet().iterator();
while(it.hasNext())
{
String roomname=it.next();
List room=czbk.get(roomname);
getInfo(room);
}
}
public static void getInfo(List<Student0> list)
{
Iterator<Student0> it=list.iterator();
while(it.hasNext())
{
Student0 s=it.next();
System.out.println(s.toString());
}
}
public static void main(String[] args) {
demo();
/*HashMap<String,HashMap<String,String>> czbk=new HashMap<String,HashMap<String,String>>();
HashMap<String,String> yure=new HashMap<String,String>();
HashMap<String,String> jiuye=new HashMap<String,String>();
czbk.put("yureban", yure);
czbk.put("jiuyeban",jiuye);
yure.put("01","zhangsan");
yure.put("02","lisi");
jiuye.put("01","zhaoliu");
jiuye.put("02","wangwu");
//遍历czbk集合,获取所有的教室。
Iterator<String> it=czbk.keySet().iterator();
while(it.hasNext())
{
String roomname=it.next();
HashMap<String,String> room=czbk.get(roomname);
getStudentInfo(room);
}
*/
}
public static void getStudentInfo(HashMap<String,String> room)
{
Iterator<String> it=room.keySet().iterator();
while(it.hasNext())
{
String id=it.next();
String name=room.get(id);
System.out.println(id+":"+name);
}
}
}
- java Map集合练习
- 集合(Map练习)
- map集合练习
- Map集合练习
- [黑马程序员]集合map练习
- 《黑马程序员》 map集合练习
- JAVA的map集合练习
- Practice Every Day_17(Map集合小练习)
- 19-Map集合-12-常用对象API(集合框架-Map集合练习-Map查表法)
- 集合框架--Map集合之记录字母次数练习
- 19-Map集合-10-常用对象API(集合框架-Map集合练习-记录字母次数思路)
- 19-Map集合-11-常用对象API(集合框架-Map集合练习-记录字母次数代码)
- map集合练习——记录字母次数
- Map练习!!!
- Map练习
- Map练习
- java集合框架的练习之Map集合以及其内部接口Entry的应用
- Map 集合
- 使用Fragment +searchview过程中遇到的问题
- using System..................
- hdu1035 Robot Motion (DFS)
- Integer to Roman
- 记录:点击popwindow外部不消失bug解决
- map集合练习
- HDU - 5093 Battle ships 初学 二分图匹配
- 29.导航控制器和页面间的传值
- C/C++中的二维数组动态内存分配与释放
- Http网络传递参数中文乱码问题
- SLAM: SLAM的发展历程
- day17
- hdu1024 N! (数组模拟乘法过程)
- 九度OJ 题目1029:魔咒词典