Collection:-

  • Collection is a single object which manages a group of objects. And in collection we call object as an elements.
  • Before the release of the JAVA SE 5.0 collections maintained references to object of type Object so, from that any type of an object can be stored in the collections.
  • After Java SE 5.0 onwards, we can use the generic collections.
  • By using generic collection we can maintain the object without type casting.

Collection API which contains interfaces that group objects as one of the following:-

  • Collection:-

A group of an objects known as an elements; implementations determine whether there is specific ordering and whether duplicates are permitted.

  • Set:-

Set is an unordered collection and in Set interface no duplicates are permitted.

  • List:-

List is an ordered collection and in List interface duplicates are permitted.

Collection implementations:-

In the collection framework Set, List, Map and Deque are the general purpose implementations of the core interfaces.

collections in JAVA

A Set Example:-

  1. import java.util.*;
  2. public class SetDemo
  3. {
  4. public static void main(String args[])
  5. {
  6. Set set = new HashSet();
  7. set.add(“one”);
  8. set.add(“second”);
  9. set.add(“3rd”);
  10. set.add(new Integer(4));
  11. set.add(new Float(5.0F));
  12. set.add(“second”); // duplicate is not added
  13. set.add(new Integer(4)); // duplicate is not added
  14. System.out.println(set);
  15. }
  16. }

Output:-

[one, second, 5.0, 3rd 4]

  • In above example, set variable is declared of type Set which is initialized to a new HashSet Object.
  • Then it uses it’s add method to add few elements and prints the set standard output.
  • Lines 12 and 13 attempt to add duplicate values to set. Because the duplicates are not allowed in Set interface add method returns false.
  • And also we can see that the order of the elements in the output is not the same as the order which they were added.
  • Line 14 prints the set object to the standard output. This works cause the HashSet class overrides the inherited toString method and creates a comma separated sequence.

A List Example:-

  1. import java.util.*;
  2. public class ListDemo
  3. {
  4. public static void main(String args[])
  5. {
  6. List list = new ArrayList();
  7. list.add(“one”);
  8. list.add(“second”);
  9. list.add(“3rd”);
  10. list.add(new Integer(4));
  11. list.add(new Float(5.0F));
  12. list.add(“second”); // duplicate is added
  13. list.add(new Integer(4)); // duplicate is added
  14. System.out.println(list);
  15. }
  16. }

Output:-

[one, second, 3rd, 4, 5.0, second, 4]

  • In the above example, List interface is almost similar as a Set interface.
  • In above example, list variable is declared of type List and which is assigned to the new ArrayList Object.
  • After that by using add method few elements are added and prints the list to the standard output.
  • Lines 12 and 13 attempt to add duplicate values to the list. Because the duplicates are allowed in List interface add method returns true.
  • And also we can see that the order of the elements in the output is same as the order which they were added.
  • Line 14 prints the list object to the standard output.

For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.

Written by Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Comment