Tutorialspoint

Java Iterate through a LinkedList using an Iterator

import java.util.LinkedList;
import java.util.Iterator;
public class Demo {
   public static void main(String[] args) {
      LinkedList<String> l = new LinkedList<String>();
      l.add("John");
      l.add("Sara");
      l.add("Susan");
      l.add("Betty");
      l.add("Nathan");
      System.out.println("The LinkedList elements are: ");
      for (Iterator i = l.iterator(); i.hasNext();) { 
         System.out.println(i.next());
      }
   }
}

Java Use Iterator to remove an element from a Collection

import java.util.ArrayList;
import java.util.Iterator;
public class Demo {
   public static void main(String[] args) {
      ArrayList<String> aList = new ArrayList<String>();
      aList.add("Apple");
      aList.add("Mango");
      aList.add("Guava");
      aList.add("Orange");
      aList.add("Peach");
      System.out.println("The ArrayList elements are: ");
      for (String s: aList) {
         System.out.println(s);
      }
      Iterator i = aList.iterator();
      String str = "";
      while (i.hasNext()) {
         str = (String) i.next();
         if (str.equals("Orange")) {
            i.remove();
            System.out.println("\nThe element Orange is removed");
            break;
         }
      }
      System.out.println("\nThe ArrayList elements are: ");
      for (String s: aList) {
         System.out.println(s);
      }
   }
}

Java Loop through a HashMap using an Iterator

import 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");
      student.put("104", "Susan");
      student.put("105", "James");
      Iterator i = student.keySet().iterator();
      while (i.hasNext()) {
         String key = (String) i.next();
         System.out.println("\nRoll Number: " + key);
         System.out.println("Name: " + student.get(key));
      }
   }
}

Java Loop through an ArrayList using an Iterator

import java.util.ArrayList;
import java.util.Iterator;
public class Demo {
   public static void main(String[] args) {
      ArrayList<String> aList = new ArrayList<String>();
      aList.add("Apple");
      aList.add("Mango");
      aList.add("Guava");
      aList.add("Orange");
      aList.add("Peach");
      System.out.println("The ArrayList elements are: ");
      for (Iterator iter = aList.iterator(); iter.hasNext();) {
         System.out.println(iter.next());
      }
   }
}

Java Create an object array from elements of LinkedList

import java.util.LinkedList;
public class Demo {
   public static void main(String[] args) {
      LinkedList<String> l = new LinkedList<String>();
      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 (Object i: objArr) {
         System.out.println(i);
      }
   }
}

Java Convert LinkedList to an array

import java.util.LinkedList;
import java.util.List;
public class Demo {
   public static void main(String[] args) {
      List<String> l = new LinkedList<String>();
      l.add("John");
      l.add("Sara");
      l.add("Susan");
      l.add("Betty");
      l.add("Nathan");
      String[] str = l.toArray(new String[0]);
      System.out.println("The String Array elements are: ");
      for (int i = 0; i< str.length; i++) {
         System.out.println(str[i]);
      }
   }
}

Java Iterate through elements of a LinkedList using a ListIterator

import java.util.ListIterator;
import java.util.LinkedList;
public class Demo {
   public static void main(String[] args) {
      LinkedList<String> l = new LinkedList<String>();
      l.add("Andy");
      l.add("Sara");
      l.add("James");
      l.add("Betty");
      l.add("Bruce");
      ListIterator li = l.listIterator();
      System.out.println("The LinkedList elements in the forward direction are: ");
      while (li.hasNext()) {
         System.out.println(li.next());
      }
      System.out.println("\nThe LinkedList elements in the reverse direction are: ");
      while (li.hasPrevious()) {
         System.out.println(li.previous());
      }
   }
}

Java Replace an element in an ArrayList using the ListIterator

import java.util.ArrayList;
import java.util.ListIterator;
public class Demo {
   public static void main(String[] args) {
      ArrayList<String> aList = new ArrayList<String>();
      aList.add("Amanda");
      aList.add("Taylor");
      aList.add("Justin");
      aList.add("Emma");
      aList.add("Peter");
      System.out.println("The ArrayList elements are: ");
      for (String s : aList) {
         System.out.println(s);
      }
      ListIterator<String> li = aList.listIterator();
      li.next();
      li.set("Monica");
      System.out.println("\nThe ArrayList elements are: ");
      for (String s : aList) {
         System.out.println(s);
      }
   }
}

Java Remove an element from an ArrayList using the ListIterator

import java.util.ArrayList;
import java.util.ListIterator;
public class Demo {
   public static void main(String[] args) {
      ArrayList<String> aList = new ArrayList<String>();
      aList.add("Apple");
      aList.add("Mango");
      aList.add("Guava");
      aList.add("Orange");
      aList.add("Peach");
      System.out.println("The ArrayList elements are: ");
      for (String s: aList) {
         System.out.println(s);
      }
      ListIterator li = aList.listIterator();
      li.next();
      li.remove();
      System.out.println("\nThe element Apple is removed");
      System.out.println("\nThe ArrayList elements are: ");
      for (String s: aList) {
         System.out.println(s);
      }
   }
}

Java Previous Index and Next Index in arraylist using ListIterator

import java.util.ArrayList;
import java.util.ListIterator;
public class Demo {
   public static void main(String[] args) {
      ArrayList<String> aList = new ArrayList<String>();
      aList.add("Amy");
      aList.add("Peter");
      aList.add("Justin");
      aList.add("Emma");
      aList.add("James");
      System.out.println("The ArrayList elements are: "+ aList);
      ListIterator<String> li = aList.listIterator();
      System.out.println("The previous index is: " + li.previousIndex());
      System.out.println("The next index is: " + li.nextIndex());
   }
}

1 2 3 4 5 6 7 ... 432 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.