泛型作業(yè)

1. Collection 接口和 Collections 類都是做什么用的 ?

? **Collection:集合的抽象數(shù)據(jù)類型**

**Collections:包含有關(guān)集合操作的靜態(tài)方法**

2. Collection 接口有幾個(gè)子接口 ?Map 接口有父接口么 ?

? 3個(gè)? ? ? ? ? 沒有

3. ? ? List 、 Set 、 Map 三個(gè)接口有什么特點(diǎn) ?

? List:有序集合,可以精準(zhǔn)的控制列表中每個(gè)元素的插入位置

? Set:可以容納所有類型的對(duì)象,包括null,不允許重復(fù),實(shí)現(xiàn)類是無序的,TreeSet除外

? Map:

? 1 每次存儲(chǔ) key-value對(duì);

? ? 2 key部分不能重復(fù)

? ? 3 常用實(shí)現(xiàn)類HashMap和TreeMap

4. 請(qǐng)簡述哈希表(散列表)

? 散列表(Hash table,也叫哈希表),是根據(jù)關(guān)鍵碼值(Key value)而直接進(jìn)行訪問的數(shù)據(jù)結(jié)構(gòu)。也就是說,它通過把關(guān)鍵碼值映射到表中一個(gè)位置來訪問記錄,以加快查找的速度。這個(gè)映射函數(shù)叫做散列函數(shù),存放記錄的數(shù)組叫做散列表

5. 以下哪個(gè)集合接口支持通過字符串主鍵檢索對(duì)象? ? ? ? A

? A.Map

? B.Set

? C.List

? D.Collection


