集合

 

容器做業java

1、    填空題面試

  1. Java集合框架提供了一套性能優良、使用方便的接口和類,包括Collection和Map兩大類,它們都位於 ____java.util_________ 包中
  2. 隊列和堆棧有些類似,不一樣之處在於 ___隊列先進先出,棧先進後出__________ 。
  3.  ____鏈表_________ 結構是一種由多個節點組成的線性數據結構,而且每一個節點包含有數據以及指向下一個節點的引用。
  4. __LinkList____________是一種集合類,它 採用鏈表做爲的存儲結構,便於刪除和添加元素,可是按照索引查詢元素效率低下。
  5.  ___TreeSet__________ 是一種Collection類型的集合類,其中元素惟一,並採用二叉樹做爲存儲結構,元素按照天然順序排列。
  6. 若是但願將自定義類Student的多個對象放入集合TreeSet,實現全部元素按照某個屬性的天然順序排列,則須要Student類實現______Comparable____________接口。
  7. 在Java中 ____HashMap_________ 集合的訪問時間接近穩定,它是一種鍵值對映射的數據結構。這個數據結構是經過數組來實現的。
  8. 迭代器Iterator爲集合而生,專門實現集合遍歷,該接口有三個方法,分別是hasNext() 、____next()________、remove()。

 

2、    選擇題算法

1.數組

如下選項中關於Java集合的說法錯誤的是( AC   。(選擇二項)安全

 

 

 

 

A.數據結構

List接口和Set接口是Collections接口有兩個子接口框架

 

B.ide

List接口中存放的元素具備有序,不惟一的特色工具

 

C.性能

Set接口中存放的元素具備無序,不惟一的特色

 

D.

Map接口存放的是映射信息,每一個元素都是一個鍵值對

 

2.

以下Java代碼,輸出的運行結果是(  A  )。(選擇一項)

 

public class Test {

         public static void main(String[ ] args) {

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

                   list.add("str1");

                   list.add(2, "str2");

                   String s=list.get(1);

                   System.out.println(s);

         }

}

 

 

 

 

A

運行時出現異常

 

B.

正確運行,輸出str1

 

C.

正確運行,輸出str2

 

D.

編譯時出現異常

 

3.

如下Java代碼的做用是首先將一個數組的內容存入集合,而後判斷集合中是否有指定的元素存在,其中共有(  D  )處錯誤。(選擇一項)

 

import java.util.List;

public class Test {

         public int getIndexofArray(float[] f){

                   int rtn=-1;

                   float objf=3.4;

                   List list=null;

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

                            list.add(f[i]);

                   }

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

                            float tmp=(float)list.get(i);

                            if(objf==tmp){

                                     rtn=i;

                            }

                   }

                   return rtn;

         }       

}

 

 

 

 

A

0

 

B.

1

 

C.

2

 

D.

3

 

4.

分析以下Java 代碼,編譯運行後將輸出(  B  )。(選擇一項)

 

public class Test {

         public Test() {

         }

         static void print(List<Integer> al) {

                   al.add(2);

                   al = new ArrayList<Integer>();

                   al.add(3);

                   al.add(4);

         }

         public static void main(String[] args) {

                   List<Integer> al = new ArrayList<Integer>();

                   al.add(1);

                   print(al);

                   System.out.println(al.get(1));

         }

}

 

 

 

 

A

1

 

B.

2

 

C.

3

 

D.

4

 

5.

Java,下列集合類型能夠存儲無序、不重複的數據的是(  D  )。(選擇一項)

 

 

 

 

A

ArrayList

 

B.

LinkedList

 

C.

TreeSet

 

D.

HashSet

 

6.

如下代碼的執行結果是(  C  )。(選擇一項)

 

Set<String> s=new HashSet<String>();

s.add("abc");

s.add("abc");

s.add("abcd");

s.add("ABC");

System.out.println(s.size());

 

 

 

 

A.

1

 

B.

2

 

C.

3

 

D.

4

 

7.

給定以下Java代碼,編譯運行的結果是( C   )。(選擇一項)

 

public class Test {

         public static void main(String[] args) {

                   Map<String, String> map = new HashMap<String, String>();

                   String s = "code";

                   map.put(s, "1");

                   map.put(s, "2");

                   System.out.println(map.size());

         }

}

 

 

 

 

