Found 9150 Articles for Object Oriented Programming

How to retain elements from a Collection in another Collection

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

141 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

580 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

189 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

123 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

200 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

304 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

How to create Java Priority Queue to ignore duplicates?

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

1K+ Views

The simplest way to create a Java Priority Queue to ignore duplicates is to first create a Set implementation −HashSet set = new HashSet (); set.add(100); set.add(150); set.add(250); set.add(300); set.add(250); set.add(500); set.add(600); set.add(500); set.add(900);Now, create Priority Queue and include the set to remove duplicates in the above set −PriorityQueuequeue = new PriorityQueue(set);Example Live Demoimport java.util.HashSet; import java.util.PriorityQueue; public class Demo {    public static void main(String[] args) {       HashSetset = new HashSet();       set.add(100);       set.add(150);       set.add(250);       set.add(300);       set.add(250);       set.add(500); ... Read More

How to create a read-only list in Java?

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

274 Views

Let us first create a List in Java −Listlist = new ArrayList(); list.add("A"); list.add("B"); list.add("C");Now to convert the above list to read-only, use Collections −list = Collections.unmodifiableList(list);We have converted the above list to read-only. Now, if you will try to add more elements to the list, then the following error would be visible −Exception in thread "main" java.lang.Error: Unresolved compilation problem:Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       Listlist = new ArrayList();       list.add("A");       list.add("B");       list.add("C");   ... Read More

Java program to create a new list with values from existing list with Lambda Expressions

karthikeya Boyini
Updated on 12-Aug-2024 23:13:48

3K+ Views

In this article, we will demonstrate how to create a new list of employee names from an existing list of Employee objects using Lambda Expressions. We will utilize Java’s Stream API to efficiently map the employee data and collect it into a new list. Lambda Expressions: Lambda expressions simplify functional programming by working with functional interfaces, which have only one method. A lambda expression provides a way to implement this method easily. Steps Following are the steps to create a new list with values from existing list with Lambda Expressions − Import the necessary classes ... Read More

Advertisements