1、创建学生类
package com.jyd.demo;
/**
*
* 类名: Student
* 备注: 学生类
* 时间: 2017年4月8日 下午2:52:49
* @version
* @since JDK 1.8
*/
public class Student implements Comparable<Student> {
private int id;
private String name;
private String sex;
private int age;
private int score;
/**
*
* Creates a new instance of Student.
*
* @param id
* @param name
* @param sex
* @param age
* @param score
*/
public Student(int id, String name, String sex, int age, int score) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.score = score;
}
/**
*
* TODO 简单描述该方法的实现功能(可选).
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(Student o) {
if(this.score>o.score){
return 1;
}else if(this.score==o.score){
return 0;
}else{
return -1;
}
}
/**
*
* TODO 简单描述该方法的实现功能(可选).
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.name+"的分数为:"+this.score;
}
/**
*
* 方法名:getId
* 备注:
* 时间:2017年4月8日下午2:53:55
* @return
* @since JDK 1.8
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
2、ArrayList、TreeSet、TreeMap、HashSet和HashMap的使用
package com.jyd.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.TreeSet;
/**
*
* 类名:StudentDemo
* 备注: 练习集合
* 时间: 2017年4月8日 下午3:11:37
* @version
* @since JDK 1.8
*/
public class StudentDemo{
public static void main(String[] args) {
Student s2 = new Student(2, "欧阳锋", "男", 24, 99);
Student s3 = new Student(3, "风清扬", "男", 24, 98);
//ArrayList存储 加入的顺序
ArrayList<Student> list = new ArrayList<Student>();
list.add(s1);
list.add(s2);
list.add(s3);
Iterator<Student> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("=================");
//HashMap存储
HashMap<String,Student> map = new HashMap<String,Student>();
map.put(s1.getName(), s1);
map.put(s2.getName(), s2);
map.put(s3.getName(), s3);
System.out.println("=================");
//TreeMap存储
TreeMap<String,Student> tm = new TreeMap<String,Student>();
tm.put(s1.getName(), s1);
tm.put(s2.getName(), s2);
System.out.println(tm.get("欧阳锋"));
System.out.println("=================");
//TreeSet存储 升序排列 Student实现了Comparable接口,也可以倒序排,或根据其它属性进行排序
TreeSet<Student> treeSet = new TreeSet<Student>();
treeSet.add(s3);
treeSet.add(s2);
treeSet.add(s1);
Iterator<Student> it1 = treeSet.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
System.out.println("=================");
//HashSet存储
HashSet<Student> hashSet = new HashSet<Student>();
hashSet.add(s3);
hashSet.add(s2);
hashSet.add(s1);
Iterator<Student> it2 = hashSet.iterator();
while(it2.hasNext()){
System.out.println(it2.next());
}
}
}