A

編譯時發生錯誤

 

B.

運行時引起異常

 

C.

正確運行,輸出:1

 

D.

正確運行,輸出:2

 

8.

下面集合類中屬於非線程安全,且結構採用了哈希表的是(  C  。(選擇一項)

 

 

 

 

A.

Vector

 

B.

ArrayList

 

C.

HashMap

 

D.

Hashtable

 

9.

 

Java中,LinkedList類與ArrayList類同屬於集合框架類,下列(  CD  )選項中是LinkedList類有而ArrayList類沒有的方法。(選擇兩項)

 

 

 

 

A

add(Object o)

 

B.

add(int index,Object o)

 

C.

getFirst()

 

D.

removeLast()

       

 

3、    判斷題

  1. 數組和集合中的元素能夠是任何數據類型,包括基本類型和引用類型。( N  )
  2. Java集合中的Set接口和List接口都是從Collection接口派生出來的。(   Y )
  3. Collection 接口存儲一組不惟一,有序的對象,它有兩個子接口:List和Set。( N   )
  4. Collection是Java集合頂級接口,其中的元素無序,惟一。Java平臺不提供這個接口任何直接的實現。( N   )
  5. List是有序的Collection,使用此接口可以精確的控制每一個元素插入的位置。用戶可以使用索引來訪問List中的無素,這相似於Java的數組。(  Y  )
  6. HashSet採用哈希表存儲結構,特色是查詢速度快,可是其中元素無序排列。( Y   )
  7. LinkedHashMap是一種有序的HashMap,查詢速度快,便於添加刪除操做。( Y   )
  8. 基本數據類型的值能夠被直接存儲在Vector對象中。(   N )
  9. Dictionary創建了關鍵字和值的映射,只要提供一個關鍵字,Dictionary就會返回一個相應的值。( Y   )
  10. 泛型是JavaSE1.7的新特性,泛型的本質是參數化類型,也就是說所操做的數據類型被指定爲一個參數。Java語言引入泛型的好處是安全簡單。(Y    )
  11. Collection是專門操做集合的工具類,提供一系列靜態方法實現對各類集合操做。( F   )
  12. Iterator接口能夠遍歷任何Collection接口的實現類,能夠從一個Collection中使用iterator( )方法來獲取迭代器實例。迭代器取代了Java集合框架中的Enumeration。(   Y )

 

4、    簡答題 (首先熟練掌握筆記上 與集合相關的面試簡答題)

1.熟練掌握Collection集合和Map集合的體系圖。

 

2.簡述List、Set、Collection、Map的特色和區別及何時使用該集合。

 Collection
               List(存取有序,有索引,能夠重複)
                   ArrayList
                       底層是數組實現的,線程不安全,查找和修改快,增和刪比較慢
                   LinkedList
                       底層是鏈表實現的,線程不安全,增和刪比較快,查找和修改比較慢
                   Vector
                       底層是數組實現的,線程安全的,不管增刪改查都慢
                   若是查找和修改多,用ArrayList
                   若是增和刪多,用LinkedList
                   若是都多,用ArrayList
               Set(存取無序,無索引,不能夠重複)
                   HashSet
                       底層是哈希算法實現
                       LinkedHashSet
                           底層是鏈表實現,可是也是能夠保證元素惟一,和HashSet原理同樣
                   TreeSet
                       底層是二叉樹算法實現
                   通常在開發的時候不須要對存儲的元素排序,因此在開發的時候大多用HashSet,HashSet的效率比較高
                   TreeSet在面試的時候比較多,問你有幾種排序方式,和幾種排序方式的區別
      Map
               HashMap
                   底層是哈希算法,針對鍵
                  LinkedHashMap
                      底層是鏈表,針對鍵
              TreeMap
               底層是二叉樹算法,針對鍵
            開發中用HashMap比較多

3.Vector和ArrayList的區別和聯繫。

ArrayList 底層是數組實現的,線程不安全,查找和修改快,增和刪比較慢

Vector 底層是數組實現的,線程安全的,不管增刪改查都慢

4.請你簡述HashMap和Hashtable的區別?

HashMap是非synchronized  效率高

 Hashtable是線程安全的也是synchronized  ,效率低

 

5、    編碼題 (首先熟練掌握筆記上 與集合相關的需求小案例)

 

  1. 使用List和Map存放多個圖書信息,遍歷並輸出。其中商品屬性:編號,名稱,單價,出版社;使用商品編號做爲Map中的key

 

package com.zuikc.bean; public class Book { private int id; private String name; private double price; private String publisher; public Book() { super(); } public Book(int id, String name, double price, String publisher) { super(); this.id = id; this.name = name; this.price = price; this.publisher = publisher; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } @Override public String toString() { return "Books_Message [id=" + id + ", name=" + name + ", price=" + price + ", publisher=" + publisher + "]"; } } package com.zuikc.demo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import com.zuikc.bean.Book; public class Books { public static void main(String[] args) { Book b1 = new Book(1000, "b1", 30.5, "adddf"); Book b1_1 = new Book(1000, "b1", 30, "fnf"); Book b2 = new Book(1000, "b2", 50, "hfh"); Book b3 = new Book(1001, "b3", 30.5, "fnnf"); Book b4 = new Book(1002, "b4", 30.5, "drtt"); Book b5 = new Book(1003, "b5", 50, "rtyh"); // 使用HashSet存儲圖書並遍歷
 List<Book> bookList = new ArrayList<Book>(); bookList.add(b1); bookList.add(b1); bookList.add(b2); bookList.add(b3); bookList.add(b4); bookList.add(b5); bookList.add(b1_1); System.out.println("遍歷輸出hashset"); System.out.println(bookList.size()); for (Book book : bookList) { System.out.println(book.toString()); } // 使用TreeSet存儲圖書並遍歷
 Map<Integer, Book> bookMap = new HashMap<Integer, Book>(); bookMap.put(b1.getId(), b1); bookMap.put(b1.getId(), b1); bookMap.put(b2.getId(), b2); bookMap.put(b3.getId(), b3); bookMap.put(b4.getId(), b4); bookMap.put(b5.getId(), b5); bookMap.put(b1_1.getId(), b1_1); System.out.println("遍歷輸出treeset"); for (Entry<Integer, Book> entry : bookMap.entrySet()) { System.out.println(entry.getKey() + "----------->" + entry.getValue()); } } }

 

 

  1. 使用HashSet和TreeSet存儲多個商品信息,遍歷並輸出;其中商品屬性:編號,名稱,單價,出版社;要求向其中添加多個相同的商品,驗證集合中元素的惟一性。

提示:向HashSet中添加自定義類的對象信息,須要重寫hashCode和equals( )

          向TreeSet中添加自定義類的對象信息,須要實現Comparable接口,指定比較規則

package com.zuikc.bean; public class Book implements Comparable<Book> { private int id; private String name; private double price; private String publisher; public Book() { super(); } public Book(int id, String name, double price, String publisher) { super(); this.id = id; this.name = name; this.price = price; this.publisher = publisher; } public int getId() { return id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + ((publisher == null) ? 0 : publisher.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Book other = (Book) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) return false; if (publisher == null) { if (other.publisher != null) return false; } else if (!publisher.equals(other.publisher)) return false; return true; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } @Override public String toString() { return "Books_Message [id=" + id + ", name=" + name + ", price=" + price + ", publisher=" + publisher + "]"; } @Override public int compareTo(Book o) { return this.id - o.id; } } package com.zuikc.demo; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; import com.zuikc.bean.Book; public class Books { public static void main(String[] args) { Book b1 = new Book(1000, "b1", 30.5, "adddf"); Book b1_1 = new Book(1000, "b1", 30, "fnf"); Book b2 = new Book(1000, "b2", 50, "hfh"); Book b3 = new Book(1001, "b3", 30.5, "fnnf"); Book b4 = new Book(1002, "b4", 30.5, "drtt"); Book b5 = new Book(1003, "b5", 50, "rtyh"); // 使用HashSet存儲圖書並遍歷
 Set<Book> hashSet = new HashSet<Book>(); hashSet.add(b1); hashSet.add(b1); hashSet.add(b2); hashSet.add(b3); hashSet.add(b4); hashSet.add(b5); hashSet.add(b1_1); System.out.println("遍歷輸出hashset"); System.out.println(hashSet.size()); for (Book book : hashSet) { System.out.println(book.toString()); } // 使用TreeSet存儲圖書並遍歷
 Set<Book> treeSet = new TreeSet<Book>(); treeSet.add(b1); treeSet.add(b1); treeSet.add(b2); treeSet.add(b3); treeSet.add(b4); treeSet.add(b5); treeSet.add(b1_1); System.out.println("遍歷輸出treeset"); for (Book book : treeSet) { System.out.println(book); } } }

 

 

  1. 實現List和Map數據的轉換。具體要求以下:

功能1:定義方法public void listToMap( ){ }將List中Student元素封裝到Map中

1)        使用構造方法Student(int id,String name,int age,String sex )建立多個學生信息並加入List

2)        遍歷List,輸出每一個Student信息

3)        將List中數據放入Map,使用Student的id屬性做爲key,使用Student對象信息做爲value

4)        遍歷Map,輸出每一個Entry的key和value

功能2:定義方法public void mapToList( ){ }將Map中Student映射信息封裝到List

1)        建立實體類StudentEntry,能夠存儲Map中每一個Entry的信息

2)        使用構造方法Student(int id,String name,int age,String sex )建立多個學生信息,並使用Student的id屬性做爲key,存入Map

3)        建立List對象,每一個元素類型是StudentEntry

4)        將Map中每一個Entry信息放入List對象

package com.zuikc.bean; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.Set; public class Student { public int id; String name; int age; String sex; public Student() { super(); } public Student(int id, String name, int age, String sex) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } public void listToMap() { Student x1 = new Student(1, "張三", 12, "男"); Student x2 = new Student(9, "李四", 22, "男"); Student x3 = new Student(10, "王五", 25, "男"); Student x4 = new Student(16, "張六", 22, "男"); Student x5 = new Student(17, "趙六", 19, "男"); Student x6 = new Student(20, "錢七", 22, "男"); List<Student> al = new ArrayList<Student>(); al.add(x1); al.add(x2); al.add(x3); al.add(x4); al.add(x4); al.add(x5); al.add(x6); for (Student x : al) { System.out.println(x); } HashMap<Integer, Student> hm = new HashMap<Integer, Student>(); Iterator<Student> it = al.iterator(); while (it.hasNext()) { Student x = it.next(); hm.put(x.id, x); } Set<Entry<Integer, Student>> en = hm.entrySet(); for (Entry<Integer, Student> e : en) { System.out.println("key:" + e.getKey() + "valuey:" + e.getValue()); } } } package com.zuikc.bean; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import java.util.Set; public class StudentEntry extends Student { public StudentEntry() { super(); } public StudentEntry(int id, String name, int age, String sex) { super(id, name, age, sex); } @Override public String toString() { return "StudentEntry [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } public void mapToList() { StudentEntry x1 = new StudentEntry(1, "張三", 12, "男"); StudentEntry x2 = new StudentEntry(9, "李四", 22, "男"); StudentEntry x3 = new StudentEntry(10, "王五", 25, "男"); StudentEntry x4 = new StudentEntry(16, "張六", 22, "男"); StudentEntry x5 = new StudentEntry(17, "趙六", 19, "男"); StudentEntry x6 = new StudentEntry(20, "錢七", 22, "男"); HashMap<Integer, StudentEntry> hm = new HashMap<Integer, StudentEntry>(); hm.put(x1.id, x1); hm.put(x2.id, x2); hm.put(x3.id, x3); hm.put(x4.id, x4); hm.put(x5.id, x5); hm.put(x6.id, x6); Set<Entry<Integer, StudentEntry>> en = hm.entrySet(); List<StudentEntry> al = new ArrayList<StudentEntry>(); for (Entry<Integer, StudentEntry> e : en) { System.out.println(e); al.add(e.getValue()); } for (StudentEntry x : al) { System.out.println(x); } } } package com.zuikc.demo; import com.zuikc.bean.Student; import com.zuikc.bean.StudentEntry; public class AA { public static void main(String[] args) { Student x = new Student(); x.listToMap(); StudentEntry x1 = new StudentEntry(); x1.mapToList(); } }
相關文章
相關標籤/搜索