Skip to main content

Posts

Showing posts with the label DSA

Collection Interface - Java Collections Framework - DSA

Most people consider the collection as the root interface of Collections Framework and it is true to a great extent but another part of Collections Framework is Map Interface, we will see that later, Most Common methods which are applicable to all collections are defined in this interface for example add() to add an element, size() to get the size and much more, below is a table of most common methods. Hierarchey of the Collection Interface. The Parent of Collection Interface is Iterator Interface and the Collection is base class for List Interface, Set Interface and Queue Interface, the respective classes which impliments either of the sub classes will also implement the defined methods from the Collection Interface, below are some of the commonly used methods. Defined Methods: Method Description add() This method returns a Boolean value true if it inserts the specified element in this collection.

Array List - Collections Framework in Java - DSA

Gist: An array list can store individual objects by following insertion order, here the initial capacity is 10 by default but can be modified as per the requirement, once the array list reaches its load factor then internal all the elements of the current array is copied to a new array with the new capacity and the reference variable will now be referring to this new array list and the old array will be dealt by the garbage collector. Hierarchical order Type of constructors Empty argument constructor or the default constructor is the same as invoking any other object here a new ArrayList is created with a default size of 10. Below is the most commonly used constructor by beginners and others alike. ArrayList array = new ArrayList(); //array has a capacity of 10 The default constructor above will allot only 10 slots but if you want the initial size to be 20 or 1000 you can do so with the following constructor this is ideal w