-
Collection 接口和 Collections 類都是做什么用的 ?
Collection是一個(gè)集合接口。他提供對(duì)集合對(duì)象進(jìn)行基本操作的通用接口方法。Collection接口意義是為各種具體集合提供最大化統(tǒng)一操作。
Collections是一個(gè)包裝類,他包含有關(guān)各種集合的靜態(tài)方法,大多數(shù)方法都是用來處理線性表的,一個(gè)工具類,服務(wù)于Java的Collection框架
-
Collection 接口有幾個(gè)子接口 ?Map 接口有父接口么 ?
三個(gè)子接口 List,Sey,Queue
沒有父接口
-
List 、 Set 、 Map 三個(gè)接口有什么特點(diǎn) ?
-
List:可以精確控制列表中每一個(gè)元素的插入位置,允許出現(xiàn)重復(fù)的值 List列表 linkedList鏈表
Set:不允許重復(fù),HashSet存儲(chǔ)順序?yàn)闊o序,TreeSet存儲(chǔ)順序?yàn)橛行?,排序后為升?/h5>
-
Map:它根據(jù)鍵的HashSet值存儲(chǔ)數(shù)據(jù),根據(jù)鍵可以直接獲取它的值,具有很快的訪問速度。
-
-
請(qǐng)簡述哈希表(散列表)
散列表也叫作哈希表(hash table),這種數(shù)據(jù)結(jié)構(gòu)提供了鍵(Key)和值(Value)的映射關(guān)系。只要給出一個(gè)Key,就可以高效查找到它所匹配的Value,時(shí)間復(fù)雜度接近于O(1)。
-
以下哪個(gè)集合接口支持通過字符串主鍵檢索對(duì)象 A`
A.Map
B.Set
C.List
D.Collection
-
以下哪些語句用于創(chuàng)建一個(gè)Map實(shí)例?D
A.Map m = new Map();
B.Map m = new Map(init capacity,increment capacity);
C.Map m = new Map(new Collection());
D.以上均不行
-
以下代碼的執(zhí)行結(jié)果是?
public class Example { public static void main(String[] args) { String s1 = "abc"; String s2 = "def"; String s3 = "def"; List<String> list = new ArrayList<String>(); list.add(s1); list.add(s2); list.add(s3); for (String string : list) { System.out.println( string ); } System.out.println("-------------------"); Set<String> set = new HashSet<>(); set.add(s1); set.add(s2); set.add(s3); for (String string : set) { System.out.println( string ); } } }運(yùn)行結(jié)果:
abc
def
def
abc
def
-
以下代碼執(zhí)行結(jié)果是?TreeMap和 HashMap 的區(qū)別是什么 ?
public class Example { public static void main(String[] args) { TreeMap<String, String> map = new TreeMap<String, String>(); map.put("one", "1"); map.put("two", "2"); map.put("three", "3"); displayMap(map); } static void displayMap(TreeMap map) { Collection<String> c = map.entrySet(); Iterator<String> i = c.iterator(); while (i.hasNext()) { Object o = i.next(); System.out.print(o.toString()); } } }運(yùn)行結(jié)果:one=1three=3two=2
TreepMap:是對(duì)鍵按需存放
HashMao:用到了哈希嗎的算法,一便快速查找一個(gè)鍵
-
Vector、ArrayList 和 LinkedList 有什么區(qū)別 ?
Arraylist和Vector是采用數(shù)組方式存儲(chǔ)數(shù)據(jù),此數(shù)組元素?cái)?shù)大于實(shí)際存儲(chǔ)的數(shù)據(jù)以便增加插入元素,都允許直接序號(hào)索引元素,
但是插入數(shù)據(jù)要涉及到數(shù)組元素移動(dòng)等內(nèi)存操作,所以插入數(shù)據(jù)慢,查找有下標(biāo),所以查詢數(shù)據(jù)快,
Vector由于使用了synchronized方法-線程安全,所以性能上比ArrayList要差,LinkedList使用雙向鏈表實(shí)
現(xiàn)存儲(chǔ),按序號(hào)索引數(shù)據(jù)需要進(jìn)行向前或向后遍歷,但是插入數(shù)據(jù)時(shí)只需要記錄本項(xiàng)前后項(xiàng)即可,插入數(shù)據(jù)較快。 -
Arrays.ArrayList 和 java.util.ArrayList 有什么區(qū)別 ?
ArrayList是List接口的實(shí)現(xiàn)類
Arrays.ArrayList是沒有add()方法的,并且修改元素也是通過修改之前傳遞進(jìn)去的固定長度數(shù)組來實(shí)現(xiàn),這就是為什么修改它的元素會(huì)直接影響傳進(jìn)來的數(shù)組。
-
Hashtable和HashMap的區(qū)別
繼承的父類不同
Hashtable繼承自Dictionary類,而HashMap繼承自AbstractMap類。但二者都實(shí)現(xiàn)了Map接口。
線程--安全性不同
javadoc中關(guān)于hashmap的一段描述如下:此實(shí)現(xiàn)不是同步的。如果多個(gè)線程同時(shí)訪問一個(gè)哈希映射,而其中至少一個(gè)線程從結(jié)構(gòu)上修改了該映射,則它必須保持外部同步。
-
分別使用 HashMap 和 List 以及數(shù)組統(tǒng)計(jì)數(shù)組中相同的值出現(xiàn)的次數(shù)
兩次
String[] array = {"abc" , "ABC" , "123" , "def" , "^_^" , "def" , "abc"}; -
請(qǐng)寫出 Iterator 迭代器的優(yōu)點(diǎn)
iterator是為了實(shí)現(xiàn)對(duì)Java容器(collection)進(jìn)行遍歷功能的一個(gè)接口。
在iterator實(shí)現(xiàn)了Iterator接口后,相當(dāng)于把一個(gè)Collection容器的所有對(duì)象,做成一個(gè)線性表(List),而iterator本身是一個(gè)指針,開始時(shí)位于第一個(gè)元素之前。 -
請(qǐng)寫出循環(huán) List 、Set、Map 的代碼
for( 集合元素類型 i : list ) { System.out.println(i) } for( 集合元素類型 i : Set ) { System.out.println(i) } ```java for (Map.Entry<String,String> m : map01.entrySet()) { System.out.println(m); } ```
-
以下哪個(gè)集合接口支持元素排序 A
A.Collection
B.Set
C.List
D.Map