Found 4334 Articles for Java 8

How to retain elements from a Collection in another Collection

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

77 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

453 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

68 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

103 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 30-Jul-2019 22:30:25

61 Views

To display the sub-list of ArrayList, let us first declare an ArrayList and add some elements −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");Get the sub-list of ArrayList now. Here, we are getting sub-list from array index 4 to 8 −Listlist = arrayList.subList(4, 8);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"); ... Read More

Java Program to get the number of hours in this duration

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

144 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

257 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

946 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

204 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 30-Jul-2019 22:30:25

2K+ Views

To create a new list with values from existing list with Lambda Expressions, the following is an example.Here, we are displaying the name of Employees. Therefore, we have created an Employee class as well −Listemp = Arrays.asList(new Employee("Jack", 29, "South"), new Employee("Tom", 24, "North"), new Employee("Harry", 35, "West"), new Employee("Katie", 32, "East"));Use Lambda to crate anew list from existing list with Lambda Expressions −Listres = emp.stream().map(u ->u.displayEmpName()).collect(Collectors.toList());Let us see the complete example −Example Live Demoimport java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; public class Demo {    public static void main(String args[]) {       Listemp = Arrays.asList(new ... Read More

Advertisements