• 20 October

    Java Array to Set

    In this post, we will learn java array to set conversion. There are many ways to convert array to set. 1. Using Java 8’s Stream If you are using Java 8, I would recommend using Java 8 Stream. [crayon-61431a3338a2d883287672/] Output [John, Martin, Mary] 2. Using HashSet constructor() We can directly call HashSet‘s constructor for java […]

  • Java Set to Array
    12 May

    Java Set to Array

    In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s Stream If you are using Java 8, I would recommend using Java 8 Stream. [crayon-61431a3339909594339431/] Output [John, Martin, Mary] 2. Using toArray() We can directly call toArray() method on set […]

  • 10 August

    Java Collection to List

    In this post, we will see how to convert Java Collection to List. There are many ways to do it, we will see two ways here. Using ArrayList constructor [crayon-61431a3339cde913030001/] Above ArrayList‘s constructor creates a list having the elements of the specified collection, in the order they are returned by the collection’s iterator. Using Java […]

  • 09 August

    Java Collections shuffle

    Shuffle method is to permute passed list with default or specified randomness. Syntax [crayon-61431a333a684004340230/] Example : Let’s see both the function with the help of example. [crayon-61431a333a68a062597863/] When you run above program, you will get below output: ========================== Before shuffling [John, Martin, Mohan, Mary, Tom] ========================== After shuffling [Mohan, Martin, Mary, Tom, John] ========================== After shuffling […]

  • 24 May

    Collections in java

    In this tutorial, we will see  about Collections in java. Collection framework is core part of java programming language. It is used in almost all the applications. Collections framework provides a way to store and retrieve the collection of elements. This framework has many interfaces and classes which makes programmer life easy. Let’s go through […]

  • 04 November

    How to create list of lists in java

    In this posts, we will see how to create a list of lists in java. You can easily create a list of lists using below syntax List<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>(); or ArrayList<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>(); One of the Usecase This is generally useful when you are trying to read a CSV file and […]

  • 25 September

    List vs ArrayList in java

    In this post, we will see the difference between List and ArrayList in Java. List is an interface where ArrayList is concrete implementation, so List is more generic than ArrayList. You can instance an ArrayList in below two ways.You might have seen this code before. [crayon-61431a333bb71350914249/] If you use 1st way, you can change the […]

  • 24 September

    How to convert List to Set in java

    In this post, we will see how to convert List to Set in java.We will convert ArrayList to HashSet. As HashSet does not allow duplicates, when you convert ArrayList to HashSet, all the duplicates will be discarded. You can simply use the constructor of HashSet to convert ArrayList to HashSet. [crayon-61431a333c43e160147642/] Here is simple example: […]

  • 30 May

    Java ArrayList indexOf example

    ArrayList‘s indexOf method is used to find out first index of object in arraylist. indexOf method takes object as argument and returns first occurrence of specified element. Methods: public int indexOf(Object o) returns index of first occurrence of element in the ArrayList. indexOf method return -1 if object is not present in the ArrayList ArrayList […]