Java Collections
12 SeptemberTreeSet in java
In this post, we will see TreeSet in java. Java TreeSet have following properties: It can contain only unique element. It stores objects in ascending order by default, It implements NavigableSet interface which extends SortedSet. When you put objects in TreeSet, it must implement Comparable interface. Lets understand it more with the help of example. […]
19 AugustJava Timer example
Timer is an utility class which can be used to schedule tasks on specific time or repeatedly. Lets say, you are developing an banking application and there is need to process the data at 7 PM evening daily. You can easily schedule task using Timer class. For creating a task, you need to extends TimerTask […]
06 AugustHow to remove duplicates from ArrayList in java
In this post, we will see how to remove duplicate elements from ArrayList in java. There are many ways to do it. Some of them are: Using iterative approach Using HashSet (but does not maintain insertion order) Using LinkedHashMap Program: package org.arpit.java2blog.algo; import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.*; public class RemoveDuplicatesArrayListMain { /* […]
25 JuneHow to iterate over Map or HashMap in java
In this post, we will see how can we iterate a map in java. There are four ways of iterating over a map, HashMap or TreeMap. Java HashMap tutorial: HashMap in java How HashMap works in java hash and indexfor method in HashMap hashcode and equals method in java How to sort HashMap by keys […]
24 JuneHow to iterate a list in java
In this post, we will see how can we iterate a list in java. There are four ways of iterating over a list. For loop For each loop(Java 5) While loop Iterator Below example will help you to understand, how to iterate list in java. I am taking custom object list to understand better 1. […]
17 JuneDifference between ArrayList and LinkedList in java
One of the common interview question is “What is difference between ArrayList and LinkedList”.Before we actually see differences,let me give you brief introduction of both. ArrayList ArrayList is implementation of list interface. ArrayList is not synchonized(so not thread safe) ArrayList is implemented using array as internal data structure.It can be dynamically resized . LinkedList LinkedList […]
19 JulyHow HashSet works in java
In this post, we will see about Hashset in java Java HashSet: Part-1:HashSet in java Part-2:Difference between HashMap and HashSet Part-3:hashcode and equals method in java This is one of the frequently asked question in core java interview so in this post, we will see how HashSet works in java.We have already seen How hashMap […]
18 JuneComparable in java
In this post, we will see how you can use comparable to sort list of objects in java. Comparable interface: Class whose objects to be sorted must implement this interface.In this,we have to implement compareTo(Object) method. For example: public class Country implements Comparable{ @Override public int compareTo(Country country) { return […]
17 JuneComparator in java
In this post, we will see how you can use comparator to sort list of objects in java. Comparator: When you want to sort the list of objects of a class,you can use Comparator interface. You don’t need to implement Comparator on the class whose objects need to be sorted. You can create a separate […]