您的位置:首页 > 编程语言 > Java开发

通过一些实例 学Java

2016-04-19 19:51 309 查看
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();// [b]keySet()返回此映射中所包含的键的Set视图[/b]

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 he
b0a7
ight){
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厘米
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: