Tag: collections

Why StringBuffer/StringBuilder should be avoided in HashMap?

Java
Why StringBuffer/StringBuilder should be avoided in HashMap?   This is one of the core java interview question under collections category. Generally in all the map manipulations we use string and avoid stringbuffer and stringbuilders, because both are mutable. What is the issue if is mutable ? If you are keeping your hashmap's key/value as stringbuilder/stringbuffer then once after you inserted the value in the map, if any of the key/value modified then it actually affects inside the map as well, even if you won't perform the put operation with the modified value. Because stringbuffer/stringbuilder is mutable, it is actually modifying or changing the value in all the places, so it involves high risk to use these as key/values in any collections.     HashM...

Java Collection Framework (As per Java 8) All Covered in One Image

Java
Java Collection Framework (As per Java 8) All Covered in One Image: Even many experienced java developers know only list/set/map and few know vector hashtable and queues. But so much more than that are actually there in the java collection framework. I am publishing the single java collection framework image to understand the complete collection hierarchy interface/sub interface and its implementation classes. You can refer this for high-level collection java hierarchy, this is created based java 8 official oracle documention available online. Many top tutorial websites also did not cover all the collections available in the below diagram, hopefully it helps everyone!       Reference Link:  Java 8 Collection Oracle Doc    

5 Ways to Remove Null from ArrayList

Java
5 Ways to Remove Null from ArrayList: Removing null from arraylist become some realtime business scenario many times. Removing null from arraylist is actually easy as it can be. We have many ways to remove null from arraylist.   5 Ways to Remove Null From ArrayList: Using collections.singleton Using removeAll with one null value list Using Iterator Using while loop (rarely used) Using removeIf (rarely used)   Below program is shared with all the above 5 ways to remove null from arraylist. By understanding all these five ways you can get better understanding about the arraylist collection and it's uses.   If you are around 5+ years experience then you will be expected to solve these things at your core java interviews.   5 Ways to Rem
Iterator in Java with Examples

Iterator in Java with Examples

Java
Iterator: Iterator is an object in general to enable the traverse to any collection. Iterator can be used in java to iterate List/Set/Map interfaces or List/Set/Map type of objects. [eg: ArrayList/LinkedList/HashSet/LinkedHashSet/TreeSet/LinkedHashMap/HashMap etc..]   Iterator has below limitations: Iterator can be used to traverse only in forward direction. No set() method:so we can not set a value of the collection using iterator. No previousIndex()/nextIndex() method: Getting next index value/previous index value is not possible with iterator. No add() method: we can not add anything using iterator to our collection objects, where as ListIterator provides add() method to add the elements to lists. Note: All these limitations can be resolved with ListIterator. ...
ListIterator in Java with Examples

ListIterator in Java with Examples

Java
ListIterator: List Iterator can be used to iterate List interfaces/List type of objects. [eg: ArrayList/LinkedList], But it can not be used to iterate Set/Map Interfaces. ListIterator can be used to iterate in both forward and backward/reverse direction, where as in Iterator backward/reverse direction iteration is not possible.   Available methods in ListIterator:   add() set() remove() previousIndex() nextIndex() next() hasNext() previous() hasPrevious()   add(): Even after the listiterator created for the list, we can add the elements to the list using add() method. This can also be used inside the while loop. Listiterator add() method example: [java] List<String> myList = new ArrayList<String>(); myList.add("Go...

Java Program to check the string word contains with map keys

Java
Java Program to check the string word contains with map keys:   Sometimes we need to check the each and every word of a particular string is contains with a map or list or set values for some of our business needs. Below program will help you to achieve the similar functionality. Input String is having some words, if the same words matches with the map keys then we are taking and keeping in separate arraylist. This list will have only the contains string words. Program: [java] package in.javadomain; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class WordSplit { Map<String, ArrayList<String>> mainMap = new HashMap<String, ArrayList<String>>(); ArrayList<Stri...

List Iteration using Lambda Expression in Java 8

Java
Lambda Expression: Lambda expressions are introduced in Java 8 and I feel its a great feature. Here we are going to see how to iterate the array list using Lambda expression.   List Iteration using Lambda Expression: [java] import java.util.ArrayList; import java.util.List; import java.util.Arrays; public class LambdaIteration{ public static void main(String[] args){ List<String> progLangLst = new ArrayList<String>(); progLangLst.add("C"); progLangLst.add("C++"); progLangLst.add("Java"); progLangLst.add("PHP"); progLangLst.add(".Net"); // old way [from java 1.5] System.out.println("Printing the list values - old way"); for(String progLang : progLangLst){ System.out.print...
Sorting irrespective of case in java

Sorting irrespective of case in java

Java
Sorting irrespective of case in java: In Java if you want to sort the elements then you can use treeset. Set will not allow duplicates, but if you have the same element in both small and upper case then it will consider that as separate elements.   Sorting respective of case: [java] import java.util.Set; import java.util.TreeSet; public class Sorting { /** * @param args */ public static void main(String[] args) { Set<String> mySet = new TreeSet<String>(); mySet.add("Vizag"); mySet.add("arcot"); mySet.add("Vellore"); mySet.add("delhi"); mySet.add("Bangalore"); mySet.add("chennai"); for (String string : mySet) { System.out.println(string); } } } [/java]   Output...

Arraylist vs LinkedList Java

Java
ArrayList Vs LinkedList: ArrayList: 1. Search operation is faster, because it maintains the array structure, so get(index) will search and give the value fastly. 2. Less memory consumption because it maintains element and data. Where in linkedlist it maintains two pointer to store the addresses of the neighbour elements. LinkedList: 1. Remove operation is faster. 2. Insertion operation is faster. Because it is doubly linked list, so maintains both the address, where as in arraylist it need to shift the deleted area with the neighbour values. When to use ArrayList: Arraylist is recommended if we use search operation more. (get(index) function). When to use LinkedList: LinkedList is recommended if we use add/remove operations more. (add() and remove() function). ...

Finding Common values in Arraylist Java Example

Java
Finding Common values in Arraylist: Two arraylists are iterated one inside another for loop and common values are inserted in the common arraylist. Input: List 1 contains: [plain gutter="false"]God .Net PHP Java [/plain] List 2 contains: [plain gutter="false"]C Java C++ .Net [/plain] Expected Values: [plain gutter="false"] Common values are :::Java Common values are :::.Net [/plain] Java Program to Find Common values in Arraylist: [java gutter="false"] import java.util.ArrayList; import java.util.List; public class FindCommonValue { public static void main(String[] args) { List<String> myList1 = new ArrayList<String>(); myList1.add("God"); myList1.add(".Net"); myList1.add("PHP"); myList1.ad...