通過一些實例 學Java

1.書店管理實例(TreeMap樹狀映射表)

在Map中如果經常插入、刪除和定位元素,最好用HashMap; 如果要經常遍歷鍵,那麼TreeMap會更好。實際應用中會根據集合大小,先把元素添加到HashMap再把這種映射轉換成一個用於有序遍歷鍵的TreeMap。

TreeMap,基於紅黑樹,是SortedMap的實現類。該映射根據其鍵的自然順序進行排序,或者根據創建映射時提供的Comparator進行排序

package ch7;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;


public class TestTreeMap {

public static void main(String[] args)
{
TreeMap<String,Integer> tm=new TreeMap<String,Integer>();//創建treemap對象, key和value的數據類型分別爲字符串和整型
tm.put("java", 10);

tm.put("C++",8);
tm.put("VC", 25);
tm.put("VB", 30);//添加鍵--值

Set<String> set=tm.keySet();// keySet()返回此映射中所包含的鍵的Set視圖


Iterator<String> it=set.iterator();//迭代器

while(it.hasNext())
{
String key=it.next();
Integer temp=tm.get(key);// get(key)返回指定鍵所映射的值,如果不包含任何映射關係,返回null
System.out.println(key+"書還有"+temp+"本");

}

String key="java";
if(tm.containsKey(key)){         //containsKey(key)如果key含有映射關係,則返回true
System.out.println("經查詢"+key+"書還有"+tm.get(key)+"本");
}else{
System.out.println("經查詢"+key+"這本書沒有了");



     tm.remove("VB");//刪除key
     System.out.println("將VB書下架");
     System.out.println("重新統計,本店圖書"+tm.size()+"種");//size()返回映射對數
}}

程序結果:

C++書還有8本
VB書還有30本
VC書還有25本
java書還有10本
經查詢java書還有10本
將VB書下架
重新統計,本店圖書3種


2.學生考試實例(HashMap散列映射表)

package ch7;
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;


public class TestHashMap {
public static void main(String[] args){


 HashMap<String,String> t1=new HashMap<String,String>();//構造默認容量爲16和默認加載因子0.75的空HashMap
 HashMap<String,String> t2=new HashMap<String,String>(32);
 HashMap<String,String> t3=new HashMap<String,String>(32,(float)0.85);// 構造一個帶指定初始容量和加載因子的空HashMap
 
 t1.put("zhang","java");
 t1.put("li","C++");
 t1.put("lai","java");
 t1.put("ma","C++");
 
 Set<String> set=t1.keySet();
 Iterator<String> it=set.iterator();


 while(it.hasNext()){
String key=it.next();
String temp=t1.get(key);
System.out.println("恭喜"+key+"通過"+temp+"考試");
 
 }
 String key="zhang";
 if(t1.containsKey(key)){
System.out.println("恭喜"+key+"通過"+t1.get(key)+"考試");
 }else
 {
System.out.println("很遺憾沒有通過考試");
 }
t1.remove("zhang");
System.out.println("刪除zhang同學");
System.out.println("目前的學生數目"+t1.size());
 
}}

程序結果爲:

恭喜ma通過C++考試
恭喜zhang通過java考試
恭喜zhang通過java考試
刪除zhang同學
目前的學生數目3


3.LinkedList(底層爲雙向循環鏈表)

package ch7;


import java.util.LinkedList;
import java.util.ListIterator;//是Iterator的子類


public class Student {
double height;
String name;
public Student(){

}
public Student(double height,String name){
this.height=height;
this.name=name;
}
@Override
public String toString()
{
return  name+","+"身高是"+height+"米";
}


public static void main(String[] args){
LinkedList<Student> list1=new LinkedList<Student>();
Student s1=new Student(1.60,"lily");
Student s2=new Student(1.80,"john");
Student s3=new Student(1.75,"adom");
Student s4=new Student(1.83,"kaka");
Student s5=new Student(1.63,"andy");
Student s6=new Student(1.70,"amy");
list1.add(s1);
list1.add(s2);
list1.add(s3);
list1.add(s4);
list1.addFirst(s5);
list1.addLast(s6);

for(int i=0;i<list1.size();i++) {
System.out.println("第"+(i+1)+"位同學的名字是"+list1.get(i));//括號不能省
}
list1.remove(2);
list1.remove(s3);
list1.add(2,s3);
list1.add(3,s2);

System.out.println("*********");
ListIterator<Student> it=list1.listIterator(list1.size());
while(it.hasPrevious()){
System.out.println(it.previous());
}
}
}

實驗結果:

第1位同學的名字是5,身高是1.63米
第2位同學的名字是1,身高是1.6米
第3位同學的名字是2,身高是1.8米
第4位同學的名字是3,身高是1.75米
第5位同學的名字是4,身高是1.83米
第6位同學的名字是6,身高是1.7米
*********
6,身高是1.7米
4,身高是1.83米
2,身高是1.8米
3,身高是1.75米
1,身高是1.6米
5,身高是1.63米


4.實現Comparable接口

package ch7;


public class AutoStudent implements Comparable {
private String name;
private int height;
public AutoStudent(){

}

public AutoStudent(String name,int height){
this.name=name;
this.height=height;
}

public double getHeight(){
return height;
}


public String getName(){
return name;
}

public void setHeight(int height){
this.height=height;
}

public void setName(String name){
this.name=name;
}
@Override
public String toString(){
return name+"同學的身高是"+height+"釐米";
}

@Override
public boolean equals(Object obj){
if(obj instanceof AutoStudent){
AutoStudent ast=(AutoStudent)obj;
if(ast.name.equals(this.name)&&ast.height==this.height)
{
return true;
}
}
return false;
}

@Override
public int hashCode(
){
return name.hashCode()^height;

}

@Override
public int compareTo(Object obj)
{
AutoStudent ast=(AutoStudent)obj;

if(ast.height==this.getHeight())
{
return this.name.compareTo(ast.name);//name的hashcode值改寫了,返回Int
}else{
return this.height-ast.height;
}
}
}



package ch7;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.SortedSet;


public class TestSetTree {


public static void main(String [] args){
SortedSet set=new TreeSet();
AutoStudent s1=new AutoStudent("lily",180);
AutoStudent s2=new AutoStudent("amy",160);
AutoStudent s3=new AutoStudent("deeny",190);
AutoStudent s4=new AutoStudent("niko",165);
AutoStudent s5=new AutoStudent("kaky",170);
AutoStudent s6=new AutoStudent("lisay",180);

set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
set.add(s6);

Iterator it=set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}

結果

amy同學的身高是160釐米
niko同學的身高是165釐米
kaky同學的身高是170釐米
lily同學的身高是180釐米
lisay同學的身高是180釐米
deeny同學的身高是190釐米

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章