![]() The Collection Framework provides a set of interfaces so that you can program on these interfaces instead of the actual implementation. Remember that a good program operates on the specifications instead of an actual implementation. This is possible as the ArrayList is a subtype of List. In line 11, we construct an ArrayList instance, and upcast it to the List interface.In this example, we pass the actual type String for the generic type E. To construct an instance of an ArrayList, we need to provide the actual type for generic type E. The interfaces/classes are designed (by the class designer) to take a generics type E.The Collection and Iterable interfaces define the common behaviors of all the collection implementations. We observe that ArrayList implements List, Collection and Iterable interfaces. ESPRESSO // LATTE // CAPPUCCINO // A List supports numerical index access, where index begins at 0įor (int i = 0 i is shown above. espresso // latte // cappuccino // (2) Use an enhanced for-each loop (JDK 5) to traverse through the list for (String str : coffeeLst) (str.toUpperCase()) (coffeeLst) // // (1) Use an Iterator to traverse through all the elements Iterator iter = erator() ArrayList can be written as ArrayList, with type inferred from List JDK 7 supports type inference on instantiation. It performs type checking when elements are added into the collection List coffeeLst = new ArrayList() Declare a List (List of String) implemented by an ArrayList // The compiler is informed about the type of objects in the collection via. ![]() JDK 5 List with Generics, implemented by an ArrayList This allows the compiler to perform type-checking when elements are added into the collection at compile-time to ensure type-safety at runtime. When you construct an instance of these generic types, you need to provide the specific type of the objects contained in these collection, e.g. The indicates that the interfaces are generic in design. The class is the most commonly used implementation of List. The interface is the most commonly used data structure of the Collection Framework, which models a resizable (dynamically-allocated) array supporting numerical index access. Generic Collection Framework (JDK 5) by Examples Example 1: List ( List of Strings) Implemented by ArrayList The classes and interfaces for the Collection Framework are kept in package java.util. You need to refer to the JDK API specification while reading this chapter. ![]() Auto-Boxing and Auto-Unboxing between primitive types and their wrapper classes.You also need to be familar with these concepts introduced in JDK 5: See " Inheritance, Substitution, Polymorphism and Type Casting" for a quick summary. Inheritance and Polymorphism, especially the upcasting and downcasting operations.Interfaces, abstract methods and their implementations.To understand this chapter, you have to be familiar with: The collection framework is retrofitted to support generics and takes full advantages of these new features. JDK 5 introduced Generics (which supports passing of types), and many related features (such as auto-boxing/auto-unboxing and for-each loop). ![]() JDK 1.2 introduced the unified collection framework, and retrofits the legacy classes ( Vector and Hashtable) to conform to this unified collection framework. Prior to JDK 1.2, Java's data structures consist of array, Vector, and Hashtable that were designed in a non-unified way with inconsistent public interfaces. Similar Collection Framework is the C++ Standard Template Library (STL). Algorithms (such as sorting and searching).The Java Collection Framework package ( java.util) contains: This allows the programmers to program at the interface specification, instead of the actual implementation. The collection framework provides a unified interface to store, retrieve and manipulate the elements of a collection, regardless of the underlying actual implementation. A well-designed framework can improve your productivity and provide ease of maintenance. ![]() A framework, by definition, is a set of interfaces that force you to adopt some design practices. Each item in a collection is called an element. In Java, dynamically allocated data structures (such as ArrayList, LinkedList, Vector, Stack, HashSet, HashMap, Hashtable) are supported in a unified architecture called the Collection Framework, which mandates the common behaviors of all the classes.Ī collection, as its name implied, is simply a container object that holds a collection of objects. Many applications may require more complex data structure such as linked list, stack, hash table, set, or tree. Furthermore, array is a simple linear structure. The array, however, does not support so-called dynamic allocation - it has a fixed length which cannot be changed once allocated. Although we can use an array as a container to store a group of elements of the same type (primitives or objects). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |