Collection note

来源:互联网 发布:linux禁止ping ip地址 编辑:程序博客网 时间:2024/05/31 00:40

ArrayList底层采用数组

 

LinkedList

底层采用双向列表

 

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

LinkedList实现的stack(栈)实例

import java.util.*;

class MyStack {


     private LinkedList ll=new LinkedList();


     public void push(object o){
          ll.addFirst();
     }
 
     public Object pop(){
          return ll.removeFirst();
     }
 
     public Object peek(){
         return  ll.getFirst();
     }

 

     public boolean empty(){
        return  ll.isEmpty();
     }

 

     public static void main(String[] args){

        Mystack myStack=new Mystack();

        mystack.push("one");

        mystack.push("two");

        mystack.push("three");

 

       System.out.println(mystack.pop());

       System.out.println(mystack.peek());

       System.out.println(mystack.pop());

       System.out.println(mystack.empty());

   }


}

 

运行结果:

three

two

two

false

 

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

LinkedList实现的Queue(队列)实例

import java.util.*;

class MyQueue {


     private LinkedList ll=new LinkedList();


     public void put(object o){
          ll.addLast();
     }
 
     public Object get(){
          return ll.removeFirst();
     }

 

     public boolean empty(){
        return  ll.isEmpty();
     }

 

     public static void main(String[] args){

        MyQueue myQueue=new MyQueue();

        myQueue.put("one");

        myQueue.put("two");

        myQueue.put("three");

 

       System.out.println(put.get());

       System.out.println(put.get());

       System.out.println(put.get());

       System.out.println(put.empty());

   }


}

 

运行结果:

one

two

three

true

 

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  

HashSet

实现了Set接口的Hash Table(哈希表),依靠HashMap来实现的。
应该为要存放到散列表的各个对象定义hashCode()equals()方法。

 

import java.util.*;
class HashSetTest{
 public static void main(String[] args){
  HashSet hs=new HashSet();
  hs.add("one");
  hs.add("two");
  hs.add("three");
  hs.add("one");     //Set中元素不能重复
  
  Iterator it=hs.iterator();  //HashSet中没有get方法,取元素只能通过Iterator
  while(it.hasNext()){
   System.out.println(it.next());
  }
 }

 

运行结果
one
two
three

 

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

import java.util.*;
class HashSetTest{
     public static void main(String[] args){
                HashSet hs=new HashSet();
                hs.add(new student(1,"zhangsan"));
                hs.add(new student(2,"lisi"));
                hs.add(new student(3,"wangwu"));
                hs.add(new student(1,"zhangsan"));
  
                Iterator it=hs.iterator();  
                while(it.hasNext()){
                        System.out.println(it.next());
                }
 }

 

class Student{

    int num;

    String name;

    Student(int num, String name){

           this.num=num;

           this.name=name;

    }

 

    public String toString(){

           return num + ":" + name;

    }

 

}

 

运行结果
1:zhangsan
3:wangwu
1:zhangsan
2:lisi

 

为什么会产生重复的元素呢?

存储对象时,是根据对象的散列码来计算它的存储位置

而这个散列码是,通过Object的hashCode()方法通过内存地址得到散列码, 因此认为它们是不同的对象

所以在这里我们需要重写hashCode()方法.

仅仅重写hashCode()方法,结果依然有重复项,因为重写hashCode()的同时还要重写equals()方法

class Student{
    int num;
    String name;

 

    Student(int num, String name){
           this.num=num;
           this.name=name;
    }

 

    public int hashCode(){
           return num*name.hashCode();
    }


    public boolean equals(Object o){
           Student s= (Student) o;
           return num==s.num && name==s.name;
    }


    public String toString(){
           return num + ":" + name;
    }
}

 

 

运行结果:

1:zhangsan
3:wangwu
2:lisi

 

如果num能够唯一区分student对象,可直接写成
hashCode(){return num;}
无法区分时,可写成
hashCode(){return num*name.hashCode();}

 

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  HashMap

 

HashMap是对key进行散列

  1. 添加元素: put(K key, V value)
  2. 取得元素: get(Object key)

  3. 没有Iterator迭代器,但是采用get方法遍历所有元素效率是很低的,因此提供如下三个方法来间接进行迭代

            Set<Map.Entry<K,V>> entrySet()

            Set<K> keySet()

            Collection<V> values()


keySet(), values(), entrySet()
基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。

(除了不同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)此类不保证映射的顺序,特别是它不保证该顺序恒久不变。

 import java.util.*;
class HashMapTest{
    public static void main(String[] args){
        HashMap hashMap = new HashMap();
        hashMap.put("","zhangsan");
        hashMap.put("2","lisi");
        hashMap.put("3","wangwu");

 

        System.out.println(hashMap.get("")); 
    }
}

 

运行结果

zhangsan

 

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

HashMap的速度通常都比TreeSet快,只有在需要排序的功能的时候,才使用TreeMap
Vector:用ArrayList代替Vector
Hashtable: 用HashMap代替Hashtable。要格外小心是小写的t
Stack: 用LinkedList代替Stack。Stack继承自Vector
Properties: 继承自Hashtable


但是要使用多线程(线程安全的)是,可以使用Collections提供的
      static Collection syschronizedList(Collection c)
      static List syschronizedList(List list)
      static Map syschronizedList(Map map)
      static Set syschronizedList(Set set)

 

如果需要同步Vector比通过syschronizedList返回的List稍快一些。但Vector有很多遗留下来的操作。
因此用List接口操纵Vector时需格外小心

 

原创粉丝点击