JAVA:面试题·一

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

**Collection:集合的抽象数据类型**

    **Collections:包含有关集合操作的静态方法**

2.Collection 接口有几个子接口 ?Map 接口有父接口么 ?

3个           没有

3.List 、 Set 、 Map 三个接口有什么特点 ?

List:有序集合,可以精准的控制列表中每个元素的插入位置

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

Map::无序、以键值对的形式添加元素,键不能重复,值可以重复 它没有继承Collection接口 

1 每次存储 key-value对;
2 key部分不能重复
3 常用实现类HashMap和TreeMap

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.以下代码的执行结果是?

执行结果
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 );
        }
    }
}

8.以下代码执行结果是?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());
        }
    }
}

9.Vector、ArrayList 和 LinkedList 有什么区别 ?

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

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

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

ArrayList是List接口的实现类

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

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

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

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

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

    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);
            }
        }
    
```java
String[] array = {"abc" , "ABC" , "123" , "def" , "^_^" , "def" , "abc"};
```
  1. 请写出 Iterator 迭代器的优点

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

  2. 请写出循环 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);
    }
    
  3. 以下哪个集合接口支持元素排序 A

    A.Collection

    B.Set

    C.List

    D.Map

你可能感兴趣的:(JAVA:面试题·一)