JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label Java collection tutorial. Show all posts
Showing posts with label Java collection tutorial. Show all posts

Difference between IdentityHashMap, WeakHashMap and EnumMap in Java

4:18 AM
IdentityHashMap vs WeakHashMap vs EnumMap in Java 1) The fundamental difference between IdentityHashMap and other Map implementations e.g. HashMap, Hashtable, WeakHashMap or EnumMap it uses equality operator (==) to search and get the value back. If you know how to get method of Map works the know that other Map implementation, which uses equals() method of the key object for that purpose. Since == operator only return true if the reference variables point to the same object, it's not possible to get the value with a different object even if it appear equal in all field. You must hold the reference of the key object outside the IdentityHashMap to get the value back. Another consequence of using == operator instead of equals() method is that there would be less collision compared to other Map implementations e.g. HsahMap. See Java Performance The Definitive Guide By Scott Oaks to learn more about the performance impact of collisions in HashMap. 2) The fundamental differenc

Difference between Iterator and ListIterator in Java?

4:15 AM
The Iterator is the standard way to traverse a collection in Java. You can use Iterator to traverse a List, Set, Map, Stack, Queue or any Collection, but you might not know that there is another way to traverse over List in Java? Yes, it's called the ListIterator. There are many differences between Iterator and ListIterator in Java, but the most significant of them is that Iterator only allows you to traverse in one direction i.e. forward, you have just got a next() method to get the next element, there is no previous() method to get the previous element. On the other hand, ListIterator allows you to traverse the list in both directions i.e. forward and backward. It has got both next() and previous() method to access the next and previous element from List. Unfortunately, ListIterator only supports the List interface, you cannot use it to traverse over Map or Set in Java. Iterator vs ListIterator You can differentiate between Iterator and ListIterator on following topics

How to sort ArrayList ascending descending order

4:07 AM
How to sort ArrayList ascending descending order  Sorting ArrayList in Java is not difficult, by using Collections.sort() method you can sort ArrayList in ascending and descending order in Java. Collections.sort() method optionally accept a Comparator and if provided it uses Comparator's compare method to compare Objects stored in Collection to compare with each other, in case of no explicit Comparator, Comparable interface's compareTo() method is used to compare objects from each other. If object's stored in ArrayList doesn't implements Comparable than they can not be sorted using Collections.sort() method in Java. Sorting ArrayList in Java – Code Example ArrayList Sorting Example in Java - Ascending Descending Order Code Here is a complete code example of How to sort ArrayList in Java, In this Sorting we have used Comparable method of String for sorting String on there natural order, You can also use Comparator in place of Comparable to sort String on any o

difference and Similarities between Hashtable and HashMap in Java

4:02 AM
Similarities between Hashtable and HashMap in Java 1) Both Hashtable and HashMap implements java.util.Map interface. 2) Hashtable and HashMap both are a hash based collection and works on the principle of hashing. 3) Hashtable and HashMap both provide constant time performance for put and get method if objects are distributed uniformly across bucket. 4) From JDK 4 both Hashtable and HashMap are part of Java collection framework. Difference between Hashtable and HashMap in Java 1) First and most significantly different between Hashtable and HashMap are that HashMap is not thread-safe  while Hashtable is a thread-safe collection. 2) The second important difference between Hashtable and HashMap is performance since HashMap is not synchronized it perform better than Hashtable. 3) The third difference on Hashtable vs HashMap is that Hashtable is obsolete class and you should be using ConcurrentHashMap in place of Hashtable in Java.

Difference between HashSet and HashMap in Java

4:00 AM
Similarities on HashMap and HashSet in Java 1) Both HashMap and HashSet are a hash based collection in Java. 2) Both HashMap and HashSet are not synchronized and can not be shared between multiple threads. 3) Iterator returned by HashMap's keySet() and HashSet are fail-fast and they throw ConcurrentModificationException if they detect any structural change in Collection. 4) Both HashMap and HashSet provided constant time performance for basic operations like put(), get() etc. 5) Both HashSet and HashMap allows null values. Differences between HashSet and HashMap in Java 1) The first and most significant difference between HashMap and HashSet is that HashMap is an implementation of Map interface while HashSet is an implementation of Set interface, which means HashMap is a key value based data-structure and HashSet guarantees uniqueness by not allowing duplicates.In reality, HashSet is a wrapper around HashMap in Java, if you look at the code of add(E e) method

Java Program to convert String ArrayList to String Array and How to convert String ArrayList to Array in Java

