一、填空题
Java集合框架提供了一套性能优良、使用方便的接口和类,包括Collection和Map两大类,它们都位于java.util包中
队列和堆栈有些相似,不同之处在于栈是先进后出,队列是先进先出。
链表结构是一种由多个节点组成的线性数据结构,并且每个节点包含有数据以及指向下一个节点的引用。
___LinkedList__是一种集合类,它 采用链表作为的存储结构,便于删除和添加元素,但是按照索引查询元素效率低下。
TreeSet是一种Collection类型的集合类,其中元素唯一,并采用二叉树作为存储结构,元素按照自然顺序排列。
如果希望将自定义类Student的多个对象放入集合TreeSet,实现所有元素按照某个属性的自然顺序排列,则需要Student类实现__Comparable__接口。
在Java中HashMap集合的访问时间接近稳定,它是一种键值对映射的数据结构。这个数据结构是通过数组来实现的。
迭代器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 list=new ArrayList();
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
list.add(f[i]);
}
for(int i=0;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 al) {
al.add(2);
al = new ArrayList();
al.add(3);
al.add(4);
}
public static void main(String[] args) {
List al = new ArrayList();
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 s=new HashSet();
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 map = new HashMap();
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()
三、判断题
数组和集合中的元素可以是任何数据类型,包括基本类型和引用类型。( F )
Java集合中的Set接口和List接口都是从Collection接口派生出来的。( T )
Collection 接口存储一组不唯一,有序的对象,它有两个子接口:List和Set。( F )
Collection是Java集合顶级接口,其中的元素无序,唯一。Java平台不提供这个接口任何直接的实现。( F )
List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引来访问List中的无素,这类似于Java的数组。( T )
HashSet采用哈希表存储结构,特点是查询速度快,但是其中元素无序排列。( T )
LinkedHashMap是一种有序的HashMap,查询速度快,便于添加删除操作。( T )
基本数据类型的值可以被直接存储在Vector对象中。( F )
Dictionary建立了关键字和值的映射,只要提供一个关键字,Dictionary就会返回一个相应的值。( T )
泛型是JavaSE1.7的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。Java语言引入泛型的好处是安全简单。( F )
Collection是专门操作集合的工具类,提供一系列静态方法实现对各种集合操作。( F )
Iterator接口可以遍历任何Collection接口的实现类,可以从一个Collection中使用iterator( )方法来获取迭代器实例。迭代器取代了Java集合框架中的Enumeration。( T )
四、简答题
集合和数组的比较
简述List、Set、Collection、Map的区别和联系。
ArrayList和LinkedList的区别和联系。
HashSet采用了哈希表作为存储结构,请说明哈希表的特点
Vector和ArrayList的区别和联系。
请你简述HashMap和Hashtable的区别?
五、编码题
1.使用List和Map存放多个图书信息,遍历并输出。其中商品属性:编号,名称,单价,出版社;使用商品编号作为Map中的key。
1 public classBook2 {3 public intid;4 publicString name;5 public doubleprice;6 publicString press;7 publicBook()8 {9 super();10 }11 public Book(int id, String name, doubleprice, String press)12 {13 super();14 this.id =id;15 this.name =name;16 this.price =price;17 this.press =press;18 }19 public intgetId()20 {21 returnid;22 }23 public void setId(intid)24 {25 this.id =id;26 }27 publicString getName()28 {29 returnname;30 }31 public voidsetName(String name)32 {33 this.name =name;34 }35 public doublegetPrice()36 {37 returnprice;38 }39 public void setPrice(doubleprice)40 {41 this.price =price;42 }43 publicString getPress()44 {45 returnpress;46 }47 public voidsetPress(String press)48 {49 this.press =press;50 }51 @Override52 publicString toString()53 {54 return "Book [id=" + id + ", name=" + name + ", press=" +press55 + ", price=" + price + "]";56 }57 }58 public classTestListMap59 {60 public static voidmain(String[] args)61 {62 Book b1 = new Book(1000, "b1", 30.5, "bjsxt");63 Book b1_1 = new Book(1000, "b1", 30, "bjsxt");64 Book b2 = new Book(1000, "b2", 50, "bjsxt");65 Book b3 = new Book(1001, "b3", 30.5, "bjsxt");66 Book b4 = new Book(1002, "b4", 30.5, "bjsxt");67 Book b5 = new Book(1003, "b5", 50, "bjsxt1");68 //使用HashSet存储图书并遍历
69 List bookList = new ArrayList();70 bookList.add(b1);71 bookList.add(b1);72 bookList.add(b2);73 bookList.add(b3);74 bookList.add(b4);75 bookList.add(b5);76 bookList.add(b1_1);77 System.out.println("遍历输出hashset");78 System.out.println(bookList.size());79 for(Book book : bookList)80 {81 System.out.println(book.toString());82 }83 //使用TreeSet存储图书并遍历
84 Map bookMap = new HashMap();85 bookMap.put(b1.getId(), b1);86 bookMap.put(b1.getId(), b1);87 bookMap.put(b2.getId(), b2);88 bookMap.put(b3.getId(), b3);89 bookMap.put(b4.getId(), b4);90 bookMap.put(b5.getId(), b5);91 bookMap.put(b1_1.getId(), b1_1);92 System.out.println("遍历输出treeset");93 for (Entryentry : bookMap.entrySet())94 {95 System.out.println(entry.getKey() + "----------->" +entry.getValue());96 }97 }98 }
2.使用HashSet和TreeSet存储多个商品信息,遍历并输出;其中商品属性:编号,名称,单价,出版社;要求向其中添加多个相同的商品,验证集合中元素的唯一性。
提示:向HashSet中添加自定义类的对象信息,需要重写hashCode和equals( )
向TreeSet中添加自定义类的对象信息,需要实现Comparable接口,指定比较规则
1 public class Book implements Comparable
2 {3 public intid;4 publicString name;5 public doubleprice;6 publicString press;7 publicBook()8 {9 super();10 }11 public Book(int id, String name, doubleprice, String press)12 {13 super();14 this.id =id;15 this.name =name;16 this.price =price;17 this.press =press;18 }19 public intcompareTo(Book o)20 {21 return this.id -o.id;22 }23 @Override24 public inthashCode()25 {26 final int prime = 31;27 int result = 1;28 result = prime * result +id;29 result = prime * result + ((name == null) ? 0: name.hashCode());30 result = prime * result + ((press == null) ? 0: press.hashCode());31 longtemp;32 temp =Double.doubleToLongBits(price);33 result = prime * result + (int) (temp ^ (temp >>> 32));34 returnresult;35 }36 @Override37 public booleanequals(Object obj)38 {39 if (this ==obj)40 {41 return true;42 }43 if (obj == null)44 {45 return false;46 }47 if (getClass() !=obj.getClass())48 {49 return false;50 }51 Book other =(Book) obj;52 if (id !=other.id)53 {54 return false;55 }56 if (name == null)57 {58 if (other.name != null)59 {60 return false;61 }62 } else if (!name.equals(other.name))63 {64 return false;65 }66 if (press == null)67 {68 if (other.press != null)69 {70 return false;71 }72 } else if (!press.equals(other.press))73 {74 return false;75 }76 if (Double.doubleToLongBits(price) !=Double77 .doubleToLongBits(other.price))78 {79 return false;80 }81 return true;82 }83 @Override84 publicString toString()85 {86 return "Book [id=" + id + ", name=" + name + ", press=" +press87 + ", price=" + price + "]";88 }89 }90 public classTestSet91 {92 public static voidmain(String[] args)93 {94 Book b1 = new Book(1000, "b1", 30.5, "bjsxt");95 Book b1_1 = new Book(1000, "b1", 30, "bjsxt");96 Book b2 = new Book(1000, "b2", 50, "bjsxt");97 Book b3 = new Book(1001, "b3", 30.5, "bjsxt");98 Book b4 = new Book(1002, "b4", 30.5, "bjsxt");99 Book b5 = new Book(1003, "b5", 50, "bjsxt1");100 //使用HashSet存储图书并遍历
101 Set hashSet = new HashSet();102 hashSet.add(b1);103 hashSet.add(b1);104 hashSet.add(b2);105 hashSet.add(b3);106 hashSet.add(b4);107 hashSet.add(b5);108 hashSet.add(b1_1);109 System.out.println("遍历输出hashset");110 System.out.println(hashSet.size());111 for(Book book : hashSet)112 {113 System.out.println(book.toString());114 }115 //使用TreeSet存储图书并遍历
116 Set treeSet = new TreeSet();117 treeSet.add(b1);118 treeSet.add(b1);119 treeSet.add(b2);120 treeSet.add(b3);121 treeSet.add(b4);122 treeSet.add(b5);123 treeSet.add(b1_1);124 System.out.println("遍历输出treeset");125 for(Book book : treeSet)126 {127 System.out.println(book.toString());128 }129 }130 }
3.实现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对象
1 public classTestListToMap2 {3 public voidlistToMap()4 {5 //1.创建多个学生信息
6 Student stu1 = new Student(110, "小明", 23, 98.0);7 Student stu2 = new Student(111, "大刚", 21, 80.5);8 Student stu3 = new Student(112, "小白", 12, 93.0);9 //2.加入List
10 List list = new ArrayList();11 list.add(stu1);12 list.add(stu2);13 list.add(stu3);14 //3.遍历List,输出每个Student信息
15 for(Student stu : list)16 {17 System.out.println(stu);18 }19 //4.将List中数据放入Map,使用Student的id属性作为key Map map = new HashMap();
20 Iterator it =list.iterator();21 while(it.hasNext())22 {23 Student stu =it.next();24 map.put(stu.getId(), stu);25 }26 //5.遍历Map,输出每个Entry的key和value
27 Set> entrySet =map.entrySet();28 for (Entryentry : entrySet)29 {30 System.out.println(entry.getKey() + "---->" +entry.getValue());31 }32 }33 }34 public classStudentEntry35 {36 private int key;//关键字
37 private Student stu;//学生
38 public intgetKey()39 {40 returnkey;41 }42 public void setKey(intkey)43 {44 this.key =key;45 }46 publicStudent getStu()47 {48 returnstu;49 }50 public voidsetStu(Student stu)51 {52 this.stu =stu;53 }54 }55 public classTestMapToList56 {57 public voidmapToList()58 {59 //1.创建多个学生信息
60 Student stu1 = new Student(110, "小明", 23, 98.0);61 Student stu2 = new Student(111, "大刚", 21, 80.5);62 Student stu3 = new Student(112, "小白", 12, 93.0);63 //2.使用Student的id属性作为key,存入Map
64 Map map = new HashMap();65 map.put(stu1.getId(), stu1);66 map.put(stu2.getId(), stu2);67 map.put(stu2.getId(), stu3);68 //3.创建List对象,每个元素类型是StudentEntry
69 List list = new ArrayList();70 //4.将Map对象转化为List集合
71 for (Entryentry : map.entrySet())72 {73 StudentEntry studentEntry = newStudentEntry();74 //将map中的一个映射关系,封装为一个studentEntry对象
75 studentEntry.setKey(entry.getKey());76 studentEntry.setStu(entry.getValue());77 //将studentEntry对象List集合
78 list.add(studentEntry);79 }80 //5.遍历Map
81 for(StudentEntry se : list)82 {83 System.out.println(se.getKey() + "\t" +se.getStu());84 }85 }86 }
六、可选题
1.假如有以下email数据“aa@sohu.com,bb@163.com,cc@sina.com,..”现需要把email中的用户部分和邮件地址部分分离,分离后以键值对应的方式放入HashMap?
1 public classEmailSplit2 {3 public static voidmain(String[] args)4 {5 String str = "aa@sohu.com,bb@163.com,cc@sina.com";6 //得到每一个email
7 String strs[] = str.split(",");8 //存放分离后email的信息
9 Map emailMap = new HashMap();10 for(String email : strs)11 {12 String temp[] = email.split("@");13 //分割email存入map
14 emailMap.put(temp[0], temp[1]);15 }16 System.out.println(emailMap.toString());17 }18 }
2.由控制台按照固定格式输入学生信息,包括学号,姓名,年龄信息,当输入的内容为exit退出;将输入的学生信息分别封装到一个Student对象中,再将每个Student对象加入到一个集合中,要求集合中的元素按照年龄大小正序排序;最后遍历集合,将集合中学生信息写入到记事本,每个学生数据占单独一行。
推荐步骤:
a) 创建Student类,并指定按照年龄正序排列
b) 通过控制台输入多个不同Student信息。格式规定为:编号#姓名#年龄
c) 取出字符串中相应信息放入Student对象,并将Student加入到集合中
d) 遍历集合的过程中将学生的信息输入到记事本
难点:
e) 如何指定学生按照年龄正序排列
f) 如果从字符串“编号#姓名#年龄”中提取学生信息
g) 放入哪种集合后可以保证学生按照年龄大小正序排列
h) 如何将集合中学生信息写入记事本,每个学生数据占单独一行
1 public class Student implements Comparable
2 {3 privateInteger num;4 privateString name;5 privateInteger age;6 //省略getter和setter方法7 //省略构造方法
8 public intcompareTo(Student stu)9 {10 return this.age -stu.age;11 }12 publicString toString()13 {14 return "Student [age=" + age + ", name=" +name15 + ", num=" + num + "]";16 }17 }18 public classTest19 {20 public static voidmain(String[] args)21 {22 //保存输入信息到set中
23 Set stuSet =saveStudentInfo();24 //遍历set
25 Iterator it =stuSet.iterator();26 while(it.hasNext())27 {28 String info =it.next().toString();29 System.out.println(info);30 }31 }32 private static SetsaveStudentInfo()33 {34 Scanner input = newScanner(System.in);35 //保存学生信息的TreeSet集合对象
36 Set stuSet = new TreeSet();37 while (true)38 {39 //输入提示
40 System.out.println("请输入学生信息:(学号#姓名#年龄)");41 String inputData =input.nextLine();42 //判断是否退出 inputData.equals("exit")
43 if ("exit".equals(inputData))44 {45 break;46 }47 //将用户输入的学生信息分割为String[]
48 String[] info = inputData.split("#");49 //将输入信息封装到Student对象中
50 Student stu51 = new Student(Integer.parseInt(info[0]), info[1],52 Integer.parseInt(info[2]));53 //将学生对象加入集合
54 stuSet.add(stu);55 }56 returnstuSet;57 }58 }