Java Collections
20 OctoberJava Array to Set
In this post, we will learn java array to set conversion. [toc] 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. package org.arpit.java2blog; import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; public class ArrayToSetMain { public static void main(String[] […]
10 AugustJava 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 ArrayList(Collection<? extends E> c) Above ArrayList‘s constructor creates a list having the elements of the specified collection, in the order they are returned by the collection’s […]
09 AugustJava Collections shuffle
Shuffle method is to permute passed list with default or specified randomness. Syntax public static void shuffle(List<?> list) or public static void shuffle(List<?> list,Random rnd) Example : Let’s see both the function with the help of example. package org.arpit.java2blog; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; public class ShuffleMain { public static void main(String[] args) […]
24 MayCollections 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 NovemberHow 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 SeptemberList 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. // 1st way List<String> list=new ArrayList<String>(); //2nd way ArrayList<String> arrayList=new […]
24 SeptemberHow 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. HashSet set=new HashSet(list); Here is […]
09 JuneHow to find length/size of Arraylist in java
In this tutorial, we will see how to find length/size of Arraylist in java. You can use ArrayList’size() method to calculate size of ArrayList. Let’s understand it with the help of Simple example. package com.arpit.java2blog; import java.util.ArrayList; import java.util.List; public class ArrayListLengthMain { public static void main(String args[]) { List<String> countryList=new ArrayList<>(); countryList.add("India"); countryList.add("China"); countryList.add("Bhutan"); […]
03 JuneConvert HashMap to ArrayList in java
In this post, we will see how to convert HashMap to ArrayList in java. Many times, you need to store keys or values into ArrayList or Store HashMap’s Node objects in ArrayList. [toc] HashMap and ArrayList both are the most used data structure in java and both have different Hierarchy. Here are the ways to […]