Found 4337 Articles for Java 8

Iterate through the values of TreeMap in Java

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

582 Views

Use Iterator and the values() method to iterate through the values of TreeMap.Let us first create a TreeMap and add some elements −TreeMap m = new TreeMap(); m.put(1, "PHP"); m.put(2, "jQuery"); m.put(3, "JavaScript"); m.put(4, "Ruby"); m.put(5, "Java"); m.put(6, "AngularJS"); m.put(7, "ExpressJS");Iterate through the values −Collection res = m.values(); Iterator i = res.iterator(); while (i.hasNext()) {    System.out.println(i.next()); }The following is the complete example to iterate through the values −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "PHP");       m.put(2, "jQuery"); ... Read More

Get Tail Map from TreeMap in Java

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

111 Views

To get Tail Map from TreeMap, use the tailMap() method. It gets a part or view of the map whose keys are greater than equal to the key set as a parameter.Let us first create a TreeMap −TreeMap m = new TreeMap();Now, we will add some elements −m.put(1, "PHP"); m.put(2, "jQuery"); m.put(3, "JavaScript"); m.put(4, "Ruby"); m.put(5, "Java"); m.put(6, "AngularJS"); m.put(7, "ExpressJS");Get the Tail Map −m.tailMap(4)The following is an example to get Tail Map from TreeMap in Java −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]){       TreeMap m = new TreeMap();     ... Read More

Use ListIterator to traverse an ArrayList in the forward direction in Java

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

165 Views

A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in the List Collection. So the ListIterator is only valid for classes such as LinkedList, ArrayList etc.The method hasNext( ) in ListIterator returns true if there are more elements in the List and false otherwise. The method next( ) returns the next element in the List and advances the cursor position.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = ... Read More

Iterate through a Collection using an Iterator in Java

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

186 Views

A collection in Java provides an architecture to handle a group of objects. The different classes in the Java Collection Framework are ArrayList, LinkedList, HashSet, Vector etc.An Iterator can be used to iterate through a Collection and a program that demonstrates this using ArrayList is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("John");       aList.add("Peter");       aList.add("Harry");       aList.add("James");       aList.add("Arthur");       System.out.println("The ArrayList elements are: "); ... Read More

Iterate through a LinkedList using an Iterator in Java

Chandu yadav
Updated on 29-Jun-2020 13:53:57

6K+ Views

An Iterator can be used to loop through an LinkedList. The method hasNext( ) returns true if there are more elements in LinkedList and false otherwise. The method next( ) returns the next element in the LinkedList and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("John");       l.add("Sara");       l.add("Susan");       l.add("Betty");       l.add("Nathan");   ... Read More

Use Iterator to remove an element from a Collection in Java

Arjun Thakur
Updated on 29-Jun-2020 13:54:59

17K+ Views

An element can be removed from a Collection using the Iterator method remove(). This method removes the current element in the Collection. If the remove() method is not preceded by the next() method, then the exception IllegalStateException is thrown.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Apple");       aList.add("Mango");       aList.add("Guava");       aList.add("Orange");       aList.add("Peach");       System.out.println("The ArrayList elements are: ");     ... Read More

Loop through a HashMap using an Iterator in Java

Ankith Reddy
Updated on 29-Jun-2020 13:55:50

174 Views

An Iterator can be used to loop through a HashMap. The method hasNext( ) returns true if there are more elements in HashMap and false otherwise. The method next( ) returns the next key element in the HashMap and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows.Example Live Demoimport java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Demo {    public static void main(String[] args) {       Map student = new HashMap();       student.put("101", "Harry");       student.put("102", "Amy");       student.put("103", "John");       ... Read More

Loop through an ArrayList using an Iterator in Java

George John
Updated on 29-Jun-2020 13:56:42

4K+ Views

An Iterator can be used to loop through an ArrayList. The method hasNext( ) returns true if there are more elements in ArrayList and false otherwise. The method next( ) returns the next element in the ArrayList and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Apple");       aList.add("Mango");       aList.add("Guava");       aList.add("Orange");       aList.add("Peach");   ... Read More

Create an object array from elements of LinkedList in Java

Chandu yadav
Updated on 29-Jun-2020 13:45:11

340 Views

An object array can be created from the elements of a LinkedList using the method java.util.LinkedList.toArray(). This method returns the object array with all the LinkedList elements in the correct order.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("Amy");       l.add("Sara");       l.add("Joe");       l.add("Betty");       l.add("Nathan");       Object[] objArr = l.toArray();       System.out.println("The object array elements are: ");       for ... Read More

Iterate through elements of a LinkedList using a ListIterator in Java

Ankith Reddy
Updated on 29-Jun-2020 13:46:41

183 Views

A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in a LinkedList.The method hasNext( ) in ListIterator returns true if there are more elements in the LinkedList while traversing in the forward direction and false otherwise. The method next( ) returns the next element in the LinkedList and advances the cursor position.The method hasPrevious( ) in ListIterator returns true if there are more elements in the LinkedList while traversing in the reverse direction and false otherwise. The method previous( ) returns the previous element in the LinkedList and reduces the ... Read More

Advertisements