1.Collection 接口和 Collections 类都是做什么用的 ?
Collection接口和Collections类都在java.util包下,前者是接口,后者是类。
Collection:是集合类的上层接口。本身是一个interface,里面包含了一些集合的基本操作。
Collections是一个集合框架的帮助类,里面包含一些对集合的排序,搜索以及序列化的操作。
2.Collection 接口有几个子接口 ?Map 接口有父接口么 ?
Collection接口有两个子接口List、Set、Queue
没有
3.List 、 Set 、 Map 三个接口有什么特点 ?
List可以精准控制列表中每个元素的插入位置。允许出现重复的值
Set不允许重复,HashSet存储顺序为无序。,TreeSet存储顺序为有序,排序后为升序
Map它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。
4请简述哈希表(散列表)
哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表或哈希表。
5.以下哪个集合接口支持通过字符串主键检索对象 A
A.Map
B.Set
C.List
D.Collection
6.以下哪些语句用于创建一个Map实例?D
A.Map m = new Map();
B.Map m = new Map(init capacity,increment capacity);
C.Map m = new Map(new Collection());
D.以上均不行
7.以下代码的执行结果是?
abcdef
def
abcdef
publicclassExample{
publicstaticvoidmain(String[]args) {
Strings1="abc";
Strings2="def";
Strings3="def";
List<String>list=newArrayList<String>();
list.add(s1);
list.add(s2);
list.add(s3);
for(Stringstring:list) {
System.out.println(string);
}
System.out.println("-------------------");
Set<String>set=newHashSet<>();
set.add(s1);
set.add(s2);
set.add(s3);
for(Stringstring:set) {
System.out.println(string);
}
}
}
8.以下代码执行结果是?TreeMap和 HashMap 的区别是什么 ?
执行结果:one=1three=3two=2
HashMap中元素是没有顺序的,TreeMap中所有元素都是有某一固定顺序的
publicclassExample{
publicstaticvoidmain(String[]args) {
TreeMap<String,String>map=newTreeMap<String,String>();
map.put("one","1");
map.put("two","2");
map.put("three","3");
displayMap(map);
}
staticvoiddisplayMap(TreeMapmap) {
Collection<String>c=map.entrySet();
Iterator<String>i=c.iterator();
while(i.hasNext()) {
Objecto=i.next();
System.out.print(o.toString());
}
}
}
9.Vector、ArrayList 和 LinkedList 有什么区别 ?
Vector是使用对象数组来保存数据的,可以根据需要自动增加容量
ArrayList也是根据需要调整容量,不过和Vector有区别,Vector在扩容时会提高一倍,而Array List则是增长50%,有利于节省内存空间
Vector的性能比ArrayList要差
LinkedList是Java提供的双向链表
ArrayList和Vector是基于动态数组实现的,LinkedList是基于双向链表实现的
10.Arrays.ArrayList 和 java.util.ArrayList 有什么区别 ?
Arrays.ArrayList只是为了序列化写的一个内部类,只有一个参数为数组的构造函数,修改元素也是通过修改之前传递进去的固定长度数组来时出现,且没有add方法
java.util.ArrayList:优点,有序,可以按下标操作,添加速度快 缺点:检索能力差
11.Hashtable和HashMap的区别
Hashtable继承自Dictionary类,而HashMap实现了Map接口
Hashtable的方法是同步的,HashMap的方法不是同步的
Hashtable重速度、轻安全,是线程非安全的,HashMap允许null值(Key和Value都允许),需要程序员自己管理线程的同步问题
HashMap => 不同步、空键值、效率高;Hashtable => 同步、非空键值、效率略低
12.分别使用 HashMap 和 List 以及数组统计数组中相同的值出现的次数
String[]array={"abc","ABC","123","def","^_^","def","abc"};
abc2
1231
def2
^_^1
13.请写出 Iterator 迭代器的优点
迭代器会提高效率,一方面每次next循环都会yield出一个值,供sum函数累加使用,这样就不用占用很大的内存,另一方面,使用迭代器也不用完全等到前n个数全部遍历完再进行累加,效率更高!
可以边遍历边删除。
14.请写出循环 List 、Set、Map 的代码
List:
Iterator<String>it=list.iterator();
while(it.hasNext()){
Stringx=it.next();
if(x.equals("del")){
it.remove();
}
}
Set:
Set<String>set=newHashSet<String>();
Iterator<String>it=set.iterator();
while(it.hasNext()) {
Stringstr=it.next();
System.out.println(str);
}
Map:
Setset=map.entrySet();
Iteratori=set.iterator();
while(i.hasNext()){
Map.Entry<String,String>entry1=(Map.Entry<String,String>)i.next();
System.out.println(entry1.getKey()+"=="+entry1.getValue());
}
15、以下哪个集合接口支持元素排序 C
A.Collection
B.Set
C.List
D.Map
网友评论