Tutorialspoint

Java Iterate through an ArrayList using a 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");
      ListIterator i = aList.listIterator();
      System.out.println("The ArrayList elements in the forward direction are: ");
      while (i.hasNext()) {
         System.out.println(i.next());
      }
      System.out.println("\nThe ArrayList elements in the reverse direction are: ");
      while (i.hasPrevious()) {
         System.out.println(i.previous());
      }
   }
}

Java Remove a specific element from a LinkedList

import java.util.LinkedList;
public class Demo {
   public static void main(String[] args) {
      LinkedList<String> l = new LinkedList<String>();
      l.add("Apple");
      l.add("Mango");
      l.add("Pear");
      l.add("Orange");
      l.add("Guava");
      System.out.println("The LinkedList is: " + l);
      System.out.println("The element Mango is removed from the LinkedList? " +
      l.remove("Mango"));
      System.out.println("The LinkedList is: " + l);
   }
}

Java creating a HashSet

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // create a hash set
      HashSet hs = new HashSet();
      // add elements to the hash set
      hs.add("B");
      hs.add("A");
      hs.add("D");
      hs.add("E");
      hs.add("C");
      hs.add("F");
      hs.add("K");
      hs.add("M");
      hs.add("N");
      System.out.println(hs);
   }
}

Java Set vs HashSet vs TreeSet

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      int a[] = {77, 23, 4, 66, 99, 112, 45, 56, 39, 89};
      Set<Integer> s = new HashSet<Integer>();
      try {
         for(int i = 0; i < 5; i++) {
            s.add(a[i]);
         }
         System.out.println(s);
         TreeSet sorted = new TreeSet<Integer>(s);
         System.out.println("Sorted list = ");
         System.out.println(sorted);
      } catch(Exception e) {}
   }
}

Java Program to retrieve the set of all keys in HashMap

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // Create hash map
      HashMap hm = new HashMap();
      hm.put("Wallet", new Integer(700));
      hm.put("Belt", new Integer(600));
      hm.put("Backpack", new Integer(1200));
      System.out.println("Map = "+hm);
      Set keys = hm.keySet();
      System.out.println("\nKeys...");
      Iterator i = keys.iterator();
      while (i.hasNext()) {
         System.out.println(i.next());
      }
   }
}

Java Remove all values from HashMap

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // Create hash map
      HashMap hm = new HashMap();
      hm.put("Wallet", new Integer(700));
      hm.put("Belt", new Integer(600));
      hm.put("Backpack", new Integer(1200));
      System.out.println("Map = "+hm);
      hm.clear();
      System.out.println("Map after removing all the elements (blank)= "+hm);
   }
}

Java Clear LinkedList

import java.util.LinkedList;
public class Demo {
   public static void main(String[] args) {
      LinkedList<String> l = new LinkedList<String>();
      l.add("Orange");
      l.add("Apple");
      l.add("Peach");
      l.add("Guava");
      System.out.println("LinkedList before using the LinkedList.clear() method: " + l);
      l.clear();
      System.out.println("LinkedList after using the LinkedList.clear() method: " + l);
   }
}

Java Get last index a particular element in an ArrayList

import 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("Orange");
      aList.add("Mango");
      aList.add("Peach");
      System.out.println("The last index of the element Orange in ArrayList is: " +
      aList.lastIndexOf("Orange"));
   }  
}

Java Get the index of a particular element in an ArrayList

import 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");
      System.out.println("The index of the element Apple in ArrayList is: " +
      aList.indexOf("Apple"));
   }
}

Java NavigableMap pollFirstEntry() method

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      NavigableMap<Integer, String> n = new TreeMap<Integer, String>();
      n.put(5, "Tom");
      n.put(9, "John");
      n.put(14, "Jamie");
      n.put(1, "Tim");
      n.put(4, "Jackie");
      n.put(15, "Kurt");
      n.put(19, "Tiger");
      n.put(24, "Jacob");
      System.out.println("NavigableMap elements...\n"+n);
      System.out.println("\nRemoved (first) key-value is "+n.pollFirstEntry());
      System.out.println("\nUpdated NavigableMap elements...\n"+n);
   }
}

Previous 1 ... 3 4 5 6 7 8 9 ... 2067 Next
Advertisements
Loading...

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