java之容器


先來一張容器的API框架圖,我們在java中所學的所有知識,都是根據下面這張圖來學習的....

容器API:

  1、Collection接口------定義了存儲一組對象的方法,其子接口Set和List分別定義了存儲的方式。

    ①、Set中的數據對象沒有順序且不可以重復。

    ②、List中的數據對象有順序且可以重復。

  2、Map接口定義了存儲“鍵(key)---值(value)映射對”的方法。

Collection接口:

  Collection接口中定義的方法(意思就是只要你實現了Collection接口,你將擁有下面所有方法):

    

Collection方法舉例:

這里要說明的就是集合里面只能裝引用類型的數據。

import java.util.*;
public class TestCollection{
    public static void main (String args[]){
        Collection collection = new ArrayList();
        //可以放入不同類型的對象
        collection.add("hello");
        collection.add(new Person("f1",18));
        collection.add(new Integer(100));
        System.out.println(collection.size());
        System.out.println(collection);
    }
}
class Person{
    private String name; 
    private int age;
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
}

接下來,我們繼續使用上面的例子,說說Collection里面remove()方法的使用:

import java.util.*;
public class TestCollection{
    public static void main (String args[]){
        Collection collection = new HashSet();
        //可以放入不同類型的對象
        collection.add("hello");
        collection.add(new Person("f1",18));
        collection.add(new Integer(100));
        
        collection.remove("hello");
        collection.remove(new Integer(100));
        
        System.out.println(collection.remove(new Person("f1",18)));
        System.out.println(collection);
    }
}
class Person{
    private String name; 
    private int age;
    public Person(String name,int age){    
        this.name = name;
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    /*public boolean equals(Object obj){
        if(obj instanceof Person){
            Person person = (Person)obj;
            return (name.equals(person.name) && age == person.age);
        }
        return super.equals(obj);
    }
    public int hashCode(){
        return name.hashCode();
    }*/
}

執行上面的例子,你會發現我們插入的數據”hello“和new Integer(100)都可以用remove()方法直接刪除,但是對於new person("f1",18)這對象可以用remove()方法直接刪除嗎?答案是不可以的....

容器類對象在調用remove、contains等方法時需要比較對象是否相等,這會涉及到對象類型的equals方法和hashCode方法;對於自定義的類型,需要重寫equals方法和hashCode方法以實現自定義對象相等規則。

  注意,相等的對象應該具有相等的hash Codes

 

Ieterator接口(簡單說:Iterator就是一個統一的遍歷我們集合中所有元素的方法)

  1、所有實現了Collection接口的容器類都有一個iterator方法用以返回一個實現了Iterator接口的對象。

  2、Iterator對象稱作迭代器,用以方便的實現對容器元素的遍歷實現。

  3、Iterator實現了下列方法:

下面我們寫一個用Iterator遍歷集合元素的方法。(注:程序運行信息輸出順序可能跟我們輸入的順序不一致,這就是Set集合無序的效果)

import java.util.*;
public class TestCollection{
    public static void main (String args[]){
        Collection collection = new HashSet();
        
        collection.add(new Person("zhang",1));
        collection.add(new Person("gao",2));
        collection.add(new Person("wang",3));
        collection.add(new Person("du",4));
        collection.add(new Person("liang",5));
        collection.add(new Person("li",6));
        
        Iterator iterator = collection.iterator();
        while(iterator.hasNext()){
            //next()的返回值類型是Object類型,需要轉換為相應類型
            Person person = (Person)iterator.next();
            System.out.println(person.name);
        }
    }
}
class Person{
    public String name; 
    private int age;
    public Person(String name,int age){    
        this.name = name;
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
}

Set接口

  1、Set接口是Collection的子接口,Set接口沒有提供的額外方法,但實現Set接口的容器類中的元素是沒有順序的,而且不可以重復

  2、Set接口可以與數學中”集合“的概念相對應。

  3、J2SDK API中所提供的容器類有HashSet、TreeSet等...

Set方法舉例:

 Set方法舉例:

List接口:

  1、List接口是Collection的子接口,實現List接口的容器類中元素是有順序的,而且可以重復。

  2、List容器中元素都對應一個整數型的序號記載其在內容中的位置,可以根據序號存取容器中的元素。

  3、L2SDK所提供的List容器類有ArrayList,LinkedList等...

List  方法舉例:

  List常用算法:

List常用算法舉例:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM