Java Articles

Page 62 of 450

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

Samual Sam
Samual Sam
Updated on 11-Mar-2026 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" );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 More

Program to iterate over a List using Java 8 Lambda

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 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));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 More

How to convert Float array list to float array in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 3K+ Views

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 More

How can I generate random booleans in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 408 Views

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 More

Java Program to find keys from a Linked HashMap and store it in a list

Samual Sam
Samual Sam
Updated on 11-Mar-2026 219 Views

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 More

How to fill multiple copies of specified Object to a List in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 891 Views

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

Java Program to sort a List in case insensitive order

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

Let’s say your list is having the following elements −P, W, g, K, H, t, ETherefore, case sensitive order means, capital and small letters will be considered irrespective of case. The output would be −E, g, H, K, P, t, WThe following is our array −String[] arr = new String[] { "P", "W", "g", "K", "H", "t", "E" };Convert the above array to a List −Listlist = Arrays.asList(arr);Now sort the above list in case insensitive order −Collections.sort(list, String.CASE_INSENSITIVE_ORDER);Exampleimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] argv) throws Exception {       String[] ...

Read More

Java Program to remove an element from List with ListIterator

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 311 Views

Let’s say the following is our List with elements −ArrayList < String > arrList = new ArrayList < String > (); arrList.add("Jack"); arrList.add("Tom"); arrList.add("Brad"); arrList.add("Amy"); arrList.add("Ben"); arrList.add("Peter"); arrList.add("Katie"); arrList.add("Tim");Now, use the listIterator(). The next() method returns the next element in the List. Hoverer, remove an element using remove() method −ListIteratoriterator = arrList.listIterator(); iterator.next(); iterator.remove();Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add("Jack");       arrList.add("Tom");       arrList.add("Brad");       arrList.add("Amy");       arrList.add("Ben");       arrList.add("Peter");   ...

Read More

Remove duplicate elements in Java with HashSet

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 13K+ Views

Set implementations in Java has only unique elements. Therefore, it can be used to remove duplicate elements.Let us declare a list and add elements −List < Integer > list1 = new ArrayList < Integer > (); list1.add(100); list1.add(200); list1.add(300); list1.add(400); list1.add(400); list1.add(500); list1.add(600); list1.add(600); list1.add(700); list1.add(400); list1.add(500);Now, use the HashSet implementation and convert the list to HashSet to remove duplicates −HashSetset = new HashSet(list1); Listlist2 = new ArrayList(set);Above, the list2 will now have only unique elements.Exampleimport java.util.ArrayList; import java.util.HashSet; import java.util.List; public class Demo {    public static void main(String[] argv) {       Listlist1 = new ArrayList();   ...

Read More

Replace an element from a Java List using ListIterator

Samual Sam
Samual Sam
Updated on 11-Mar-2026 370 Views

Let us first create a Java List and add elements −ArrayList < String > list = new ArrayList < String > (); list.add("Katie"); list.add("Tom"); list.add("Jack"); list.add("Amy"); list.add("Andre"); list.add("Brad"); list.add("Peter"); list.add("Bradley");Now, use ListIterator and return the next element in the List with next() −ListIteratoriterator = list.listIterator(); iterator.next();Replace the element in the List with set() method. Here, whatever element is set will get replaced as the first element of the Iterator −iterator.set("Angelina");Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListlist = new ArrayList();       list.add("Katie");       list.add("Tom");   ...

Read More
Showing 611–620 of 4,496 articles
« Prev 1 60 61 62 63 64 450 Next »
Advertisements