Java Collections
- 30 May
Java ArrayList indexOf example
ArrayList‘s indexOf method is used to find out first index of object in arraylist. indexOf method takes object as argument and returns first occurrence of specified element. Methods: public int indexOf(Object o) returns index of first occurrence of element in the ArrayList. indexOf method return -1 if object is not present in the ArrayList ArrayList […]
- 18 May
ArrayList in java
ArrayList in java is most common Collections data structure along with HashMap which we use very often. 1. Why to choose ArrayList vs Array: Array is fixed length data structure If array is full , you can not add element to it, where as ArrayList in java can dynamically grow and shrink as per our […]
- 01 May
How to sort HashMap in java by keys and values
HashMap does not preserve order of element but what if you want to sort it by keys or values. In this post, we will see how to sort HashMap by keys or values. Java HashMap tutorial: HashMap in java HashMap internal working hash and indexfor method in HashMap hashcode and equals in java sort HashMap […]
- 01 May
LinkedHashSet in java
In this post, we will see about LinkedHashSet in java. LinkedHashSet is same as HashSet except that it maintains insertion order. Some points about LinkedHashSet LinkedHashSet implements Set interface and extends HashSet class. LinkedHashSet maintains insertion order, so when you will be able to access elements in the order they were inserted like ArrayList. Example: LinkedHashSetMain.java [crayon-6767cb18ca1b3307614455/] When you run […]
- 29 April
HashMap in java
HashMap is hash table based implementation of Map interface. It stores entry in key-value pairs. It maps keys to values. It is one of the most used Collection. Java HashMap HashMap implements Map an interface that maps a key to value. It is not synchronized and is not thread-safe. Duplicate keys are not allowed One […]
- 29 April
TreeMap in java with examples
TreeMap class implements Map similar to HashMap. Some important points about TreeMap: TreeMap implements Map interface and extends HashMap class. TreeMap is implemented using Red black tree based NavigableMap. TreeMap is ordered collection and store its elements in natural ordering of keys. Key which you would like to put in TreeMap must implement Comaparable interface or you can […]
- 28 April
LinkedHashMap in java
LinkedHashMap is a Hashtable and linked list-based implementation of Map interface, with predictable insertion order. It maintains double linked list of all its entries, that’s how it differs from HashMap. Java LinkedHashMap Some points about LinkedHashMap LinkedHashMap implements Map interface and extends HashMap class. LinkedHashMap maintains insertion order, so when you will be able to […]
- 12 April
Java Collections interview questions and answers
In this post, I am going to share some of logical java Collections interview questions which will help to understand Collections concepts better. For an extensive list of java programs, please go to Java interview programs. Question 1: What can be output of below code : [crayon-6767cb18cc218164277741/] options are :Â A)France France India B)India India […]
- 12 September
Difference between Iterator and ListIterator in java
In this post, we will see difference between Iterator and ListIterator in java. They are both used for traversal but it is good to point out differences between them. Iterator vs ListIterator: Parameter Iterator ListIterator Traversal Iterator can be used to traverse List,set, Queue ListIterator can be used to traverse only List. Traversal direction Iterator can […]