Found 7442 Articles for Java

Program to iterate over a List using Java 8 Lambda

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

3K+ Views

Let us first create a List and add elements −ArrayListarrayList = new ArrayList(); arrayList.add("100"); arrayList.add("200"); arrayList.add("300"); arrayList.add("400"); arrayList.add("500");Now, iterate over it with Lambda Expressions −ArrayListlist = arrayList; System.out.println("Iterating..."); list.stream().forEach(elem -> System.out.println(elem));Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       ArrayListarrayList = new ArrayList();       arrayList.add("100");       arrayList.add("200");       arrayList.add("300");       arrayList.add("400");       arrayList.add("500");       arrayList.add("600");       arrayList.add("700");       arrayList.add("800");       arrayList.add("900");       arrayList.add("1000");       System.out.println("ArrayList...");       ... Read More

How to test if a List is an Unmodifable List in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:25

1K+ Views

We will first set a list to unmodifiable and after that test if it is unmodifiable or not. Let us create a List and add elements −List list = new LinkedList (); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50);Set the above list to unmodifiable −Listunmodifiable = Collections.unmodifiableList(list);Now, use If-else, to check whether the list in unmodifiable or not −if (unmodifiable.getClass().getName().contains("UnmodifiableList"))    System.out.println("This is an UnmodifiableList" ); else    System.out.println("This is not an UnmodifiableList" );Example Live Demoimport java.util.Collections; import java.util.LinkedList; import java.util.List; public class Demo {    public static void main(String[] args) {       Listlist = new LinkedList();     ... Read More

How to shuffle a List in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

228 Views

First, create an Integer array −Integer[] strArray = new Integer[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };Now, convert it into a List −Listlist = Arrays.asList(strArray);Use Collections to shuffle as shown below −Collections.shuffle(list);Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String args[]) {       Integer[] strArray = new Integer[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };       Listlist = Arrays.asList(strArray);       System.out.println("List = "+list);       Collections.shuffle(list);       System.out.println("Shuffled List = "+list);    } }OutputList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Shuffled List = [100, 90, 40, 70, 20, 10, 80, 30, 60, 50]

How to retain elements from a Collection in another Collection

Samual Sam
Updated on 30-Jul-2019 22:30:25

143 Views

Let’s say the following is our Collection i.e. ArrayList −Listlist = new ArrayList(); list.add(100); list.add(200); list.add(200); list.add(200); list.add(300); list.add(400); list.add(400); list.add(500);Now, create another Collection −List list2 = new ArrayList(); list2.add(100); list2.add(200); list2.add(300); list2.add(400);To retain all elements from a Collection in another Collection, try the following with list and list2 as our two Collections −list.retainAll(list2);Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       Listlist = new ArrayList();       list.add(100);       list.add(200);       list.add(200);       list.add(200);       list.add(300);   ... Read More

How to remove all elements from a Collection in another Collection

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

582 Views

Let’s say the following is our Collection i.e. ArrayList −Listlist = new ArrayList(); list.add(100); list.add(200); list.add(200); list.add(200); list.add(300); list.add(400); list.add(400); list.add(500);Now, create another Collection −List list2 = new ArrayList(); list2.add(100); list2.add(200); list2.add(300); list2.add(400);To remove all elements from a Collection in another Collection, try the following with list and list2 as our two Collections −list.removeAll(list2);Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       Listlist = new ArrayList();       list.add(100);       list.add(200);       list.add(200);       list.add(200);       list.add(300);     ... Read More

Java Program to insert all elements of other Collection to specified Index of ArrayList

Samual Sam
Updated on 30-Jul-2019 22:30:25

105 Views

Let us first create an ArraList and add some elements to it −ArrayList < String > arr = new ArrayList < String > (); arr.add("50"); arr.add("100"); arr.add("150"); arr.add("200"); arr.add("250"); arr.add("300");Now, create a new collection. We are creating Vector here −Vectorvector = new Vector(); vector.add("500"); vector.add("700"); vector.add("800"); vector.add("1000");Now, we will append all the elements of the above Vector to our ArrayList beginning from index 3 −arr.addAll(3, vector);Example Live Demoimport java.util.ArrayList; import java.util.Vector; public class Demo {    public static void main(String[] args) {       ArrayListarr = new ArrayList();       arr.add("50");       arr.add("100");       arr.add("150"); ... Read More

Java Program to get previous and next index using ListIterator

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

190 Views

To get the exact evaluation of the previous and next index, you need use the next() method of the Iterator. This will eventually help you in gaining more understanding about Iterators. Let us first create an ArrayList and add some elements −ArrayList arrList = new ArrayList (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500); arrList.add(600); arrList.add(700); arrList.add(800); arrList.add(900); arrList.add(1000);Now, create a ListIterator −ListIteratoriterator = arrList.listIterator();Get the previous and next index now −iterator.previousIndex(); iterator.nextIndex();Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add(100);   ... Read More

Java program to display sub-list of ArrayList

Samual Sam
Updated on 14-Nov-2024 17:38:11

125 Views

In this article, we will learn how to print a sublist of an ArrayList in Java using the subList() method. The ArrayList class in Java provides a dynamic array for storing elements. The subList() method returns a portion of the list based on specified indices. Problem StatementGiven an ArrayList containing several elements, write a Java program to display a sub-list from the original list, starting from a specified index and ending at another specified index.Input [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]Output Sub-list from index 4 to 8:[500, 600, 700, 800] Steps to display a sub-list ... Read More

Java Program to get the number of hours in this duration

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

202 Views

At first, set the Duration −Duration duration = Duration.ofDays(25);Now, get the number of hours from the above Duration that has 25 days −duration.toHours()Example Live Demoimport java.time.Duration; public class Demo {    public static void main(String[] args) {       Duration duration = Duration.ofDays(25);       System.out.println("Hours in 25 days = "+duration.toHours());    } }OutputHours in 25 days = 600

How to fetch elements with iterator in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:25

307 Views

Let us first create a List and add elements −Listlist = Arrays.asList(new String[] { "P", "Q", "R", "S", "T", "U", "V", "W" });Now, use Iterator to fetch each element −Iteratori = list.iterator();Display the elements −while (i.hasNext()) {    System.out.println(i.next()); }Example Live Demoimport java.util.Arrays; import java.util.Iterator; import java.util.List; public class Demo {    public static void main(String[] a) {       Listlist = Arrays.asList(new String[] { "P", "Q", "R", "S", "T", "U", "V", "W" });       Iteratori = list.iterator();       System.out.println("Displaying elements...");       while (i.hasNext()) {          System.out.println(i.next());       } ... Read More

Advertisements