Prefer Collections over older classes
来源:互联网 发布:同步看电视的软件 编辑:程序博客网 时间:2024/05/18 02:32
http://www.javapractices.com/topic/TopicAction.do?Id=39
Most programmers prefer the Java Collections Framework to arrays, Vector, and Hashtable, for the following reasons:
- the intent of the Collections Framework is to replace the older, less comprehensive tools
- synchronization can be easily controlled (while Hashtable and Vector are always synchronized)
- immutability can be easily controlled
- search and sort algorithms are ready-made
- the type of a reference to a Collection can be generic, while underlying implementation can be easily changed
- arrays are of fixed size, and are more error prone - off-by-one errors with array indexes have always been a fruitful source of error
- Arrays.asList may be used to pass an array to a method expecting a Collection or a List
- Collection.toArray may be used to convert a Collection to an Object[] or a T[]
- Collections.enumeration and Collections.list may be used to convert between enumerations and collections
Prior to JDK 1.5, the main disadvantage of using collections is that they always held an Object, and retrieval of their contents required casting. Casting cannot be checked at compile-time. Arrays, on the other hand, have the advantage of always being "parameterized" to hold objects of a specific type (in all versions of the JDK), and this is verified by the compiler. Thus, in older versions of the JDK, changing from a collection to an array forces one particular kind of error to appear at compile-time instead of run-time.
One minor advantage of arrays is that they are slightly easier to initialize. However, using Arrays.asList makes the difference trivial:
import java.util.*;public final class ArrayInit { /** Initialize arrays and Lists. */ public static void main(String... args){ //Array initializers are compact String[] paintings = {"oil", "watercolour"}; //Build a List using Arrays.asList(T...) //This works for any type, not just for String List<String> languages = Arrays.asList( "urdu", "hindi", "pali", "sanskrit" ); //Build a List in a more verbose way List<String> nicePlaces = new ArrayList<>(); nicePlaces.add("luxembourg gardens"); nicePlaces.add("sea side"); nicePlaces.add("large magellanic cloud"); }}
- Prefer Collections over older classes
- Older Thread-Safe Collections
- Why I Prefer Vim Over Emacs
- Prefer TimeUnit Sleep over Thread.Sleep
- weblogic配置:<prefer-web-inf-classes>true</prefer-web-inf-classes>
- weblogic配置<prefer-web-inf-classes>true</prefer-web-inf-classes>
- C#循环效率比较 Item6 - Prefer 'for' over 'foreach'. [Performance]
- Favor static member classes over nonstatic
- Effective Java 18:接口优于抽象类 Prefer interfaces to abstract classes
- Effective Objective-C 2.0: Item 3: Prefer Literal Syntax over the Equivalent Methods
- question2.Would you prefer to have classes during the day and do part-time jobs in the evening or ?
- Older Hobby
- Prefer company
- Classes
- Over
- OVER
- over
- over
- Java正则表达式入门
- 二、总结几种关于ViewControllers之间传值的方式(从子视图向父视图传值)
- java笔试题-随笔1
- APK反编译步骤
- 黑马程序员-学习笔记/面试题-交通灯
- Prefer Collections over older classes
- 2006年底的1篇CRM文章:对当前国内CRM市场发展的一点拙见。
- 请允许我重新开始
- jsp:useBean用法
- C++的替代运算标记符
- Annotation注解----详解(2)
- 关于cvSnakeImage函数的问题
- python实现TCP通信
- POJ 1664 放苹果