Treeset树集适用于数据的排序;树映射TreeMap,必须保证节点中的关键字是升序排列的
2009-11-21 16:53
295 查看
package com.fuxi.test.collection;
/**
* Treeset树集适用于数据的排序;树映射TreeMap,必须保证节点中的关键字是升序排列的
* 此例是分别按着学生的英语成绩和数学成绩排序
*/
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
public class TreeMapTest {
public static void main(String[] args) {
TreeMap<StudentKey, Student3> map = new TreeMap<StudentKey, Student3>();
String str[] = {"zhangyi","zhanger","zhangsan","zhangsi"};
double math[] = {89,78,98,21};
double english[] = {43,53,64,23};
Student3 student[] = new Student3[4];
for(int i = 0;i<student.length;i++){
student[i] = new Student3(str[i],math[i],english[i]);
}
StudentKey key[] = new StudentKey[4];
for(int i = 0;i<key.length;i++){
key[i] = new StudentKey(student[i].math);
}
for(int i = 0;i<student.length;i++){
map.put(key[i], student[i]);
}
int number = map.size();
System.out.println("树映射中有"+number+"个对象,按数学成绩排序:");
Collection<Student3> collection = map.values();
Iterator<Student3> it = collection.iterator();
while(it.hasNext()){
Student3 stu = it.next();
System.out.println("姓名"+stu.name+"数学"+stu.math);
}
map.clear();
for(int i = 0;i<key.length;i++){
key[i] = new StudentKey(student[i].english);
}
for(int i =0;i<student.length;i++){
map.put(key[i], student[i]);
}
number = map.size();
System.out.println("树映射中有"+number+"个对象:按英语成绩排序:");
Collection<Student3> collection2 = map.values();
Iterator<Student3> it2 = collection2.iterator();
while(it2.hasNext()){
Student3 stu = it2.next();
System.out.println("姓名:"+stu.name+"英语"+stu.english);
}
}
}
class StudentKey implements Comparable{
double d = 0;
public StudentKey(double d){
this.d =d ;
}
public int compareTo(Object b) {
StudentKey st = (StudentKey) b;
if((this.d-st.d)==0){
return -1;
}else{
return (int) ((this.d-st.d)*1000);
}
}
}
class Student3{
String name;
double math,english;
public Student3(String name,double math,double english){
this.english = english;
this.name = name;
this.math = math;
}
}
/**
* Treeset树集适用于数据的排序;树映射TreeMap,必须保证节点中的关键字是升序排列的
* 此例是分别按着学生的英语成绩和数学成绩排序
*/
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
public class TreeMapTest {
public static void main(String[] args) {
TreeMap<StudentKey, Student3> map = new TreeMap<StudentKey, Student3>();
String str[] = {"zhangyi","zhanger","zhangsan","zhangsi"};
double math[] = {89,78,98,21};
double english[] = {43,53,64,23};
Student3 student[] = new Student3[4];
for(int i = 0;i<student.length;i++){
student[i] = new Student3(str[i],math[i],english[i]);
}
StudentKey key[] = new StudentKey[4];
for(int i = 0;i<key.length;i++){
key[i] = new StudentKey(student[i].math);
}
for(int i = 0;i<student.length;i++){
map.put(key[i], student[i]);
}
int number = map.size();
System.out.println("树映射中有"+number+"个对象,按数学成绩排序:");
Collection<Student3> collection = map.values();
Iterator<Student3> it = collection.iterator();
while(it.hasNext()){
Student3 stu = it.next();
System.out.println("姓名"+stu.name+"数学"+stu.math);
}
map.clear();
for(int i = 0;i<key.length;i++){
key[i] = new StudentKey(student[i].english);
}
for(int i =0;i<student.length;i++){
map.put(key[i], student[i]);
}
number = map.size();
System.out.println("树映射中有"+number+"个对象:按英语成绩排序:");
Collection<Student3> collection2 = map.values();
Iterator<Student3> it2 = collection2.iterator();
while(it2.hasNext()){
Student3 stu = it2.next();
System.out.println("姓名:"+stu.name+"英语"+stu.english);
}
}
}
class StudentKey implements Comparable{
double d = 0;
public StudentKey(double d){
this.d =d ;
}
public int compareTo(Object b) {
StudentKey st = (StudentKey) b;
if((this.d-st.d)==0){
return -1;
}else{
return (int) ((this.d-st.d)*1000);
}
}
}
class Student3{
String name;
double math,english;
public Student3(String name,double math,double english){
this.english = english;
this.name = name;
this.math = math;
}
}
相关文章推荐
- java数据结构之树集TreeSet与书映射(TreeMap)
- Java容器-引用数据类型排序+TreeSet、TreeMap底层实现
- 2 个按升序(从小到大)排列的数列 A[1],A[2],....,A[M]和 B[1],B[2],...,B[N]中各元素按其大小关系存放到数组 C。同时必须注意对数组 A 或 B 残余元素的追加处理。(数组 A、B 中初始元素数量自定,数据从键盘输入。)
- java数据结构之树集TreeSet与书映射(TreeMap)
- java数据结构之树集TreeSet与书映射(TreeMap)
- 给定一堆整数数据,你的工作是:执行尽可能少的删除操作,并使剩下的数据以升序排列(只做删除操作,不作排序)
- 数据结构:单链表(二)之链表节点排序,升序插入数据,删除指定的所有节点,翻转链表操作
- java List Collections.sort 排序 支持 对象属性 普通数据类型 升序 降序 正序 倒序 排列
- 《java入门第一季》之TreeSet存储自定义对象并保证排序和唯一
- kafka解决数据同步,保证排序
- 数据结构中线性表的基本操作-合并两个线性表-按照元素升序排列
- 【数据结构】将一组数据升序排序(利用堆排序)
- elementui tree查询数据怎样显示当前节点的所有子节点
- 模型数据升序排列(快排)
- 冒泡排序示例,按时间升序排列数据
- easyui-tree自动勾选指定的节点checkbox(回绑数据)
- ext TreePanel 一次递归加载数据及异步加载数据(点子节点加载数据)
- TreeMap<K,V> 映射的使用 TreeMap 排序
- java treeMap 排序 treeset同理
- C语随机产生20个正整数存入数组a中,且每个数均在1000-9999之间(包含1000和9999)。对数组进行排序,要求按每个数的后三位的大小进行升序排列,然后取出满足此条件的前10个数放入数组b中,