3:57 AM
Java Program to convert String ArrayList to String Array Converting String ArrayList into String array is very common programming task in Java. you often need to convert Array to Array List in Java  and vice-versa. In this Java program, we will How to convert String ArrayList to String array. This is also a common programming exercise which is asked too many Java programmers in various Java related courses. It’s also worth noting that ArrayList in Java is internally backed by array and Array in Java are objects much like String which is also an Object in Java. In this Java program, we first create an ArrayList which stores name of months as String e.g. Jan, Feb, and Mar. Later we use ArrayList  toArray() method to convert ArrayList into an array. How to convert String ArrayList to Array in Java How to convert String ArrayList to String Array in JavaHere is full code example of Java test program which convert String ArrayList to String Array in Java.In this Java program we fir

Difference between CopyOnWriteArrayList and ArrayList in Java.

2:50 AM
CopyOnWriteArrayList and ArrayList in Java.  we have seen What is CopyOnWriteArrayList in Java and How it achieves thread-safety by creating a separate copy of List for each writes operation. Now let's see Some difference between ArrayList and CopyOnWriteArrayList in Java, which is another implementation of List interface : 1) First and foremost difference between CopyOnWriteArrayList and ArrayList in Java is that CopyOnWriteArrayList is a thread-safe collection while ArrayList is not thread-safe and can not be used in the multi-threaded environment. 2) The second difference between ArrayList and CopyOnWriteArrayList is that Iterator of ArrayList is fail-fast and throw ConcurrentModificationException once detect any modification in List once iteration begins but Iterator of CopyOnWriteArrayList is fail-safe and doesn't throw ConcurrentModificationException. 3) The third difference between CopyOnWriteArrayList vs ArrayList is that Iterator of former doesn

What is CopyOnWriteArrayList or CopyOnWriteArrayList vs Array List in Java-Tutorial

2:48 AM
CopyOnWriteArrayList vs Array List in Java CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrency API along with its popular cousin ConcurrentHashMap in Java. CopyOnWriteArrayList implements List interface like ArrayList, Vector, and LinkedList but its a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class. As the name suggests CopyOnWriteArrayList creates a copy of underlying ArrayList with every mutation operation e.g. add, remove, or when you set values. That's why it is only suitable for a small list of values which are read frequently but modified rarely e.g. a list of configurations. Normally CopyOnWriteArrayList is very expensive because it involves costly Array copy with every writes operation but it's very efficient if you have a List where Iteration outnumbers mutation e.g. you mostly need to iterate the ArrayList and don't modify it too often

Difference between HashMap and ConcurrentHashMap in Java Collection

2:14 AM
Difference between HashMap and ConcurrentHashMap in Java In this section, we will see some more details about HashMap and ConcurrentHashMap and compare them on various parameters like thread-safety, synchronization, performance, ease of use etc. 1)  The significant difference between HashMap and ConcurrentHashMap is that later is thread-safe and can be used in a concurrent environment without external synchronization. Though it doesn't provide the same level of synchronization as achieved by using Hashtable but it's enough for the most practical purpose. 2)You can make HashMap synchronized by wrapping it on Collections.synchornizedMap(HashMap) which will return a collection which is almost equivalent to Hashtable, where every modification operation on Map is locked on Map object while in case of ConcurrentHashMap, thread-safety is achieved by dividing the whole Map into different partition based upon Concurrency level and only locking particular portion instead of loc

Difference between a List and ArrayList Reference Variable in Java?

8:56 AM
Someone who is just starting with Java programming language often has doubt about how we are storing an ArrayList object in List variable, what is the difference between List and ArrayList? or why not just store the ArrayList object in ArrayList variable just like we do for String, int, and other data types. Well, the main difference between List and ArrayList is that List is an interface while ArrayList is a class. Most importantly, it implements the List interface, which also means that ArrayList is a subtype of List interface. In Java or any object oriented language, super type of variable can store an object of subtype. This is known as Polymorphism because any virtual method will be executed from subclass only, even though they were called from super type. This is the beginning, now let's see those two questions as well. Why store ArrayList object on List variable? You might have seen something like this: List<Movie> listOfMovies = new ArrayList<Movie>()

Producer Consumer problem Solution using BlockingQueue in Java Thread

1:14 AM
Producer Consumer Solution using BlockingQueue in Java Thread Producer Consumer problem is one of the classic multi-threading problems in computer science and the multi-threading world. It's tricky because it involves inter-thread communication, but it's important because most of the multi-threading problems fits into this category. There are many ways to solve producer consumer problem in Java e.g. you can solve this by using wait() and notify() method, as discussed here, or you can use the Semaphore to solve this problem. In this article, you will learn a third way to solve the producer-consumer problem by using the BlockingQueue in Java. It is arguably the simplest way to solve this problem in any programming language because blocking queue data structure not only provides storage but also provides flow control and thread-safety, which makes the code really simple. Brian Goetz has also explained this key class and pattern in his classic Java Concurrency in Practice book,

.