Found 33676 Articles for Programming

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

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

104 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

187 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

122 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

199 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

272 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

Java Program to create a new list with values from existing list with Function Mapper

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

144 Views

To create a new list with values from fields from existing list with Function Mapper, the following is an example. Here, we have a class Employee −class Employee {    private String emp_name;    private int emp_age;    private String emp_zone; }The following is an example that creates a new list from existing with Function Mapper −Example Live Demoimport java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; public class Demo {    public static void main(String args[]) {       List < Employee > emp = Arrays.asList(new Employee("Jack", 29, "South"), new Employee("Tom", 24, "North"),          new Employee("Harry", 35, ... Read More

Java program to add and remove elements from a set which maintains the insertion order

karthikeya Boyini
Updated on 19-Nov-2024 18:23:22

585 Views

In this article, we will learn how to use a LinkedHashSet in Java to add and remove elements while maintaining the insertion order. You will see how elements are stored, removed, and updated in a LinkedHashSet without altering the sequence in which they were added. Problem Statement Write a Java program to add elements to a LinkedHashSet, remove specific elements, and display the updated set while ensuring the insertion order of the elements remains unchanged. Below is a demonstration of the same − Input Set = [20, 60, 80, 120, 150, 200, 220, 260, 380] Output Set = [20, 60, ... Read More

Advertisements