Java基础之集合类常见试题

来源:互联网 发布:国际服务器端口定义 编辑:程序博客网 时间:2024/06/05 05:25

1、Collection和Collections的区别

java.util.Collection 是一个集合接口,Collection接口在Java类库中有很多具体的实现,例如List、Set

java.util.Collections 是针对集合类的一个帮助类,它提供了一系列的静态方法实现对各种集合的搜索、排序、线程安全化等操作。

2、ArrayList与Vector的区别

这两个类都实现了List接口(List接口继承自Collection接口)。它们都是有序集合,它们内部的元素都是可以重复的,都可以根据序号取出其中的某一元素。

它们两个的区别在于:

(1)、线程安全的问题:Vector是早期Java就有的,是允许多线程操作的,是线程安全的;而ArrayList是在Java2中才出现,它是线程不安全的,只能使用单线程

操作。 由于Vector支持多线程操作,所以在性能上就比不上ArrayList了。同样的HashTable相比于HashMap也是支持多线程的操作而导致性能不如HashMap。

(2)、数据增长的问题

ArrayList和Vector都有一个初始的容量大小,当存储进去它们里面的元素个数超出容量的时候,就需要增加ArrayList和Vector的存储空间,每次增加存储空间

的时候不是只增加一个存储单元,是增加多个存储单元。Vector默认增加原来的一倍,ArrayList默认增加原来的0.5倍。

Vector可以由我们自己来设置增长的大小,ArrayList没有提供相关的方法。

3、LinkedList与ArrayList有什么区别

两者都实现的是List接口,不同之处在于:

(1)、ArrayList是基于动态数组实现的,LinkedList是基于链表的数据结构。

(2)、get访问List内部任意元素时,ArrayList的性能要比LinkedList性能好。LinkedList中的get方法是要按照顺序从列表的一端开始检查,直到另一端

(3)、对于新增和删除操作LinkedList要强于ArrayList,因为ArrayList要移动数据

4、去掉Vector中的一个重复元素

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import java.util.HashSet;  
  2. import java.util.Iterator;  
  3. import java.util.Vector;  
  4. public class VectorDemo {  
  5.     public static void main(String[] args) {  
  6.         Vector veList=new Vector();  
  7.         veList.add("aa");  
  8.         veList.add("bb");  
  9.         veList.add("aa");  
  10.         veList.add("bb");  
  11.         veList.add("cc");  
  12.           
  13.         //去掉Vector中的重复元素方法一:  
  14.         veList=getNewVector(veList);  
  15.         //迭代结果  
  16.         System.out.println("*************************第一种方式************************");  
  17.         for(int i=0;i<veList.size();i++){  
  18.             System.out.println(veList.get(i));  
  19.         }  
  20.           
  21.         //去掉Vector中的重复元素方法二:  
  22.         Vector veList1=getNewVector1(veList);  
  23.         System.out.println("*************************第二种方式************************");  
  24.         for(int i=0;i<veList1.size();i++){  
  25.             System.out.println(veList1.get(i));  
  26.         }         
  27.     }  
  28.   
  29.     private static Vector getNewVector(Vector veList) {  
  30.         Vector newVector=new Vector();  
  31.         for(int i=0;i<veList.size();i++){  
  32.             String str=(String) veList.get(i);  
  33.             if(!newVector.contains(str)){  
  34.                 newVector.add(str);  
  35.             }  
  36.         }  
  37.         return newVector;  
  38.     }  
  39.       
  40.     private static Vector getNewVector1(Vector veList) {  
  41.         Vector newVector=new Vector();  
  42.         HashSet set=new HashSet(veList);  
  43.         Iterator it =set.iterator();  
  44.         while (it.hasNext()) {  
  45.             String str=(String) it.next();  
  46.             newVector.add(str);  
  47.         }  
  48.         return newVector;  
  49.     }  
  50. }  

5、HashMap与HashTable的区别

两者都实现了Map接口,主要区别在于:

(1)、HashTable是早期Java就有的,支持多线程操作,是线程安全的。HashMap是Java2才出现的,是HashTable的轻量级实现,仅支持单线程操作,线程不安

的。

(2)、HashMap允许空的key和value  HashTable不允许

6、List与Map的区别

List是存储单列数据的集合,Map是存储key和value这样双列数据的集合,List中存储的数据是有顺序的,并且允许重复。Map当中存储的数据是没有顺序的,它

存储的key是不能重复的,value是可以重复的。

List继承Collection接口,Map不是,Map没有父类

7、List、Map、Set三个接口,存取元素时各有什么特点

首先List和Set都是单列元素的集合,它们有一个共同的父接口Collection。

List内的元素讲究有序性,内部元素可重复;但是Set恰恰相反,它讲究的是无序性,元素不可重复;Set的add方法有一个boolean的返回值,每当add一个新元

素的时候都会调用equals方法进行逐一比较,当新元素与所有的已存在元素的都不重复的时候add成功返回true,否则返回false。

