Collection、Map试题

Collection 接口和 Collections 类都是做什么用的 ?

Collection:集合的抽象数据类型

Collections:包含有关集合操作的静态方法
Collection 接口有几个子接口 ?Map 接口有父接口么 ?

3个 没有

List 、 Set 、 Map 三个接口有什么特点 ?
List:有序集合,可以精准的控制列表中每个元素的插入位置

Set:可以容纳所有类型的对象,包括null,不允许重复,实现类是无序的,TreeSet除外

Map:

1 每次存储 key-value对;
2 key部分不能重复
3 常用实现类HashMap和TreeMap
请简述哈希表(散列表)

散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表

以下哪个集合接口支持通过字符串主键检索对象 A

A.Map

B.Set

C.List

D.Collection

以下哪些语句用于创建一个Map实例? D

A.Map m = new Map();

B.Map m = new Map(init capacity,increment capacity);

C.Map m = new Map(new Collection());

D.以上均不行

以下代码的执行结果是?

执行结果
abc
def

def

abc
def

public class Example {

public static void main(String[] args) {

 String s1 = "abc";
 String s2 = "def";
 String s3 = "def";

 List list = new ArrayList();
 list.add(s1);
 list.add(s2);
 list.add(s3);
 
 for (String string : list) {
     System.out.println( string );
 }
 
 System.out.println("-------------------");
 
 Set set = new HashSet<>();
 set.add(s1);
 set.add(s2);
 set.add(s3);
 
 for (String string : set) {
     System.out.println( string );
 }

}
}
以下代码执行结果是?TreeMap和 HashMap 的区别是什么 ?

one=1three=3two=2 TreeMap有序 HashMap无序

public class Example {

public static void main(String[] args) {

    TreeMap map = new TreeMap();
    map.put("one", "1");
    map.put("two", "2");
    map.put("three", "3");
    displayMap(map);

}

static void displayMap(TreeMap map) {

    Collection c = map.entrySet();
    Iterator i = c.iterator();

    while (i.hasNext()) {
        Object o = i.next();
        System.out.print(o.toString());
    }
}

}
Vector、ArrayList 和 LinkedList 有什么区别 ?

Vector、ArrayList:查询速度快,增删修改速度比较慢

LinkedList:查询速度比较慢,增删修改速度快

Arrays.ArrayList 和 java.util.ArrayList 有什么区别 ?

ArrayList是List接口的实现类

Arrays.ArrayList是没有add()方法的,并且修改元素也是通过修改之前传递进去的固定长度数组来实现,这就是为什么修改它的元素会直接影响传进来的数组。

Hashtable和HashMap的区别

1 Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现

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

3 只有HashMap可以让你将空值作为一个表的条目的key或value

分别使用 HashMap 和 List 以及数组统计数组中相同的值出现的次数

HashMap方法:
public static void main(String[] args) {
String[] array = {"abc" , "ABC" , "123" , "def" , "_" , "def" , "abc"};
HashMap 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> es = hm.entrySet();
    for ( Map.Entry h : es ) {
        Integer value = h.getValue();
        String key = h.getKey();
        System.out.println(key + "出现的次数为:" + value);
    }
}
/*
    打印结果:

ABC出现的次数为:1
123出现的次数为:1
abc出现的次数为:2
def出现的次数为:2
_出现的次数为:1
*/
List方法:

public static void main(String[] args) {
String[] array = {"abc" , "ABC" , "123" , "def" , "_" , "def" , "abc"};
ArrayList 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) + "相同值出现的次数为:" + value +"次");
                list.remove(list.get(j));
            }
        }
    }

    for (String l : list) {
        System.out.println(l);
    }
}

/*
打印结果:
[abc, ABC, 123, def, _, def, abc]
abc相同值出现的次数为:1次
ABC
123
def相同值出现的次数为:1次
_
*/

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

请写出 Iterator 迭代器的优点

迭代器通常被称为轻量级对象:创建它的代价小

请写出循环 List 、Set、Map 的代码

for( 集合元素类型 i : list ) {

System.out.println(i)

}

for( 集合元素类型 i : Set ) {

System.out.println(i)

}

for (Map.Entry m : map01.entrySet()) {
System.out.println(m);
}
以下哪个集合接口支持元素排序 A

A.Collections

B.Set

C.List

D.Map

你可能感兴趣的:(Collection、Map试题)