6. 以下哪些語句用于創(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.以上均不行

7. 以下代碼的執(zhí)行結(jié)果是?

? ##### 執(zhí)行結(jié)果

? abc

? def

? def

? ----------------------------------

? abc

? def

? ```java

? 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 );

? }

? }

? }

? ```

8. 以下代碼執(zhí)行結(jié)果是?TreeMap和 HashMap 的區(qū)別是什么 ?

? one=1three=3two=2? ? ? ? ? TreeMap有序? ? ? ? HashMap無序

```java

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());

}

}

}

```

9. Vector、ArrayList 和 LinkedList 有什么區(qū)別 ?

? Vector、ArrayList:查詢速度快,增刪修改速度比較慢

? LinkedList:查詢速度比較慢,增刪修改速度快

10. Arrays.ArrayList 和 java.util.ArrayList 有什么區(qū)別 ?

? ArrayList是List接口的實(shí)現(xiàn)類

? Arrays.ArrayList**是沒有add()方法的,并且修改元素也是通過修改之前傳遞進(jìn)去的固定長度數(shù)組來實(shí)現(xiàn),這就是為什么修改它的元素會(huì)直接影響傳進(jìn)來的數(shù)組。**

11. Hashtable和HashMap的區(qū)別

? ? 1? ? Hashtable是基于陳舊的Dictionary類的,HashMap是Java 1.2引進(jìn)的Map接口的一個(gè)實(shí)現(xiàn)

? ? 2? ? Hashtable的方法是同步的,而HashMap的方法不是

? ? 3? ? 只有HashMap可以讓你將空值作為一個(gè)表的條目的key或value

12. 分別使用 HashMap 和 List 以及數(shù)組統(tǒng)計(jì)數(shù)組中相同的值出現(xiàn)的次數(shù)

? ? ##### HashMap方法:

? ? ```java

? ? public static void main(String[] args) {

? ? ? ? ? ? String[] array = {"abc" , "ABC" , "123" , "def" , "^_^" , "def" , "abc"};

? ? ? ? ? ? HashMap<String, Integer> hm = new HashMap<>();


? ? ? ? ? ? for ( String key : array ) {

? ? ? ? ? ? ? ? Integer value = hm.get(key);

? ? ? ? ? ? ? ? if ( value == null ) {

? ? ? ? ? ? ? ? ? ? hm.put(key,1);

? ? ? ? ? ? ? ? }else {

? ? ? ? ? ? ? ? ? ? value ++;

? ? ? ? ? ? ? ? ? ? hm.put(key,value);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }


? ? ? ? ? ? Set<Map.Entry<String, Integer>> es = hm.entrySet();

? ? ? ? ? ? for ( Map.Entry<String,Integer> h : es ) {

? ? ? ? ? ? ? ? Integer value = h.getValue();

? ? ? ? ? ? ? ? String key = h.getKey();

? ? ? ? ? ? ? ? System.out.println(key + "出現(xiàn)的次數(shù)為:" + value);

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /*

? ? ? ? ? ? 打印結(jié)果:

? ? ABC出現(xiàn)的次數(shù)為:1

? ? 123出現(xiàn)的次數(shù)為:1

? ? abc出現(xiàn)的次數(shù)為:2

? ? def出現(xiàn)的次數(shù)為:2

? ? ^_^出現(xiàn)的次數(shù)為:1

? ? ? ? */

? ? ```

? ? List方法:

? ? ```java

? ? public static void main(String[] args) {

? ? ? ? ? ? String[] array = {"abc" , "ABC" , "123" , "def" , "^_^" , "def" , "abc"};

? ? ? ? ? ? ArrayList<String> list = new ArrayList<>();

? ? ? ? ? ? for ( String a : array ) {

? ? ? ? ? ? ? ? list.add(a);

? ? ? ? ? ? }


? ? ? ? ? ? System.out.println(list);

? ? ? ? ? ? for (int i = 0; i < list.size(); i++) {

? ? ? ? ? ? ? ? for (int j = i+1; j < list.size(); j++) {

? ? ? ? ? ? ? ? ? ? if ( list.get(i) == list.get(j) ) {

? ? ? ? ? ? ? ? ? ? ? ? int value = 0;

? ? ? ? ? ? ? ? ? ? ? ? value ++;

? ? ? ? ? ? ? ? ? ? ? ? list.set(i,list.get(i) + "相同值出現(xiàn)的次數(shù)為:" + value +"次");

? ? ? ? ? ? ? ? ? ? ? ? list.remove(list.get(j));

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }


? ? ? ? ? ? for (String l : list) {

? ? ? ? ? ? ? ? System.out.println(l);

? ? ? ? ? ? }

? ? ? ? }

? ? /*

? ? ? ? ? ? 打印結(jié)果:

? ? [abc, ABC, 123, def, ^_^, def, abc]

? ? abc相同值出現(xiàn)的次數(shù)為:1次

? ? ABC

? ? 123

? ? def相同值出現(xiàn)的次數(shù)為:1次

? ? ^_^

? ? ? ? */

? ? ```


? ? ```java

? ? String[] array = {"abc" , "ABC" , "123" , "def" , "^_^" , "def" , "abc"};

? ? ```

13. 請(qǐng)寫出 Iterator 迭代器的優(yōu)點(diǎn)

? ? 迭代器通常被稱為輕量級(jí)對(duì)象:創(chuàng)建它的代價(jià)小

14. 請(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);

? ? }

? ? ```

15. 以下哪個(gè)集合接口支持元素排序? ? A

? ? A.Collections

? ? B.Set

? ? C.List

? ? D.Map

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Collection 接口和 Collections 類都是做什么用的 ?Collection:集合的抽象數(shù)據(jù)類型...
    常皓欽閱讀 237評(píng)論 0 0
  • Collection 接口和 Collections 類都是做什么用的 ?Collection:集合的抽象數(shù)據(jù)類型...
    開心_1acc閱讀 132評(píng)論 0 0
  • Collection 接口和 Collections 類都是做什么用的 ?Collection:集合的抽象數(shù)據(jù)類型...
    瓊瓊i閱讀 151評(píng)論 0 0
  • Collection 接口和 Collections 類都是做什么用的 ?Collection接口和Collect...
    朱夢軒閱讀 395評(píng)論 0 0
  • . Collection 接口和 Collections 類都是做什么用的 ? Collection:集合的抽象數(shù)...
    承超越閱讀 193評(píng)論 0 0

友情鏈接更多精彩內(nèi)容