back

The Collections framework in Java

by Lilit Kocharyan for IT, Technology 40 Comments
The Collections framework in Java

Today’s topic is a little bit technical as it is related to Java developers. We are going to speak about Java and its frameworks like Collections, the Map interface, Java TreeMap class, etc. Our team continues to study interesting and essential features in web and mobile development, so let’s continue!

Basic concepts

Collections in java is a framework that provides an architecture to store and manipulate the group of objects. The Java Collection Framework is divided into two interfaces: Collection and Map. These interfaces isolate all collections that enter the two types of data boxes: simple sets of elements and sets of “key”. Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).

Methods of Collection interface
    1. boolean add(Object element) is used to insert an element in this collection.
    2. boolean addAll(Collection c) is used to insert the specified collection elements in the invoking collection.
    3. boolean remove(Object element) is used to delete an element from this collection.
    4. boolean removeAll(Collection c) is used to delete all the elements of specified collection from the invoking collection.
    5. boolean retainAll(Collection c) is used to delete all the elements of invoking collection except the specified collection.
    6. int size() return the total number of elements in the collection.
    7. void clear() removes the total no of element from the collection.
    8. boolean contains(Object element) is used to search an element.
    9. boolean containsAll(Collection c) is used to search the specified collection in this collection.
    10. Iterator iterator() returns an iterator.
    11. Object[] toArray() converts collection into array.
    12. boolean isEmpty() checks if collection is empty.
    13. boolean equals(Object element) matches two collection.
    14. int hashCode() returns the hashcode number for collection.

How ArrayList Works Internally in Java

ArrayList is a resizable array implementation of the List interface i.e. ArrayList grows dynamically as the elements are added to it.

If the size of the current elements (including the new element to be added to the ArrayList) is greater than the maximum size of the array then increase the size of array. But the size of the array can not be increased dynamically. So, what happens internally is, a new Array is created and the old array is copied into the new array.

Java LinkedList class

Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces. The important points about Java LinkedList are:

  • Java LinkedList class can contain duplicate elements.
  • Java LinkedList class maintains insertion order.
  • Java LinkedList class is non synchronized.
  • In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.
  • Java LinkedList class can be used as list, stack or queue.

Doubly Linked List

In case of doubly linked list, we can add or remove elements from both side.

The Map Interface

Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction. The Map interface includes methods for basic operations (such as put, get, remove, containsKey, containsValue, size, and empty), bulk operations (such as putAll and clear), and collection views (such as keySet, entrySet, and values).The Java platform contains three general-purpose Map implementations: HashMapTreeMap, and LinkedHashMap. Their behavior and performance are precisely analogous to HashSet, TreeSet, and LinkedHashSet, as described in The Set Interface section.

Java TreeMap class

Java TreeMap class implements the Map interface by using a tree. It provides an efficient means of storing key/value pairs in sorted order.

The important points about Java TreeMap class are:

  • A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
  • It contains only unique elements.
  • It cannot have null key but can have multiple null values.
  • It is same as HashMap instead maintains ascending order.

40 thoughts on “The Collections framework in Java”

  1. Pingback: free nude chat
  2. Pingback: adult webcams
  3. Pingback: free sex chat
  4. Pingback: live sex chat
  5. Pingback: live cam girls
  6. Pingback: live sex cams
  7. Pingback: live sex chat
  8. Pingback: live nude chat
  9. Pingback: webcam girls
  10. Pingback: free cam girls
  11. Pingback: rebirth ro
  12. Pingback: hours calculator
  13. Pingback: utc to ist
  14. Pingback: daily sunrise time
  15. Pingback: trx address
  16. Pingback: trx generator
  17. Pingback: tron address scan
  18. Pingback: live nude chat
  19. Pingback: live video chat
  20. Pingback: live adult webcams
  21. Pingback: free sex shows
  22. Pingback: cheap video chat
  23. Pingback: mail scanning
  24. Pingback: kooky
  25. Pingback: WishHour.Com
  26. Pingback: WishHour.Com
  27. Pingback: WishHour.Com

Comments are closed.

JavaScript and its most used frameworks and libraries
Prev post JavaScript and its most used frameworks and libraries
40 Comments

Frameworks try to solve most of the big and known problems that are usually encountered…

Servers
Next post Servers
40 Comments

Servers have been around as long as networks. A server is a computer program which…