Practice Every Day_17(Map集合小练习)
2012-05-08 11:02
381 查看
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class MapPractice
{
public static void main(String[] args)
{
TreeMap<Student,String> tm=new TreeMap<Student,String>(new NameComparator());
tm.put(new Student("wang",34),"北京");
tm.put(new Student("li",21),"上海");
tm.put(new Student("wu",19),"潍坊");
tm.put(new Student("liu",18),"淄博");
Set<Map.Entry<Student, String>> enSet=tm.entrySet();
Iterator<Map.Entry<Student, String>> ite=enSet.iterator();
while(ite.hasNext())
{
Map.Entry<Student,String> me=ite.next();
Student stu=me.getKey();
String add=me.getValue();
System.out.println(stu+"..."+add);
}
HashMap<Student,String> hm=new HashMap<Student,String>();
hm.put(new Student("wang",34),"北京");
hm.put(new Student("li",21),"上海");
hm.put(new Student("wu",19),"潍坊");
hm.put(new Student("liu",18),"淄博");
//第一种取出方式:keySet
Set<Student> keySet=hm.keySet();
Iterator<Student> it=keySet.iterator();
while(it.hasNext())
{
Student stu=it.next();
String add=hm.get(stu);
System.out.println(stu+".."+add);
}
//第二种取出方式: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 add=me.getValue();
System.out.println(stu+"..."+add);
}
}
}
class Student implements Comparable<Student>
{
private String name;
private int age;
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 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 int getAge()
{
return age;
}
public String getName()
{
return name;
}
public String toString()
{
return name+".."+age;
}
}
class NameComparator 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;
}
}
相关文章推荐
- Practice Every Day_7(Lucene3.5小练习)
- Practice Every Day_6 (Lucene 小练习)
- 19-Map集合-10-常用对象API(集合框架-Map集合练习-记录字母次数思路)
- Robin Hu的专栏Let's practice improving every day
- 泛型练习(创建泛型类操作Map集合里的对象)
- 19-Map集合-12-常用对象API(集合框架-Map集合练习-Map查表法)
- 集合框架-Map练习-记录字母出现的次数
- [置顶] java集合框架的练习之Map集合以及其内部接口Entry的应用
- 17-集合框架-14-常用对象API(集合框架-LinkedList集合-练习(堆栈和队列))
- 【java编程】Map集合之hashMap两种取出方式练习
- map集合小练习
- Practice Every Day_4
- Map集合练习之对字符串中字母出现的次数求和
- Practice Every Day_13
- (73)Map公共方法演示、Map集合获得键值对的两种方法:entrySet和keySet。Map中元素唯一性练习
- 英语每日听写练习 Day 17
- 17-集合框架-20-常用对象API(集合框架练习)
- Practice Every Day_8
- 黑马程序员_Map集合练习和工具类以及一些新的知识
- Practice Every Day_9(更新索引)