Skip to content

Instantly share code, notes, and snippets.

@HabaCo
Created June 16, 2014 11:26
Show Gist options
  • Save HabaCo/84b0a81ecbd00284698e to your computer and use it in GitHub Desktop.
Save HabaCo/84b0a81ecbd00284698e to your computer and use it in GitHub Desktop.
Student 類別, HashSet 找值, TreeSet 排序找值, LinkedList 排序找值, ArrayList 排序找值
package Test01;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class StudentArrayList {
public static void main(String[] args) throws IOException {
String FileName = "test0616.txt";
FileReader fr = new FileReader(FileName);
BufferedReader br = new BufferedReader(fr);
ArrayList<Student> al = new ArrayList<Student>();
String inputStr;
while ((inputStr = br.readLine())!=null){
String[] tmp = inputStr.split(",");
Student stu = new Student(Integer.parseInt(tmp[0]), tmp[1], Integer.parseInt(tmp[2]), Float.parseFloat(tmp[3]));
al.add(stu);
}
// 使用覆載 comparable 的 compareTo
Collections.sort(al);
// 使用覆載 comparator 的 compare
Collections.sort(al, new Student()); // Student implements comparator
System.out.println("ArrayListList:");
for (Object obj : al.toArray()){
Student s=(Student)obj;
System.out.printf("號碼:%-8d 姓名:%-8s 身高:%-4d 體重:%-3.2f\n",s.getNum(), s.getName(), s.getHeight(), s.getWeight());
}
System.out.println("身高最高:");
System.out.println("姓名: " + al.get(al.size()-1).getName() + " 身高: " + al.get(al.size()-1).getHeight());
System.out.println("身高最矮: ");
System.out.println("姓名: " + al.get(0).getName() + " 身高: " + al.get(0).getHeight());
}
}
package Test01;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
public class StudentHashSet {
public static void main(String[] args) throws IOException{
String FileName = "test0616.txt";
FileReader fr = new FileReader(FileName);
BufferedReader br = new BufferedReader(fr);
HashSet<Student> hs = new HashSet<Student>();
String inputStr;
while ((inputStr=br.readLine())!=null){
String[] tmp = inputStr.split(",");
Student stu = new Student(Integer.parseInt(tmp[0]), tmp[1], Integer.parseInt(tmp[2]), Float.parseFloat(tmp[3]));
// HashSet 本身有無序的特性
hs.add(stu);
}
// 利用 Iterator 將 HashSet 裡的資料一個一個讀出來
Iterator<Student> it1 = hs.iterator();
Student highSt = getHighest(it1);
// 讀取 Iterator 不可倒退,且讀完即失效,所以使用了第二個來存取最小值
Iterator<Student> it2 = hs.iterator();
Student shortSt = getShortest(it2);
System.out.println("HashSet:");
for (Object obj : hs.toArray()){
Student s=(Student)obj;
System.out.printf("號碼:%-8d 姓名:%-8s 身高:%-4d 體重:%-3.2f\n",s.getNum(), s.getName(), s.getHeight(), s.getWeight());
}
System.out.println("身高最高:");
System.out.println("姓名: " + highSt.getName() + " 身高: " + highSt.getHeight());
System.out.println("身高最矮: ");
System.out.println("姓名: " + shortSt.getName() + " 身高: " + shortSt.getHeight());
}
// 循序比對每一個人的身高
public static Student getHighest(Iterator<Student> it){
Student st = it.next();
while(it.hasNext()){
Student stmp=it.next();
if (st.getHeight() < stmp.getHeight())
st = stmp;
}
return st; // 回傳身高最高的人
}
// .. 同上
public static Student getShortest(Iterator<Student> it){
Student st = it.next();
while(it.hasNext()){
Student stmp=it.next();
if (st.getHeight() > stmp.getHeight())
st = stmp;
}
return st; // 回傳身高最高的人
}
}
package Test01;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
public class StudentLinkedList {
public static void main(String[] args) throws IOException {
String FileName = "test0616.txt";
FileReader fr = new FileReader(FileName);
BufferedReader br = new BufferedReader(fr);
LinkedList<Student> ls = new LinkedList<Student>();
String inputStr;
while ((inputStr = br.readLine())!=null){
String[] tmp = inputStr.split(",");
Student stu = new Student(Integer.parseInt(tmp[0]), tmp[1], Integer.parseInt(tmp[2]), Float.parseFloat(tmp[3]));
ls.add(stu);
}
// 使用覆載 comparable 的 compareTo
Collections.sort(ls);
// 使用覆載 comparator 的 compare
Collections.sort(ls, new Student());
System.out.println("LinkedList:");
for (Object obj : ls.toArray()){
Student s=(Student)obj;
System.out.printf("號碼:%-8d 姓名:%-8s 身高:%-4d 體重:%-3.2f\n",s.getNum(), s.getName(), s.getHeight(), s.getWeight());
}
System.out.println("身高最高:");
System.out.println("姓名: " + ls.getLast().getName() + " 身高: " + ls.getLast().getHeight());
System.out.println("身高最矮: ");
System.out.println("姓名: " + ls.getFirst().getName() + " 身高: " + ls.getFirst().getHeight());
}
}
package Test01;
import java.util.Comparator;
public class Student implements Comparable<Student>, Comparator<Student>{
private int num;
private String name;
private int height;
private float weight;
public Student(){
// nothing
}
// overloading constructor
public Student(int n, String na, int h, float w){
setNum(n);
setName(na);
setHeight(h);
setWeight(w);
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
@Override
public int compareTo(Student st) {
// 若回傳值<0,新的 Element 會加在後方,因此透過比較決定擺放位置
// eg. 小至大
return this.height-st.getHeight();
}
@Override
public int compare(Student s1, Student s2) {
// 反之則是大至小
return -s1.compareTo(s2);
}
}
package Test01;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.TreeSet;
public class StudentTreeSet {
public static void main(String[] args) throws IOException {
String FileName = "test0616.txt";
FileReader fr = new FileReader(FileName);
BufferedReader br = new BufferedReader(fr);
// TreeSet 為實作 SortedSet(interface) 的類別,預設有基本資料型別的排序方法
// 但未知型別物件的排序需要自行定義,使Student覆寫compareTo方法讓TreeSet集合能夠使用
TreeSet<Student> ts = new TreeSet<Student>();
// // 透過 TreeSet 建構子指定使用 compare 方法 ----- TreeSet建構子會尋找類別裡的compare(obj1,obj2)方法
// TreeSet<Student> ts = new TreeSet<Student>(new Student());
String inputStr;
while ((inputStr=br.readLine())!=null){
String[] tmp = inputStr.split(",");
Student stu = new Student(Integer.parseInt(tmp[0]), tmp[1], Integer.parseInt(tmp[2]), Float.parseFloat(tmp[3]));
// TreeSet 在加入時會參照 compareTo 方法排序
ts.add(stu);
}
System.out.println("TreeSet:");
for (Object obj : ts.toArray()){
Student s=(Student)obj;
System.out.printf("號碼:%-8d 姓名:%-8s 身高:%-4d 體重:%-3.2f\n",s.getNum(), s.getName(), s.getHeight(), s.getWeight());
}
System.out.println("身高最高:");
System.out.println("Name: "+((Student)ts.last()).getName() + " Height: "+((Student)ts.last()).getHeight());
System.out.println("身高最矮:");
System.out.println("Name: "+((Student)ts.first()).getName() + " Height: "+((Student)ts.first()).getHeight());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment