What is collection in java ? Types of collections and thier uses ? and a code example
What is collection in java ? Types of collections and thier uses ? and a code example
Collapse
X
-
-
To elaborate further on rajujrks answer, here are short explanations of the most common collection types:- Array: Arrays are a very basic collection type; they have a fixed number of elements, all of the same (super)type. The elements have a given order, though you can access any element in an array without having to consider the others.
- List: A List is a collection of Objects which has a given order but, in difference to an array the size is not necessarily fixed (though it can be) and you can't necessarily access any element in the List without considering the others (though depending on the implementation it may be possible nevertheless). Lists are normally iterated through starting with the first element and then continuing until you reach a certain element you're looking for or the end of the list.
- Set: A set contains no duplicate elements and is not necessarily ordered in any fixed way.
- Queue: A Queue is a so called FIFO-collection, which stands for "first in, first out". Similar to a queue you may stand in at a shop, elements are added at the end of the queue while they are taken from the front. You cannot, in general, access any elements behind the first one.
There is a subtype of this, the Deque or "double ended queue". This offers adding elements to and removing them from either end of the queue. - Map: A Map will save an element in combination with a key; it is a kind of generalisation of an Array where the key is the position. Maps allow you to use any kind of object as a key and can therefore be very useful for database-like operations or stuff like saving properties from property files.
For more details, check out the site The Collections Framework Overview which explains the idea behind the framework and the way in which the various types can and should be used.Comment
Comment