集合

 

容器作业

一、    填空题

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

 

二、    选择题

1.

以下选项中关于Java集合的说法错误的是( AC   。(选择二项)

 

 

 

 

A.

List接口和Set接口是Collections接口有两个子接口

 

B.

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

       

 

三、    判断题

  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 )

 

四、    简答题 (首先熟练掌握笔记上 与集合相关的面试简答题)

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  ,效率低

 

五、    编码题 (首先熟练掌握笔记上 与集合相关的需求小案例)

 

  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 [>;

    }

}

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 [>;

    }

 

    @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 [>;

    }

 

    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 [>;

    }

 

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

    }

}