Map与List和Set不同,它是双列存储的(键和值一一对应),它在存储元素调用的是put方法,每次存储时,要存储一份key和value,不能存储重复的key,这个

重复的规则也是利用equals进行比较。取数据的时候则可以根据key获取value。另外还是以获得所有key的集合和所有value的集合,还可以获得key和value组成

的Map.Entry对象的集合。

8、介绍一下TreeSet

(1)TreeSet的原理

Tree在存储对象的时候需要排序,但是需要指定排序的算法。

Integer和String可以自动排序(有默认算法)

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import java.util.*;  
  2. public class TreeSetDemo1 {  
  3.     public static void main(String[] args) {  
  4.         Set ts = new TreeSet();  
  5.         ts.add(new Integer(5));  
  6.         ts.add(new Integer(10));  
  7.         ts.add(new Integer(1));  
  8.         ts.add(new Integer(6));  
  9.         ts.add(new Integer(2));  
  10.         Iterator it = ts.iterator();  
  11.         /** 
  12.          * 结果打印的顺序是1 2 5 6 10是按照规律的顺序排列的,这是因为Integer类实现了Comparable接口 
  13.          * 重写了它的compareTo()方法 
  14.          */  
  15.         while (it.hasNext()) {  
  16.             System.out.println(it.next());  
  17.         }  
  18.     }  
  19. }  

注:Integer类中compareTo()方法的实现方式:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * Compares two {@code Integer} objects numerically. 
  3.  * 
  4.  * @param   anotherInteger   the {@code Integer} to be compared. 
  5.  * @return  the value {@code 0} if this {@code Integer} is 
  6.  *          equal to the argument {@code Integer}; a value less than 
  7.  *          {@code 0} if this {@code Integer} is numerically less 
  8.  *          than the argument {@code Integer}; and a value greater 
  9.  *          than {@code 0} if this {@code Integer} is numerically 
  10.  *           greater than the argument {@code Integer} (signed 
  11.  *           comparison). 
  12.  * @since   1.2 
  13.  */  
  14. public int compareTo(Integer anotherInteger) {  
  15.     return compare(this.value, anotherInteger.value);  
  16. }  
  17.   
  18. /** 
  19.  * Compares two {@code int} values numerically. 
  20.  * The value returned is identical to what would be returned by: 
  21.  * <pre> 
  22.  *    Integer.valueOf(x).compareTo(Integer.valueOf(y)) 
  23.  * </pre> 
  24.  * 
  25.  * @param  x the first {@code int} to compare 
  26.  * @param  y the second {@code int} to compare 
  27.  * @return the value {@code 0} if {@code x == y}; 
  28.  *         a value less than {@code 0} if {@code x < y}; and 
  29.  *         a value greater than {@code 0} if {@code x > y} 
  30.  * @since 1.7 
  31.  */  
  32. public static int compare(int x, int y) {  
  33.     return (x < y) ? -1 : ((x == y) ? 0 : 1);  
  34. }  

自定义的类存储的时候需要指定排序的算法,否则会出现异常。如果想把自定义的类存储到TreeSet对象中,那

么必须实现Comparable接口,重写它的compareTo()方法,在方法内定义比较大小的方法,根据大小关系,返回正数、负数或者0.

在使用TreeSet的add方法进行存储对象的时候就会自动调用compareTo()方法进行比较,根据比较结果按照二叉树的方式进行存储。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import java.util.Iterator;  
  2. import java.util.Set;  
  3. import java.util.TreeSet;  
  4. public class TreeSetDemo2 {  
  5.     public static void main(String[] args) {  
  6.             Set ts = new TreeSet();  
  7.             ts.add(new Teacher("zhangsan"1));  
  8.             ts.add(new Teacher("lisi"2));  
  9.             ts.add(new Teacher("wangmazi"3));  
  10.             ts.add(new Teacher("wangwu",4));  
  11.             ts.add(new Teacher("mazi"3));  
  12.             Iterator it = ts.iterator();  
  13.             while (it.hasNext()) {  
  14.                 System.out.println(it.next());  
  15.             }  
  16.         }  
  17. }  
  18. class Teacher implements Comparable {  
  19.     int num;  
  20.     String name;  
  21.   
  22.     Teacher(String name, int num) {  
  23.         this.num = num;  
  24.         this.name = name;  
  25.     }  
  26.   
  27.     public String toString() {  
  28.         return "学号:" + num + "\t\t姓名:" + name;  
  29.     }  
  30.   
  31.     //o中存放时的红黑二叉树中的节点,从根节点开始比较  
  32.     public int compareTo(Object o) {  
  33.         Teacher ss = (Teacher) o;  
  34.         int result = num < ss.num ? 1 : (num == ss.num ? 0 : -1);//降序  
  35.         //int result = num > ss.num ? 1 : (num == ss.num ? 0 : -1);//升序  
  36.         if (result == 0) {  
  37.             result = name.compareTo(ss.name);  
  38.         }  
  39.         return result;  
  40.     }  
  41. }  
0 0
原创粉丝点击