Browsed by
Tag: collections

Why StringBuffer/StringBuilder should be avoided in HashMap?

Why StringBuffer/StringBuilder should be avoided in HashMap?

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,…

Read More Read More

748 total views, 2 views today

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

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

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…

Read More Read More

725 total views, 1 views today

5 Ways to Remove Null from ArrayList

5 Ways to Remove Null from ArrayList

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….

Read More Read More

969 total views, 2 views today

Iterator in Java with Examples

Iterator in Java with Examples

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…

Read More Read More

882 total views, no views today

ListIterator in Java with Examples

ListIterator in Java with Examples

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…

Read More Read More

817 total views, no views today

Java Program to check the string word contains with map keys

Java Program to check the string word contains with map keys

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…

Read More Read More

723 total views, 2 views today

List Iteration using Lambda Expression in Java 8

List Iteration using Lambda Expression in Java 8

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: 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.println(progLang); }…

Read More Read More

943 total views, no views today

Sorting irrespective of case in java

Sorting irrespective of case in 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: 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");…

Read More Read More

1,012 total views, 1 views today

Arraylist vs LinkedList Java

Arraylist vs LinkedList 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…

Read More Read More

827 total views, 1 views today

Finding Common values in Arraylist Java Example

Finding Common values in Arraylist Java Example

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: import java.util.ArrayList; import java.util.List; public class FindCommonValue { public static void main(String[] args) { List<String> myList1 =…

Read More Read More

8,236 total views, 1 views today