Showing posts with label Collection Framework. Show all posts
Showing posts with label Collection Framework. Show all posts

Sunday, December 3, 2017

Set vs List: When to use Set instead of List?

A Set cannot contain duplicate elements while a List can. A List (in Java) also implies order. Conceptually we usually refer to an unordered grouping that allows duplicates as a Bag and doesn't allow duplicates is a Set. List is used for the collection of elements with duplicates.
For more, you can go through this link: What is the difference between Set and List?

When to use List, Set and Map in Java?

1) If you do not want to have duplicate values in the database then Set should be your first choice as all of its classes do not allow duplicates.
2) If there is a need for frequent search operations based on the index values then List (ArrayList) is a better choice.
3) If there is a need of maintaining the insertion order then also the List is a preferred collection interface.
4) If the requirement is to have the key & value mappings in the database then Map is your best bet.

Tuesday, November 8, 2016

Java Collection Properties One by One



  1. The nature of Sets that there are no duplicates.
  2. The advantage of using a TreeSet is that it not only remove the duplicates, but also sorts the Data in Ascending order.
  3. The HashSet does not preserve the ordering the Set, so it can be used if you are not interested in ordering.
  4. The LinkedHashSet keeps the original ordering of the Set. So it is advisable to be used if you want to keep the original ordering of your Set!

Monday, August 29, 2016

Collections Single Tweak

 Q1: What is the difference between ArrayList and Vector ?
Ans: 
Vector is synchronized while ArrayList is not . Vector is slow while ArrayList is fast. Every time when needed, Vector increases the capacity twice of its initial size while ArrayList increases its ArraySize by 50%.

Q2 What is the difference between HashMap and Hashtable ?
Ans:
a. HashMap allows one null key and any number of null values while Hashtable does not allow null keys and null values.
b. HashMap is not synchronized or thread-safe while Hashtable is synchronized or thread-safe.

Q3 What is the difference between Iterator and ListIterator.
Ans:
Using Iterator we can traverse the list of objects in forward direction . But ListIterator can traverse the collection in both directions that is forward as well as backward.


Q4 What is the difference between HashSet and TreeSet ?
Ans:
a.  HashSet maintains the inserted elements in random order while TreeSet maintains elements in the sorted order
b. HashSet can store null object while TreeSet can not store null object.


Q5 What is the difference between HashMap and ConcurrentHashMap ?Ans:
a. HashMap is not synchronized while ConcurrentHashMap is synchronized.
b. HashMap can have one null key and any number of null values while ConcurrentHashMap does not allow null keys and null values .




Sunday, August 28, 2016

TreeMap how does it sort??

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

It is not only alphabetical, but it is also upper/down case sensitive.
TreeMap<String, Integer> treemap = new TreeMap<String, Integer>();
treemap.put("Lol", 1);
treemap.put("Marc", 2);
treemap.put("Jesper", 3);
treemap.put("lol1", 1);
treemap.put("marc1", 2);
treemap.put("jesper1", 3);
Output:
Jesper = 3
Lol = 1
Marc = 2
jesper1 = 3
lol1 = 1
marc1 = 2
So, if you don't need it, you can use your custom comparator, and compare string in lower case:
TreeMap<String, Integer> treemap = new TreeMap<String, Integer>(new Comparator<String>() {
    public int compare(String o1, String o2) {
        return o1.toLowerCase().compareTo(o2.toLowerCase());
    }
});
treemap.put("Lol", 1);
treemap.put("Marc", 2);
treemap.put("Jesper", 3);
treemap.put("lol1", 1);
treemap.put("marc1", 2);
treemap.put("jesper1", 3);
Output:
Jesper = 3
jesper1 = 3
Lol = 1
lol1 = 1
Marc = 2
marc1 = 2

Resource Link:

http://stackoverflow.com/a/13642719/2293534