`
- 浏览:
21539981 次
- 性别:
- 来自:
杭州
-
/**
* 通过这个程序,测试树映像的使用,表目集合的遍历
*/
importjava.util.TreeMap;
importjava.util.Map;
importjava.util.Iterator;
importjava.util.Set;
publicclassTreeMapTest
{
publicstaticvoidmain(String[]args)
{
TreeMapset=newTreeMap();
TreeMapTesttest=newTreeMapTest();
//生成三个学生实例
Studenttom=newStudent("Tom","20020410");
Studentjack=newStudent("Jack","20020411");
Studentsmith=newStudent("Smith","20020412");
set.put("one",tom);
set.put("two",jack);
set.put("three",smith);
System.out.println("现在映像中元素的个数是:"+set.size()+"/n");
test.getValue(set);
System.out.println("/n现在设置重复关键字,覆盖原来的对象。/n");
Studentrose=newStudent("Rose","20020413");
System.out.println("覆盖已经存在的关键字时返回值:"+set.put("two",rose)+"/n");
test.getValue(set);
}
publicvoidgetValue(TreeMapset)
{
System.out.println("遍历映像中的关键字/值对分别是:");
Setentries=set.entrySet();//得到关键字/值对的集合
Iteratoriter=entries.iterator();
while(iter.hasNext())
{
Map.Entryentry=(Map.Entry)iter.next();
Stringkey=(String)entry.getKey();
Studentvalue=(Student)entry.getValue();
System.out.print(key);
System.out.println("/t/t"+value);
}
}
}
/**
*我们设计的学生基本类
*/
classStudent
{
privateStringstrName="";//学生姓名
privateStringstrNumber="";//学号
privateStringstrSex="";//性别
privateStringstrBirthday="";//出生年月
privateStringstrSpeciality="";//专业
privateStringstrAddress="";//地址
publicStudent(Stringname,Stringnumber)
{
strName=name;
strNumber=number;
}
publicStringgetStudentName()
{
returnstrName;
}
publicStringgetStudentNumber()
{
returnstrNumber;
}
publicvoidsetStudentSex(Stringsex)
{
strSex=sex;
}
publicStringgetStudentSex()
{
returnstrSex;
}
publicStringgetStudentBirthday()
{
returnstrBirthday;
}
publicvoidsetStudentBirthday(Stringbirthday)
{
strBirthday=birthday;
}
publicStringgetStudentSpeciality()
{
returnstrSpeciality;
}
publicvoidsetStudentSpeciality(Stringspeciality)
{
strSpeciality=speciality;
}
publicStringgetStudentAddress()
{
returnstrAddress;
}
publicvoidsetStudentAddress(Stringaddress)
{
strAddress=address;
}
publicStringtoString()
{
Stringinformation="学生姓名="+strName+",学号="+strNumber;
if(!strSex.equals(""))
information+=",性别="+strSex;
if(!strBirthday.equals(""))
information+=",出生年月="+strBirthday;
if(!strSpeciality.equals(""))
information+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information+=",籍贯="+strAddress;
returninformation;
}
}
/**
* 通过这个程序,测试树映像的使用,表目集合的遍历
*/
importjava.util.TreeMap;
importjava.util.Map;
importjava.util.Iterator;
importjava.util.Set;
publicclassTreeMapTest2
{
publicstaticvoidmain(String[]args)
{
TreeMapset=newTreeMap(newReverseSort());
TreeMapTest2test=newTreeMapTest2();
//生成三个学生实例
Studenttom=newStudent("Tom","20020410");
Studentjack=newStudent("Jack","20020411");
Studentsmith=newStudent("Smith","20020412");
set.put("one",tom);
set.put("two",jack);
set.put("three",smith);
System.out.println("现在映像中元素的个数是:"+set.size()+"/n");
test.getValue(set);
System.out.println("/n现在设置重复关键字,覆盖原来的对象。/n");
Studentrose=newStudent("Rose","20020413");
System.out.println("覆盖已经存在的关键字时返回值:"+set.put("two",rose)+"/n");
test.getValue(set);
}
publicvoidgetValue(TreeMapset)
{
System.out.println("遍历映像中的关键字/值对分别是:");
Setentries=set.entrySet();//得到关键字/值对的集合
Iteratoriter=entries.iterator();
while(iter.hasNext())
{
Map.Entryentry=(Map.Entry)iter.next();
Stringkey=(String)entry.getKey();
Studentvalue=(Student)entry.getValue();
System.out.print(key);
System.out.println("/t/t"+value);
}
}
}
/**
*我们设计的学生基本类
*/
classStudent
{
privateStringstrName="";//学生姓名
privateStringstrNumber="";//学号
privateStringstrSex="";//性别
privateStringstrBirthday="";//出生年月
privateStringstrSpeciality="";//专业
privateStringstrAddress="";//地址
publicStudent(Stringname,Stringnumber)
{
strName=name;
strNumber=number;
}
publicStringgetStudentName()
{
returnstrName;
}
publicStringgetStudentNumber()
{
returnstrNumber;
}
publicvoidsetStudentSex(Stringsex)
{
strSex=sex;
}
publicStringgetStudentSex()
{
returnstrSex;
}
publicStringgetStudentBirthday()
{
returnstrBirthday;
}
publicvoidsetStudentBirthday(Stringbirthday)
{
strBirthday=birthday;
}
publicStringgetStudentSpeciality()
{
returnstrSpeciality;
}
publicvoidsetStudentSpeciality(Stringspeciality)
{
strSpeciality=speciality;
}
publicStringgetStudentAddress()
{
returnstrAddress;
}
publicvoidsetStudentAddress(Stringaddress)
{
strAddress=address;
}
publicStringtoString()
{
Stringinformation="学生姓名="+strName+",学号="+strNumber;
if(!strSex.equals(""))
information+=",性别="+strSex;
if(!strBirthday.equals(""))
information+=",出生年月="+strBirthday;
if(!strSpeciality.equals(""))
information+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information+=",籍贯="+strAddress;
returninformation;
}
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
在JavaEE中 TreeMap排序的问题
主要为大家详细介绍了java中treemap和treeset实现红黑树,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本篇文章主要介绍了java中TreeMap排序的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Java TreeMap源码解析 Java TreeMap源码解析 Java TreeMap源码解析
This document helps you to program objects for the Java TreeMap collection.
JAVA中用TREEMAP做的,可以求学生的总分,总分的平均值,最大值最小值
在eclipse上用java实现统计一句话或一段话中单词出现的个数,并按照字母表顺序输出,并利用treemap实现
java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
比较Java原生的 3种Map的效率。 1. TreeMap 2. HashMap 3. ConcurrentSkipListMap 本测试查找方法使用Map的get方法,循环、离散获取。对于ConcurrentSkipListMap,获得顺序片段,可用subMap()方法,提取50w的子序列...
TreeMap源码是基于数据结构中的红黑树进行设计并开发的。
TreeMap是一种基于红黑树实现的有序映射(SortedMap)。它实现了NavigableMap接口,可以按照键的自然顺序或自定义排序规则对键值对进行排序和访问。
主要介绍了JAVA使用TreeMap对字符串进行排序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Java TreeMap排序算法,结合实例形式分析了TreeMap排序算法的原理、实现方法与相关注意事项,需要的朋友可以参考下
TreeMap源码解读.java
Java中AVL平衡二叉树实现Map_(仿照TreeMap和TreeSet)1
通过分析java.util.TreeMap源码来对经典问题红黑树加强理解和理清思路。
继上篇文章介绍完了HashMap,这篇文章开始介绍Map系列另一个比较重要的类TreeMap。 大家也许能感觉到,网络上介绍HashMap的文章比较多,但是介绍TreeMap反而... implements NavigableMap, Cloneable, java.io.Seria
下面小编就为大家带来一篇浅谈java中的TreeMap 排序与TreeSet 排序。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
用一段JAVA程序实现对英文文章的单词统计,以及按照词频排序
本资源提供了List对对象中的属性和TreeMap, String>对键值排序,并针对100w条数据排序,对比List和TreeMap, String>排序的效率。个人认为排序效率对比可以相信,但也可能存在不科学之处,还请高手给与指点,多多包涵...