Found 4337 Articles for Java 8

Get the index of a particular element in an ArrayList in Java

George John
Updated on 13-Sep-2023 14:48:25

26K+ Views

The index of a particular element in an ArrayList can be obtained by using the method java.util.ArrayList.indexOf(). This method returns the index of the first occurrence of the element that is specified. If the element is not available in the ArrayList, then this method returns -1.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       List aList = new ArrayList();       aList.add("Orange");       aList.add("Apple");       aList.add("Peach");       aList.add("Guava");       aList.add("Mango");       ... Read More

NavigableMap pollFirstEntry() method in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:24

62 Views

The pollFirstEntry() method in NavigableMap remove and return a key-value mapping associated with the least key in this mapThe following is an example to implement pollFirstEntry() methodExample Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); n.put(15, ... Read More

Get the unmodifiable view of the specified ArrayList in Java

Arjun Thakur
Updated on 29-Jun-2020 13:42:25

396 Views

The unmodifiable view of the specified ArrayList can be obtained by using the method java.util.Collections.unmodifiableList(). This method has a single parameter i.e. the ArrayList and it returns the unmodifiable view of that ArrayList.A program that demonstrates this is given as followsExample Live Demoimport java.util.ArrayList; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       List aList = new ArrayList();       aList.add("Sally");       aList.add("George");       aList.add("John");       aList.add("Susan");       aList.add("Martha");       aList = Collections.unmodifiableList(aList);       System.out.println("The ... Read More

Insert all elements of other Collection to Specified Index of Java ArrayList

Ankith Reddy
Updated on 29-Jun-2020 13:43:20

267 Views

The elements of a Collection can be inserted at the specified index of the ArrayList using the method java.util.ArrayList.addAll(). This method has two parameters i.e. the specific index at which to start inserting the Collection elements in the ArrayList and the Collection itself.If there is an element already present at the index specified by ArrayList.addAll() then that element and all subsequent elements are shifted to the right to make the space for the Collection elements in the ArrayList.A program that demonstrates this is given as follow.Example Live  Demoimport java.util.ArrayList; import java.util.Vector; public class Demo {    public static void main(String[] args) ... Read More

NavigableSet Class ceiling() method in Java

George John
Updated on 30-Jul-2019 22:30:24

104 Views

The ceiling() method returns the least element greater than or equal to the given element i.e. 30 hereceiling(30)The following is an example to implement the ceiling method in JavaExample Live Demoimport java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100); System.out.println("Returned Value = " + set.ceiling(30)); } }OutputReturned Value = 40

Fetch elements of Java TreeSet using Iteration

Chandu yadav
Updated on 30-Jul-2019 22:30:24

97 Views

Use Iterator class to fetch elements of TreeSet.Create a TreeSet and add elements to itTreeSet set = new TreeSet(); set.add("13"); set.add("11"); set.add("12"); set.add("16"); set.add("19"); set.add("23"); set.add("21"); set.add("20"); set.add("30");Now, to display the elements, use Iterator classIterator i = set.iterator(); while(i.hasNext()){ System.out.println(i.next()); }The following is an example that fetch elements of TreeSet using IterationExample Live Demoimport java.util.*; public class Demo { public static void main(String args[]){ TreeSet set = new TreeSet(); set.add("13"); set.add("11"); ... Read More

Get the element ordered last in Java TreeSet

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

144 Views

To get the element ordered last i.e. the last element in TreeSet, use the last() method.Create a TreeSet and add elements to itTreeSet set = new TreeSet (); set.add("65"); set.add("45"); set.add("19"); set.add("27"); set.add("89"); set.add("57");Now, get the last elementset.last()The following is an example to get the element ordered last in TreeStExample Live Demoimport java.util.*; public class Demo { public static void main(String args[]){ TreeSet set = new TreeSet (); set.add("65"); set.add("45"); set.add("19"); ... Read More

Loop through ArrayList in Java

Ankith Reddy
Updated on 29-Jun-2020 13:35:59

687 Views

The elements of the ArrayList can be accessed one by one by using a for loop. A program that demonstrates this is given as followsExample Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Sun");       aList.add("Moon");       aList.add("Star");       aList.add("Planet");       aList.add("Comet");       System.out.println("The ArrayList elements are:");       for (String s : aList) {          System.out.println(s);       }    } }OutputThe output of the above ... Read More

Iterate through ArrayList in Java

George John
Updated on 29-Jun-2020 13:36:44

6K+ Views

The iterator can be used to iterate through the ArrayList wherein the iterator is the implementation of the Iterator interface. Some of the important methods declared by the Iterator interface are hasNext() and next().The hasNext() method returns true if there are more elements in the ArrayList and otherwise returns false. The next() method returns the next element in the ArrayList.A program that demonstrates iteration through ArrayList using the Iterator interface is given as followsExample Live Demoimport java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Demo {    public static void main(String[] args) {       List aList = new ArrayList();   ... Read More

Create NavigableMap in Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

91 Views

Let us first create a NavigableMapNavigableMap n = new TreeMap();Now, let us add some elementsn.put("A", 888); n.put("B", 999); n.put("C", 444); n.put("D", 555); n.put("E", 666); n.put("F", 888);The following is the complete example to create NavigableMap, add elements and display themExample Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 888); n.put("B", 999); n.put("C", 444); ... Read More

Advertisements