Java Articles

Page 37 of 450

Replace an element in an ArrayList using the ListIterator in Java

George John
George John
Updated on 11-Mar-2026 1K+ Views

An element in ArrayList can be replaced using the ListIterator method set(). This method has a single parameter i.e. the element that is to be replaced and the set() method replaces it with the last element returned by the next() or previous() methods.A program that demonstrates this is given as follows.Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Amanda");       aList.add("Taylor");       aList.add("Justin");       aList.add("Emma");       aList.add("Peter");       System.out.println("The ArrayList elements are: ...

Read More

Iterate through a LinkedList using an Iterator in Java

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 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.Exampleimport 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 ListIterator to traverse an ArrayList in the forward direction in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 257 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 −Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ...

Read More

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

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ 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 hasPrevious( ) in ListIterator returns true if there are more elements in the List while traversing in the reverse direction and false otherwise. The method previous( ) returns the previous element in the List and reduces the cursor position backward.A program that demonstrates this is given as follows −Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) { ...

Read More

Get Size of Java LinkedHashMap

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 732 Views

The size() method is used to get the size of LinkedHashMap in Java.Create a LinkedHashMap and add some elements to it −LinkedHashMap l = new LinkedHashMap(); l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad");Get the size now −l.size()The following is an example to get the size of LinkedHashMap −Exampleimport java.util.LinkedHashMap; public class Demo {    public static void main(String[] args) {       LinkedHashMap l = new LinkedHashMap();       l.put("1", "Jack");       l.put("2", "Tom");       l.put("3", "Jimmy");       l.put("4", "Morgan");       l.put("5", "Tim");     ...

Read More

Check if a particular element exists in Java LinkedHashSet

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 319 Views

Use the contains() method to check if a specific element exists in LinkedHashSet or not.Let us first create a LinkedHashSet and add some elements −LinkedHashSet l = new LinkedHashSet(); l.add(new String("1")); l.add(new String("2")); l.add(new String("3")); l.add(new String("4")); l.add(new String("5")); l.add(new String("6")); l.add(new String("7"));Now, check whether it contains element “5” or not −l.contains("5")The following is an example to check if a particular element exists in LinkedHashSet −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       LinkedHashSet l = new LinkedHashSet();       l.add(new String("1"));       l.add(new String("2"));       l.add(new ...

Read More

Convert a Set into a List in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 313 Views

First, create a Set and add elements −Set s = new HashSet(); s.add("P"); s.add(new Date()); s.add(new Long(898999)); s.add("Q"); s.add("R"); s.add(new Integer(1));Convert the above Set to a List −List l = new ArrayList(s);The following is an example to convert a set into a list in Java −Exampleimport java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Set; import java.util.HashSet; public class Demo {    public static void main(String[] args) {       Set s = new HashSet();       s.add("P");;       s.add(new Date());       s.add(new Long(898999));       s.add("Q");       s.add("R");       s.add(new ...

Read More

Copy all the elements from one set to another in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

Use the clone() method to copy all elements from one set to another.First HashSet −HashSet set = new HashSet (); set.add("One"); set.add("Two");Create another set and clone first set into the second −HashSet newSet = new HashSet ();Copy (clone) all elements to the second set −newSet = (HashSet)set.clone();The following is an example to copy all elements from one set to another −Exampleimport java.util.*; public class Demo {    public static void main(String args[]) {       HashSet set = new HashSet ();       HashSet newSet = new HashSet ();       set.add("One");   ...

Read More

Fetch key-valuepair from Java LinkedHashSet

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 372 Views

To fetch LinkedHashSet key-value pair in Java, use the entrySet() method.Let us first create LinkedHashSet and add some elements −LinkedHashMap l = new LinkedHashMap(); l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad");Now, fetch a key-value pair −l.entrySet()The following is an example to fetch key-value pair from LinkedHashSet −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       LinkedHashMap l = new LinkedHashMap();       l.put("1", "Jack");       l.put("2", "Tom");       l.put("3", "Jimmy");       l.put("4", "Morgan");       l.put("5", "Tim");       ...

Read More

Create a TreepMap in Java and add elements

Samual Sam
Samual Sam
Updated on 11-Mar-2026 99 Views

Let us create TreeMap in Java. It stores unique elements in ascending order −TreeMap m = new TreeMap();Now let us 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");The following is an example to create a TreeMap −Exampleimport java.util.*; public class Demo {    public static void main(String args[]) {       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, ...

Read More
Showing 361–370 of 4,498 articles
« Prev 1 35 36 37 38 39 450 Next »
Advertisements