Welcome 微信登录

首页 / 软件开发 / JAVA / 驯服Tiger - 集合框架

驯服Tiger - 集合框架2010-12-16John ZukowskiJDK 5.0 中最吸引人的地方在于集合框架的一些最突出的特性上,例如:支 持泛型的语言级别上的新变化,以及可以在 java.util.concurrent 包中找到的 并发集合工具包。实际上,以前在 developerWorks 上的“驯服 Tiger: 并发集 合”和“介绍 JDK 5.0 中的泛型”这两篇教程中介绍了上述特性。但是其他增 强还没有得到足够的重视。在本文中,我将研究其他三个变化:更新过的 Arrays 和 Collections 类、新的 Queue 接口以及它的 PriorityQueue 实现。

数组(Array)

Arrays 类提供了一系列处理数组的静态工具方法,这些索引的数据结构的大 小是固定的。在 5.0 版本之前,Arrays 类拥有针对原始数据库类型和通用 Object 类型的每种不同数组类型的 binarySearch()、equals()、fill() 和 sort() 方法。用于将 Object 数组转换成 List 的附加 asList() 方法仍然有 用。Tiger 为所有数组添加了 hashCode() 和 toString() 方法,还添加了特定 于 Object 数组的 deepEquals()、deepHashCode() 和 deepToString() 方法。 总计有 21 个新方法可用:

public static boolean deepEquals(Object[] a1, Object[] a2)

public static int deepHashCode(Object[] a)

public static String deepToString(Object[] a)

public static int hashCode(boolean[] a)

public static int hashCode(byte[] a)

public static int hashCode(char[] a)

public static int hashCode(double[] a)

public static int hashCode(float[] a)

public static int hashCode(int[] a)

public static int hashCode(long[] a)

public static int hashCode(Object[] a)

public static int hashCode(short[] a)

public static String toString(boolean[] a)

public static String toString(byte[] a)

public static String toString(char[] a)

public static String toString(double[] a)

public static String toString(float[] a)

public static String toString(int[] a)

public static String toString(long[] a)

public static String toString(Object[] a)

public static String toString(short[] a)