zoukankan      html  css  js  c++  java
  • Java集合类相关面试题

    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中的一个反复元素

    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Vector;
    public class VectorDemo {
    	public static void main(String[] args) {
    		Vector veList=new Vector();
    		veList.add("aa");
    		veList.add("bb");
    		veList.add("aa");
    		veList.add("bb");
    		veList.add("cc");
    		
    		//去掉Vector中的反复元素方法一:
    		veList=getNewVector(veList);
    		//迭代结果
    		System.out.println("*************************第一种方式************************");
    		for(int i=0;i<veList.size();i++){
    			System.out.println(veList.get(i));
    		}
    		
    		//去掉Vector中的反复元素方法二:
    		Vector veList1=getNewVector1(veList);
    		System.out.println("*************************另外一种方式************************");
    		for(int i=0;i<veList1.size();i++){
    			System.out.println(veList1.get(i));
    		}		
    	}
    
    	private static Vector getNewVector(Vector veList) {
    		Vector newVector=new Vector();
    		for(int i=0;i<veList.size();i++){
    			String str=(String) veList.get(i);
    			if(!newVector.contains(str)){
    				newVector.add(str);
    			}
    		}
    		return newVector;
    	}
    	
    	private static Vector getNewVector1(Vector veList) {
    		Vector newVector=new Vector();
    		HashSet set=new HashSet(veList);
    		Iterator it =set.iterator();
    		while (it.hasNext()) {
    			String str=(String) it.next();
    			newVector.add(str);
    		}
    		return newVector;
    	}
    }

    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能够自己主动排序(有默认算法)

    import java.util.*;
    public class TreeSetDemo1 {
        public static void main(String[] args) {
            Set ts = new TreeSet();
            ts.add(new Integer(5));
            ts.add(new Integer(10));
            ts.add(new Integer(1));
            ts.add(new Integer(6));
            ts.add(new Integer(2));
            Iterator it = ts.iterator();
            /**
             * 结果打印的顺序是1 2 5 6 10是依照规律的顺序排列的,这是由于Integer类实现了Comparable接口
             * 重写了它的compareTo()方法
             */
            while (it.hasNext()) {
                System.out.println(it.next());
            }
        }
    }

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

        /**
         * Compares two {@code Integer} objects numerically.
         *
         * @param   anotherInteger   the {@code Integer} to be compared.
         * @return  the value {@code 0} if this {@code Integer} is
         *          equal to the argument {@code Integer}; a value less than
         *          {@code 0} if this {@code Integer} is numerically less
         *          than the argument {@code Integer}; and a value greater
         *          than {@code 0} if this {@code Integer} is numerically
         *           greater than the argument {@code Integer} (signed
         *           comparison).
         * @since   1.2
         */
        public int compareTo(Integer anotherInteger) {
            return compare(this.value, anotherInteger.value);
        }
    
        /**
         * Compares two {@code int} values numerically.
         * The value returned is identical to what would be returned by:
         * <pre>
         *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
         * </pre>
         *
         * @param  x the first {@code int} to compare
         * @param  y the second {@code int} to compare
         * @return the value {@code 0} if {@code x == y};
         *         a value less than {@code 0} if {@code x < y}; and
         *         a value greater than {@code 0} if {@code x > y}
         * @since 1.7
         */
        public static int compare(int x, int y) {
            return (x < y) ? -1 : ((x == y) ?

    0 : 1); }


    自己定义的类存储的时候须要指定排序的算法,否则会出现异常。

    假设想把自己定义的类存储到TreeSet对象中,那

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

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

    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeSet;
    public class TreeSetDemo2 {
        public static void main(String[] args) {
                Set ts = new TreeSet();
                ts.add(new Teacher("zhangsan", 1));
                ts.add(new Teacher("lisi", 2));
                ts.add(new Teacher("wangmazi", 3));
                ts.add(new Teacher("wangwu",4));
                ts.add(new Teacher("mazi", 3));
                Iterator it = ts.iterator();
                while (it.hasNext()) {
                    System.out.println(it.next());
                }
            }
    }
    class Teacher implements Comparable {
        int num;
        String name;
    
        Teacher(String name, int num) {
            this.num = num;
            this.name = name;
        }
    
        public String toString() {
            return "学号:" + num + "		姓名:" + name;
        }
    
        //o中存放时的红黑二叉树中的节点,从根节点開始比較
        public int compareTo(Object o) {
            Teacher ss = (Teacher) o;
            int result = num < ss.num ?

    1 : (num == ss.num ? 0 : -1);//降序 //int result = num > ss.num ? 1 : (num == ss.num ? 0 : -1);//升序 if (result == 0) { result = name.compareTo(ss.name); } return result; } }

  • 相关阅读:
    Mac普通用户修改了/etc/sudoers文件的解决办法
    python对缓存(memcached,redis)的操作
    线程、进程、协程和队列
    python作用域和多继承
    sokect编程进阶
    socket编程基础
    python面相对象进阶
    python异常处理
    configparser模块
    subprocess模块
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/6729467.html
Copyright © 2011-2022 走看看