概述
TreeMap类是SortedMap的唯一实现,基于红黑树数据结构,它根据键的自然顺序或者创建映射的Comparator进行排序
/**
* @author yixiao Han
* @data:2018 10 29
* @function: test The java Map coding
*/
package Treemap;
import java.util.*;
class Student {
public int id;
public String name;
public int math_score;
public int english_score;
public int computer_score;
public Student(int id, String name, int math_score, int english_score, int computer_score) {
this.id = id;
this.name = name;
this.math_score = math_score;
this.english_score = english_score;
this.computer_score = computer_score;
}
}
public class MapTest {
public static void main(String[] args) {
TreeMap<Integer, Student> map = new TreeMap<>();// public class TreeMap<K,V>
/**
* param <K> the type of keys maintained by this map ‘map的键值
* @param <V> the type of mapped values’映射的类型
* 这句的大致意思为一个整型的数字与一个STudent类型相对应
* 后面的new<> TreeMap则是泛型的声明方法
* Generics<String> gen=new Generics<>()
*/
int total = 0;
int[] grade = new int[5];
Student[] s = new Student[5];
s[0] = new Student(01, "季安", 85, 75, 95);
s[1] = new Student(02, "老司机", 100, 92, 60);
s[2] = new Student(03, "yixiao", 92, 50, 100);
s[3] = new Student(04, "李彦龙", 80, 50, 10);
s[4] = new Student(05, "宫新港", 70, 80, 82);
for (int i = 0; i < 5; i++) {
map.put(i + 1, s[i]);
/*
* map.put
* public V put(K key, V value) {
Entry<K,V> t = root;
if (t == null) {
compare(key, key); // type (and possibly null) check
root = new Entry<>(key, value, null);
size = 1;
modCount++;
return null;
}
往map的表中填入数据 对应的K为键值,S[i]为学生
*/
}
int[] arr = new int[5];
int i = 0;
int j = Integer.parseInt(args[0]);//获得配置的主函数参数 j=01
System.out.println("student number name computer_score english_score math_score ");
if (map.containsKey(j)) {
/**
* map containsKey(Object key)
* 如果此映射包含指定键的映射关系,则返回 true。
*/
System.out.println("what information you are looking for:");
Student student = map.get(j);
System.out.print(student.id + " ");
System.out.print(student.name + " ");
System.out.print(student.computer_score + " ");
System.out.print(student.english_score + " ");
System.out.print(student.math_score + " ");
total = student.computer_score + student.english_score + student.math_score;// total score
System.out.print(total);
}
else{
System.out.println("this student is not in our school,please kill the program");
}
System.out.println();
TreeMap<Integer, Student> tp = new TreeMap();
/**
* 声明一个新的TreeMap tp
* 用来保存新的键值和对应的映射类型
* 重新排序过的内容
* 基于红黑树(Red-Black tree)的 NavigableMap 实现。该映射根据其键的自然顺序进行排序,
* 或者根据创建映射时提供的 Comparator 进行排序,具体取决于使用的构造方法。
* 即数字从 1 到 9 的排序方法,字母从 a 到 z 的排序方法,短者优先。数组的索引与单元值保持关联。.
*/
Iterator<Map.Entry<Integer, Student>> it = map.entrySet().iterator();
/*
* @Iterator:迭代器,collection接口的iterator和toArry都可以用来遍历容器中的所有元素
* 前者返回一个Iterator的对象,后者返回一个带有所有元素的数组
* hasNext函数 判断是否遍历完容器中的所有对象
* @map.Entry public static interface Map.Entry<K,V>
* entrySet().iterator()
* 该种方式看起来冗余却有其优点所在。首先,在老版本java中这是惟一遍历map的方式。
* 另一个好处是,你可以在遍历时调用iterator.remove()来删除entries,另两个方法则不能
* 。根据javadoc的说明,如果在for-each遍历中尝试使用此方法,结果是不可预测的。
*/
System.out.println("before the sort");
while (it.hasNext()) {
/**
* hasNext()函数返回boolean形变量
*/
Map.Entry entry = it.next();
/**
* 容器的入口:用来存储迭代出的元素
*
*/
arr[i] = (Integer) entry.getKey();//
/**
* 返回与此项对应的键,这里为参数为啊s[i]的键值
*/
s[i] = (Student) entry.getValue();
/**
* 返回与此项对应的值。
*/
System.out.println(s[i].id + " ");
System.out.println(s[i].name + " ");
System.out.println(s[i].computer_score + " ");
System.out.println(s[i].english_score + " ");
System.out.println(s[i].math_score + " ");
total = s[i].computer_score + s[i].english_score + s[i].math_score;
System.out.println(total);
System.out.println();
grade[i] = total;
tp.put(grade[i], s[i]);
/**
* put(K key, V value)
* 将指定值与此映射中的指定键进行关联。
* 新的map是由总成绩为键值构成的Map
* 所以新的map tp是由分数自然排序 的
*/
i++;
System.out.print(i+"个n");
//System.out.print(arr[i]);不可以这样使用
}
i = 0;
System.out.println("after sort");
Iterator<Map.Entry<Integer, Student>> iter = tp.entrySet().iterator();
/**
* 返回Iterator迭代器,用来遍历map中的所有元素
*/
while (iter.hasNext()) {
Map.Entry entry1 = iter.next();
arr[i] = (Integer) entry1.getKey();//
s[i] = (Student) entry1.getValue();
System.out.println(s[i].id + " ");
System.out.println(s[i].name + " ");
System.out.println(s[i].computer_score + " ");
System.out.println(s[i].english_score + " ");
System.out.println(s[i].math_score + " ");
total = s[i].computer_score + s[i].english_score + s[i].math_score;
System.out.println(total);
System.out.println();
}
}
}
最后
以上就是安详钢铁侠为你收集整理的java 容器和泛型之TreeMap的全部内容,希望文章能够帮你解决java 容器和泛型之TreeMap所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复