Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 250 of 589
How to fetch elements with iterator in Java?
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()); }Exampleimport 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 MoreJava Program to get previous and next index using ListIterator
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();Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo { public static void main(String[] args) { ArrayListarrList = new ArrayList(); arrList.add(100); ...
Read MoreHow to remove all elements from a Collection in another Collection
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);Exampleimport 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 MoreHow to retain elements from a Collection in another Collection
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);Exampleimport 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 MoreHow to test if a List is an Unmodifable List in Java?
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" );Exampleimport java.util.Collections; import java.util.LinkedList; import java.util.List; public class Demo { public static void main(String[] args) { Listlist = new LinkedList(); ...
Read MoreProgram to iterate over a List using Java 8 Lambda
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));Exampleimport 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..."); for ...
Read MoreHow to convert Float array list to float array in Java?
Let us first create a float array list −ArrayList < Float > arrList = new ArrayList < Float > (); arrList.add(5.2 f); arrList.add(10.3 f); arrList.add(15.3 f); arrList.add(20.4 f);Now, convert the float array list to float array. At first, we have set the same size to float array i.e. the same number of elements. After that, we have assigned each value −final float[] arr = new float[arrList.size()]; int index = 0; for (final Float value: arrList) { arr[index++] = value; }Exampleimport java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayListarrList = new ArrayList(); ...
Read MoreHow can I generate random booleans in Java?
To generate random booleans like TRUE or FALSE, at first create a new Random object −Random randNum = new Random();Now, loop through the count of booleans you want and generate random booleans with nextBooleans() method −for (int i = 1; i
Read MoreJava Program to find keys from a Linked HashMap and store it in a list
Let us first create a LinkedHashMap with key-value pair −Mapmap = new LinkedHashMap(); map.put("1", "Katie"); map.put("2", "Peter"); map.put("3", "Amy"); map.put("4", "Kane"); map.put("5", "Colin"); map.put("6", "Andre"); map.put("7", "Pollard"); map.put("8", "David"); map.put("9", "Jofra"); map.put("10", "Akila");Now, create a new List and store the keys in it for the above Map −Listlist = new ArrayList(); list.addAll(map.keySet());Exampleimport java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class Demo { public static void main(String[] args) { Mapmap = new LinkedHashMap(); map.put("1", "Katie"); map.put("2", "Peter"); map.put("3", "Amy"); map.put("4", "Kane"); ...
Read MoreHow to fill multiple copies of specified Object to a List in Java
To fill multiple copies of specified object to a list means let’s say you have an element 100 and want to display it 10 times. For this, let us see an example.The following is our list and iterator. We have used nCopiec Collections method to set the elements and how many copies you want −Listlist = Collections.nCopies(10, 100); Iteratoriterator = list.iterator();After that display the multiple copies −while (iterator.hasNext()) System.out.println(iterator.next());Exampleimport java.util.Collections; import java.util.Iterator; import java.util.List; public class Demo { public static void main(String[] args) { Listlist = Collections.nCopies(10, 100); Iteratoriterator = list.iterator(); ...
Read More