Java Collections Framework
A Collections Framework mainly contains the following 3 parts
A Collections Framework is defined by a set of interfaces, concrete class implementations for most of the interfaces and a set of standard utility methods and algorithms. In addition, the framework also provides several abstract implementations, which are designed to make it easier for you to create new and different implementations for handling collections of data.Core Collection Interfaces
The core interfaces that define common functionality and allow collections to be manipulated independent of their implementation.The 6 core Interfaces used in the Collection framework are:
- Collection
- Set
- List
- Iterator (Not a part of the Collections Framework)
- SortedSet
- Map
- SortedMap
Collection Interface
Map Interface
Concrete Classes
The concrete classes that are specific implementations of the core interfaces, providing data structures that a java program can use.Standard utility methods and algorithms
Standard utility methods and algorithms
that can be used to perform various operations on collections, such as sorting, searching or creating customized collections.
How are Collections Used
- The collections stores object references, rather than objects themselves. Hence primitive values cannot be stored in a collection directly. They need to be encapsulated (using wrapper classes) into an Object prior to storing them into a Collection (such as HashSet, HashMap etc).
- The references are always stored as type Object. Thus, when you retrieve an element from a collection, you get an Object rather then the actual type of the collection stored in the database. Hence we need to downcast it to the Actual Type while retrieving an element from a collection.
- One of the capabilities of the Collection Framework is to create a new Collection object and populate it with the contents of an existing Collection object of a same or different actual type.
Below is an example program showing the storing and retrieving of a few Collection Types
import java.util.*; public class CollectionsDemo { public static void main(String[] args) { List a1 = new ArrayList(); a1.add(”Beginner”); a1.add(”Java”); a1.add(”tutorial”); System.out.println(” ArrayList Elements”); System.out.print(”\t” + a1); List l1 = new LinkedList(); l1.add(”Beginner”); l1.add(”Java”); l1.add(”tutorial”); System.out.println(); System.out.println(” LinkedList Elements”); System.out.print(”\t” + l1); Set s1 = new HashSet(); // or new TreeSet() will order the elements; s1.add(”Beginner”); s1.add(”Java”); s1.add(”Java”); s1.add(”tutorial”); System.out.println(); System.out.println(” Set Elements”); System.out.print(”\t” + s1); Map m1 = new HashMap(); // or new TreeMap() will order based on keys m1.put(”Windows”, “98″); m1.put(”Win”, “XP”); m1.put(”Beginner”, “Java”); m1.put(”Tutorial”, “Site”); System.out.println(); System.out.println(” Map Elements”); System.out.print(”\t” + m1); } } |
Output
ArrayList Elements[Beginner, Java, tutorial]
LinkedList Elements
[Beginner, Java, tutorial]
Set Elements
[tutorial, Beginner, Java]
Map Elements
{Tutorial=Site, Windows=98, Win=XP, Beginner=Java}
No comments:
Post a Comment