Java集合類相關面試題


1、Collection和Collections的差別

java.util.Collection 是一個集合接口,Collection接口在Java類庫中有非常多詳細的實現。比如List、Set

java.util.Collections 是針對集合類的一個幫助類,它提供了一系列的靜態方法實現對各種集合的搜索、排序、線程安全化等操作。

2、ArrayList與Vector的差別

這兩個類都實現了List接口(List接口繼承自Collection接口)。它們都是有序集合。它們內部的元素都是能夠反復的,都能夠依據序號取出當中的某一元素。

它們兩個的差別在於:

(1)、線程安全的問題:Vector是早期Java就有的,是同意多線程操作的。是線程安全的;而ArrayList是在Java2中才出現,它是線程不安全的,僅僅能使用單線程

操作。 因為Vector支持多線程操作,所以在性能上就比不上ArrayList了。

相同的HashTable相比於HashMap也是支持多線程的操作而導致性能不如HashMap。

(2)、數據增長的問題

ArrayList和Vector都有一個初始的容量大小,當存儲進去它們里面的元素個數超出容量的時候。就須要添加ArrayList和Vector的存儲空間,每次添加存儲空間

的時候不是僅僅添加一個存儲單元。是添加多個存儲單元。

Vector默認添加原來的一倍,ArrayList默認添加原來的0.5倍。

Vector能夠由我們自己來設置增長的大小,ArrayList沒有提供相關的方法。

3、LinkedList與ArrayList有什么差別

兩者都實現的是List接口。不同之處在於:

(1)、ArrayList是基於動態數組實現的,LinkedList是基於鏈表的數據結構。

(2)、get訪問List內部隨意元素時。ArrayList的性能要比LinkedList性能好。LinkedList中的get方法是要依照順序從列表的一端開始檢查,直到還有一端

(3)、對於新增和刪除操作LinkedList要強於ArrayList。由於ArrayList要移動數據

4、去掉Vector中的一個反復元素

import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;
public class VectorDemo {
	public static void main(String[] args) {
		Vector veList=new Vector();
		veList.add("aa");
		veList.add("bb");
		veList.add("aa");
		veList.add("bb");
		veList.add("cc");
		
		//去掉Vector中的反復元素方法一:
		veList=getNewVector(veList);
		//迭代結果
		System.out.println("*************************第一種方式************************");
		for(int i=0;i<veList.size();i++){
			System.out.println(veList.get(i));
		}
		
		//去掉Vector中的反復元素方法二:
		Vector veList1=getNewVector1(veList);
		System.out.println("*************************另外一種方式************************");
		for(int i=0;i<veList1.size();i++){
			System.out.println(veList1.get(i));
		}		
	}

	private static Vector getNewVector(Vector veList) {
		Vector newVector=new Vector();
		for(int i=0;i<veList.size();i++){
			String str=(String) veList.get(i);
			if(!newVector.contains(str)){
				newVector.add(str);
			}
		}
		return newVector;
	}
	
	private static Vector getNewVector1(Vector veList) {
		Vector newVector=new Vector();
		HashSet set=new HashSet(veList);
		Iterator it =set.iterator();
		while (it.hasNext()) {
			String str=(String) it.next();
			newVector.add(str);
		}
		return newVector;
	}
}

5、HashMap與HashTable的差別

兩者都實現了Map接口。主要差別在於:

(1)、HashTable是早期Java就有的,支持多線程操作。是線程安全的。HashMap是Java2才出現的。是HashTable的輕量級實現,僅支持單線程操作。線程不安

的。

(2)、HashMap同意空的key和value  HashTable不同意

6、List與Map的差別

List是存儲單列數據的集合,Map是存儲key和value這樣雙列數據的集合,List中存儲的數據是有順序的,而且同意反復。

Map其中存儲的數據是沒有順序的,它

存儲的key是不能反復的,value是能夠反復的。

List繼承Collection接口,Map不是。Map沒有父類

7、List、Map、Set三個接口。存取元素時各有什么特點

首先List和Set都是單列元素的集合。它們有一個共同的父接口Collection。

List內的元素講究有序性。內部元素可反復。可是Set恰恰相反。它講究的是無序性,元素不可反復。Set的add方法有一個boolean的返回值,每當add一個新元

素的時候都會調用equals方法進行逐一比較,當新元素與全部的已存在元素的都不反復的時候add成功返回true。否則返回false。

Map與List和Set不同,它是雙列存儲的(鍵和值一一相應)。它在存儲元素調用的是put方法,每次存儲時,要存儲一份key和value。不能存儲反復的key,這個

反復的規則也是利用equals進行比較。取數據的時候則能夠依據key獲取value。另外還是以獲得全部key的集合和全部value的集合。還能夠獲得key和value組成

的Map.Entry對象的集合。

8、介紹一下TreeSet

(1)TreeSet的原理

Tree在存儲對象的時候須要排序。可是須要指定排序的算法。

Integer和String能夠自己主動排序(有默認算法)

import java.util.*;
public class TreeSetDemo1 {
    public static void main(String[] args) {
        Set ts = new TreeSet();
        ts.add(new Integer(5));
        ts.add(new Integer(10));
        ts.add(new Integer(1));
        ts.add(new Integer(6));
        ts.add(new Integer(2));
        Iterator it = ts.iterator();
        /**
         * 結果打印的順序是1 2 5 6 10是依照規律的順序排列的,這是由於Integer類實現了Comparable接口
         * 重寫了它的compareTo()方法
         */
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
}

注:Integer類中compareTo()方法的實現方式:

    /**
     * Compares two {@code Integer} objects numerically.
     *
     * @param   anotherInteger   the {@code Integer} to be compared.
     * @return  the value {@code 0} if this {@code Integer} is
     *          equal to the argument {@code Integer}; a value less than
     *          {@code 0} if this {@code Integer} is numerically less
     *          than the argument {@code Integer}; and a value greater
     *          than {@code 0} if this {@code Integer} is numerically
     *           greater than the argument {@code Integer} (signed
     *           comparison).
     * @since   1.2
     */
    public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
    }

    /**
     * Compares two {@code int} values numerically.
     * The value returned is identical to what would be returned by:
     * <pre>
     *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
     * </pre>
     *
     * @param  x the first {@code int} to compare
     * @param  y the second {@code int} to compare
     * @return the value {@code 0} if {@code x == y};
     *         a value less than {@code 0} if {@code x < y}; and
     *         a value greater than {@code 0} if {@code x > y}
     * @since 1.7
     */
    public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ?

0 : 1); }


自己定義的類存儲的時候須要指定排序的算法,否則會出現異常。

假設想把自己定義的類存儲到TreeSet對象中,那

么必須實現Comparable接口。重寫它的compareTo()方法。在方法內定義比較大小的方法,依據大小關系,返回正數、負數或者0.

在使用TreeSet的add方法進行存儲對象的時候就會自己主動調用compareTo()方法進行比較,依據比較結果依照二叉樹的方式進行存儲。

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetDemo2 {
    public static void main(String[] args) {
            Set ts = new TreeSet();
            ts.add(new Teacher("zhangsan", 1));
            ts.add(new Teacher("lisi", 2));
            ts.add(new Teacher("wangmazi", 3));
            ts.add(new Teacher("wangwu",4));
            ts.add(new Teacher("mazi", 3));
            Iterator it = ts.iterator();
            while (it.hasNext()) {
                System.out.println(it.next());
            }
        }
}
class Teacher implements Comparable {
    int num;
    String name;

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

    public String toString() {
        return "學號:" + num + "\t\t姓名:" + name;
    }

    //o中存放時的紅黑二叉樹中的節點,從根節點開始比較
    public int compareTo(Object o) {
        Teacher ss = (Teacher) o;
        int result = num < ss.num ?

1 : (num == ss.num ? 0 : -1);//降序 //int result = num > ss.num ? 1 : (num == ss.num ? 0 : -1);//升序 if (result == 0) { result = name.compareTo(ss.name); } return result; } }


免責聲明!

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



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