天天看點

黑馬程式員----集合

——Java教育訓練、Android教育訓練、iOS教育訓練、.Net教育訓練、期待與您交流! ——-

集合

集合類:對象用于封裝特有資料,對象多了需要存儲;如果對象個數不确定就使用集合容器進行存儲

集合的特點:

1.用于存儲對象的容器

2.集合的長度是可變的

3.集合中不可以存儲基本資料類型值

集合容器因為内部的資料結構不同,有多種具體容器,在不斷的向上抽取中,就形成了集合架構。

數組和集合類同是容器,有何不同?

數組雖然可以存儲對象,但長度是固定的;集合長度是可變的。

數組中可以存儲基本資料類型,集合隻能存儲對象。

集合架構的構成及分類:

Collection接口

collection的常見方法:

1.添加:

boolean add(Object obj);

boolean addAll(Collection coll);

2.删除:

boolean remove(Object obj);

boolean removeAll(Collection coll);

void clear();

3.判斷:

boolean contains(Object obj);

boolean containsAll(Collection coll);

boolean isEmpty();

4.擷取:

int size()

Iterator iterator();

取出元素的方式:疊代器。

該對象必須依賴于具體容器,因為每一個容器的資料結構都不用,是以該疊代器對象是在容器中進行内部實作的,也就是iterator方法在每個容器中的實作方式都是不同的。

對于使用容器者而言,具體的實作不重要,隻要通過容器擷取到該實作的疊代器的對象即可,也就是iterator方法。

Iterator接口就是對所有的Collection容器進行元素的取出的公共接口。

5.其他:

boolean retainAll(Collection coll); 取交集

Objcet toArray(); 将集合轉成數組

public static void main(String[] args){
        Collection<String> coll  =new ArrayList<String>();
        coll.add("abc1");
        coll.add("abc2");
        coll.add("abc3");
        System.out.println("coll:"+coll);

        coll.remove("abc2");
        System.out.println("coll:"+coll);
        System.out.println("coll.contains(\"abc1\")="+coll.contains("abc1"));


        Collection<String> coll2 = new ArrayList<String>();
        coll2.add("abc1");
        coll2.add("abc6");
        coll2.add("abc7");
        System.out.println("coll2="+coll2);

        //将coll2中的元素添加到coll中
        coll.addAll(coll2);
        System.out.println("coll.addAll(coll2) = "+coll);

        //将coll集合中删除與coll2集合相同的元素
        coll.removeAll(coll2);
        System.out.println("coll.removeAll(coll2) = "+coll);

        boolean b1  = coll.containsAll(coll2);
        System.out.println("coll.containsAll(coll2) = "+b1);

        //取交集,保留兩個集合中相同的元素。
        coll.retainAll(coll2);
        System.out.println("coll、coll2交集:"+coll);
    }
           
黑馬程式員----集合

List、Set

Collection

|–List:有序(存入和取出的順序一緻),元素都有索引的角标,允許重複元素。

|–Set:元素不能重複,無序。

List:特有常見方法。

有一個共同特點就是可以操作角标。

1.添加

void add(index,element);

void addAll(index,collection);

2.删除

Object remove(index);

3修改

Object set(index,element);

4擷取

Object get(index);

int indexOf(objcet);

int lastIndexOf(objcet);

List subList(from,to);

List集合可以完成對元素的增删改查。

public static void show(){
        List<String> list = new ArrayList<String>();
        list.add("abc1");
        list.add("abc2");
        list.add("abc3");
        System.out.println("list = "+list);

        list.add(,"abcadd");
        System.out.println("list.add(1,\"abcadd\") ="+list);

        list.remove();
        System.out.println("list.remove(2)="+list);

        list.set(, "abcset");
        System.out.println("list.set(1,\"abcset\")="+list);

        System.out.println("list.get(0) = "+list.get());
        System.out.println("list.subList(1,2)"+list.subList(, ));
        System.out.println(list);
    }
           
黑馬程式員----集合

在疊代器過程中,不要使用集合操作元素,容易出現異常:

java.util.ConcurrentModificationException

可以使用Iterator接口的子接口ListIterator來完成在疊代中對元素進行更多的操作。

public static void iteratorlistDemo(){
        List<String> list = new ArrayList<String>();
        list.add("abc1");
        list.add("abc2");
        list.add("abc3");
        System.out.println("list ="+list);
        ListIterator<String> listIt = list.listIterator();
        while(listIt.hasNext()){
            String str = (String) listIt.next();
            if(str.equals("abc2")){
                listIt.add("abc0");
            }
        }
        System.out.println("hasNext:"+listIt.hasNext());
        System.out.println("hansPrevios:"+listIt.hasPrevious());

        while(listIt.hasPrevious()){
            System.out.println("prevoids :"+listIt.previous());
        }
        System.out.println("list :"+list);
        System.out.println("test list.iterator()--------------------------");
        Iterator<String> it = list.iterator();

        while(it.hasNext()){
            String str = (String) listIt.next();
            if(str.equals("abc2")){
                listIt.add("abc0");
            }
        }
    }
           
黑馬程式員----集合

vector、ArrayList\linkedList

List

|–Vector:内部是數組結構,是同步的,增删,查詢都很慢。

|–ArrayList:内部是數組資料結構,是不同步的,替代了Vector。查詢速度比Vector快

|–LinkedList:内部是連結清單資料結構,不同步,增删元素的速度快。

LinkedList方法:

addFirst();

addLast();

jdk1.6版本後新方法:

OfferFirst();與addFirst()方法沒有差別。

offerLast();與addLast方法沒有差別。

getFirst();擷取但不移除,如果連結清單尾空,抛出NoSuchElementException.

getLast();

jdk1.6版本後新方法:

peekFirst();//擷取但不移除,如果連結清單為空,傳回null。

peekLast();

removeFirst();//擷取并移除,如果連結清單尾空,抛出NoSuchElementException。

removeLast();

jdk1.6版本後新方法:

pollFirst();//擷取并移除,如果連結清單為空,傳回null;

pollLast();

public static void main(String[] args) {
        System.out.println("vector--------------------");
        Vector<String> v = new Vector<String>();
        v.addElement("abc1");
        v.add("abc2");
        v.add("abc3");
        v.addElement("abc4");

        Enumeration<String> en = v.elements();
        while(en.hasMoreElements()){
            System.out.println("nextElement:"+en.nextElement());
        }

        Iterator<String> it =v.iterator();
        while(it.hasNext()){
            System.out.println("next:"+it.next());
        }

        System.out.println("linklist-------------------------------");
        LinkedList<String> link = new LinkedList<String>();
        link.addFirst("abc1");
        link.offerFirst("abc2");
        link.offerLast("abc3");
        link.offer("abc4");
        Iterator<String> itLink = link.iterator();
        while(itLink.hasNext()){
            System.out.println("next:"+itLink.next());
        }
        System.out.println(link);
        System.out.println("getFirst:"+link.getFirst());
        System.out.println("getLast:"+link.getLast());
        System.out.println("removeFirst:"+link.removeFirst());
        System.out.println("removeLast:"+link.removeLast());
        while(!link.isEmpty()){
            System.out.println(link.removeFirst());
        }
    }
           
黑馬程式員----集合

Set

Set:元素不可以重複,是無序。

Set接口中的方法和collection一緻。

|–HashSet:内部資料結構是哈希表,是不同步的。

|–TreeSet:可以對set集合中的元素進行排序,是不同步的。

哈希表确定元素是否相同

1.判斷的是兩個元素的哈希值是否相同。

如果相同,再判斷兩個對象的内容是否相同。

2。判斷哈希值相同,其實判斷的是對象的hashCode方法。判斷内容相同,用的是equals方法。

往HashSet集合中存儲Person對象,如果姓名和年齡相同,視為同一個人,視為相同元素。

public class SetDemo {

    public static void main(String[] args) {
        HashSet<Person> hs = new HashSet<Person>();
        hs.add(new Person("list4", ));
        hs.add(new Person("forme", ));
        hs.add(new Person("set5", ));
        hs.add(new Person("forme", ));
        Iterator<Person> it = hs.iterator();
        while(it.hasNext()){
            Person p = (Person)it.next();
            System.out.println(p.getName()+"..."+p.getAge());
        }
    }

}

class Person{
    private String name;
    private int age;
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    public int hashCode(){
        return name.hashCode()+age*;
    }
    public boolean equals(Object obj){
        if(this == obj) return true;
        if(!(obj instanceof Person))
            throw new ClassCastException("類型錯誤");
        Person p= (Person)obj;
        return this.name.equals(p.name)&&this.age==p.age;
    }
}
           
黑馬程式員----集合

LinkHashSet :按照輸入順序進行編排。

TreeSet

TreeSet判斷元素唯一性的方式:就是根據比較方法的傳回結果是否是0,是0,就是相同元素,不存。

TreeSet對元素進行排序的方式一:

讓元素自身具備比較功能,元素就需要實作Comparable接口,覆寫compareTo方法。

可以使用TreeSet集合第二種排序方式:

讓集合自身具備比較功能,定義一個類實作Comparator接口,覆寫compare方法。

将該類對象作為參數傳遞給TreeSet集合的構造函數。

P.S.

如果自定義類實作了Comparable接口,并且TreeSet的構造函數中也傳入了比較器,那麼将以比較器

的比較規則為準。

TreeSet集合的底層是二叉樹進行排序的。

public class TreeSetDemo {

    public static void main(String[] args) {
        TreeSet<String> ts= new TreeSet<String>(new ComparatorByLen());
        ts.add("aaa");
        ts.add("zzs");
        ts.add("a");
        ts.add("1234");
        ts.add("2345");

        Iterator<String> it = ts.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

}

class ComparatorByLen implements Comparator{
    @Override
    public int compare(Object arg0, Object arg1) {
        String s1= (String)arg0;
        String s2= (String)arg1;
        int temp = s1.length() -s2.length();
        return temp;
    }
}
           
黑馬程式員----集合