Tutorialspoint

Java Get the unmodifiable view of the specified ArrayList

import 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 ArrayList elements are: " + aList);
   }
}

Java Insert all elements Collection to Specified Index ArrayList

import java.util.ArrayList;
import java.util.Vector;
public class Demo {
   public static void main(String[] args) {
      ArrayList<String> aList = new ArrayList<String>();
      aList.add("John");
      aList.add("Sally");
      aList.add("Harry");
      aList.add("Martha");
      aList.add("Susan");
      Vector<String> vec = new Vector<String>();
      vec.add("Peter");
      vec.add("Michael");
      vec.add("Jenna");
      aList.addAll(2, vec);
      System.out.println("The ArrayList elements are: " + aList);
   }
}

Java NavigableSet Class ceiling() method

import java.util.NavigableSet;
import java.util.TreeSet;
public class Demo {
   public static void main(String[] args) {
      NavigableSet<Integer> 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));
   }
}

Java Fetch elements of TreeSet using Iteration

import java.util.*;
public class Demo {
   public static void main(String args[]){
      TreeSet<String> set = new TreeSet<String>();
      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");
      System.out.println("TreeSet elements ...");
      Iterator<String> i = set.iterator();
      while(i.hasNext()){
         System.out.println(i.next());
      }
   }
}

Java Get the element ordered last in TreeSet

import java.util.*;
public class Demo {
   public static void main(String args[]){
      TreeSet<String> set = new TreeSet<String>();
      set.add("65");
      set.add("45");
      set.add("19");
      set.add("27");
      set.add("89");
      set.add("57");
      System.out.println("TreeSet elements (Sorted)...");
      Iterator<String> i = set.iterator();
      while(i.hasNext()){
         System.out.println(i.next());
      }
      System.out.println("Element order last = "+set.last());
   }
}

Java Loop through ArrayList

import java.util.ArrayList;
import java.util.List;
public class Demo {
   public static void main(String[] args) {
      ArrayList<String> aList = new ArrayList<String>();
      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);
      }
   }
}

Java Iterate through ArrayList

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Demo {
   public static void main(String[] args) {
      List aList = new ArrayList();
      aList.add("Adam");
      aList.add("John");
      aList.add("Nancy");
      aList.add("Peter");
      aList.add("Mary");
      Iterator i = aList.iterator();
      System.out.println("The ArrayList elements are:");
      while (i.hasNext()) {
         System.out.println(i.next());
      }
   }
}

Java Remove duplicate items from an ArrayList

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class Demo {
   public static void main(String[] args) {
      List<String> aList1 = new ArrayList<String>();
      aList1.add("Adam");
      aList1.add("John");
      aList1.add("Mary");
      aList1.add("Adam");
      aList1.add("Mary");
      System.out.println("The ArrayList items are:" );
      for (String i : aList1)
      System.out.println(i);
      HashSet<String> h = new HashSet<String>(aList1);
      List<String> aList2 = new ArrayList<String>(h);
      System.out.println("\nThe ArrayList items after removing duplicates are:" );
      for (String i : aList2)
      System.out.println(i);
   }
}

Java Create NavigableMap

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      NavigableMap<String, Integer> n = new TreeMap<String, Integer>();
      n.put("A", 888);
      n.put("B", 999);
      n.put("C", 444);
      n.put("D", 555);
      n.put("E", 666);
      n.put("F", 888);
      n.put("G", 999);
      n.put("H", 444);
      n.put("I", 555);
      n.put("J", 666);
      System.out.println("NavigableMap elements...\n"+n);
   }
}

Java Remove the last entry of the TreeMap

import java.util.*;
public class Demo{
   public static void main(String args[]){
      TreeMap<Integer, String> m = new TreeMap<Integer, String>();
      m.put(1,"India");
      m.put(2,"US");
      m.put(3,"Australia");
      m.put(4,"Netherlands");
      m.put(5,"Canada");
      System.out.println("TreeMap Elements = "+m);
      System.out.println("Removing Last Entry : "+m.pollLastEntry());
      System.out.println("Updated TreeMap Elements = "+m);
   }
}

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

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