Commit 0f3e6077 by 陈洁

陈洁

parent e893c4d9
package cn.freemud.demo;
import java.util.List;
public class ListDemo {
static final int N=50000;
static long timeList(List list){
long start=System.currentTimeMillis();
Object obj=new Object();
for (int i=0;i<N;i++){
list.add(0,obj);
}
return System.currentTimeMillis()-start;
}
static long readList(List list){
long start=System.currentTimeMillis();
for (int i=0,j=list.size();i<j;i++){
}
return System.currentTimeMillis()-start;
}
static List addList(List list){
Object o=new Object();
for (int i=0;i<N;i++){
list.add(0,o);
}
return list;
}
}
package cn.freemud.demo;
import java.awt.*;
import java.util.*;
import java.util.List;
import static cn.freemud.demo.ListDemo.*;
import static cn.freemud.demo.TestMap.testMap;
public class TestCJ {
static class Person {
private String name;
private int age;
Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
System.out.println("hashCode:" + this.name);
return this.name.hashCode() + age;
}
@Override
public boolean equals(Object obj) {
System.out.println(this + "---equals---'+obj");
if (obj instanceof Person) {
Person p = (Person) obj;
return this.name.equals(p.name) && this.age == p.age;
} else {
return false;
}
}
@Override
public String toString() {
return "Person@name:" + this.name + " ;age:" + this.age;
}
}
public static void main(String[] args) {
testSet();
testHashSet();
//HashMap测试
testMap(N);
//list 测试
System.out.println("ArrayList添加"+N+"条耗时:"+timeList(new ArrayList()));
System.out.println("LinkedLsit添加"+N+"条耗时:"+timeList(new LinkedList()));
List list1=addList(new ArrayList());
List list2=addList(new LinkedList());
System.out.println("ArrayList查找"+N+"条耗时:"+readList(list1));
System.out.println("LinkedList查找"+N+"条耗时:"+timeList(list2));
//用thread类的子类创建线程
InheritThread itd=new InheritThread();
//用Runnable接口类的对象创建线程
Thread rtd=new Thread(new RunnableThread());
itd.setPriority(5);
rtd.setPriority(4);
itd.start();
rtd.start();
}
public static void testSet() {
//Set 集合存取顺序不一致
Set hs = new HashSet();
hs.add("第一个");
hs.add("第二个");
hs.add("第三个");
System.out.println("Set中数据:" + hs);
System.out.println("Set集合元素数量:" + hs.size());
boolean add = hs.add("第三个");
System.out.println("重复的‘第三个’添加结果:" + add);
System.out.println("hs的数量:" + hs.size());
Iterator it = hs.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
public static void testHashSet() {
HashSet hs = new HashSet();
hs.add(new Person("Jack", 20));
hs.add(new Person("Lili", 21));
hs.add(new Person("Mike", 22));
hs.add(new Person("Jack", 20));
hs.add(new Person("Jack", 23));
Iterator it = hs.iterator();
while (it.hasNext()) {
Object next = it.next();
System.out.println(next);
}
}
}
package cn.freemud.demo;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
public class TestMap {
static int hashMapW = 0;
static int hashMapR = 0;
static int linkMapW = 0;
static int linkMapR = 0;
static int treeMapW = 0;
static int treeMapR = 0;
static int hashTableW = 0;
static int hashTableR = 0;
public static void testMap(int size){
int index;
Random random=new Random();
String[] key=new String[size];
//HashMap插入
Map<String,String> map=new HashMap<String, String>();
long start=System.currentTimeMillis();
for (int i=0;i<size;i++){
key[i]= UUID.randomUUID().toString();
map.put(key[i],UUID.randomUUID().toString());
}
long end=System.currentTimeMillis();
hashMapW+=(end-start);
System.out.println("HashMap插入耗时 = " + (end - start) + " ms");
//HashMap读取
start=System.currentTimeMillis();
for (int i=0;i<size;i++){
index=random.nextInt(size);
map.get(key[index]);
}
end=System.currentTimeMillis();
hashMapR+=(end-start);
System.out.println("HashMap读取耗时 = " + (end - start) + " ms");
}
}
package cn.freemud.demo;
class InheritThread extends Thread {
InheritThread() {
}
InheritThread(String name) {
super(name);
}
public void run() {
System.out.println("ThreadDemo is running......");
for(int i=0;i<10;i++){
System.out.println("ThreadDemo:i="+i);
try{
Thread.sleep((int) Math.random()*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class RunnableThread implements Runnable{
public void run(){
System.out.println("RunnableThread is running....");
for(int i=0;i<10;i++){
System.out.println(" RunnableThread;i="+i);
try {
Thread.sleep((int) Math.random()*1000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment