Tutorialspoint

Get Size of Java LinkedHashSet

import java.util.*;
public class Demo {
   public static void main(String[] args) {
		LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
		set.add(10);
		set.add(20);
		set.add(30);
		set.add(40);
		set.add(50);
		set.add(60);
		System.out.println("LinkedHashSet elements...");
		System.out.println(set);
		System.out.println("Size: "+set.size());
   }
}

Iterate through elements of Java LinkedHashSet

import java.util.*;
public class Demo {
   public static void main(String[] args) {
		LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
		set.add(10);
		set.add(20);
		set.add(30);
		set.add(40);
		set.add(50);
		set.add(60);
		System.out.println("LinkedHashSet...");
		System.out.println(set);
		System.out.println("Iterating...");
		Iterator i = set.iterator();
		while (i.hasNext()){
		   System.out.println(i.next());
      }
   }
}

Create a new ArrayList from another collection in Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Demo {
   public static void main(String args[]) throws Exception {
      String str[] = { "John", "Macy", "Peter", "Susan", "Lucy" };
      List aList = new ArrayList(Arrays.asList(str));
      System.out.println("The ArrayList elements are: " + aList);
   }
}

Fill elements in a Java int array in a specified range

import java.util.Arrays;
public class Demo {
   public static void main(String[] argv) throws Exception {
      int[] intArray = new int[10];
		int intValue = 7;
		int indexStart = 2;
		int indexFinish = 8;
		Arrays.fill(intArray, indexStart, indexFinish, intValue);
		System.out.println("The int array content is: " + Arrays.toString(intArray));
   }
}

headSet() method of Java NavigableSet Class

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.headSet(55));
		System.out.println("Returned Value (high endpoint included) = " + set.headSet(55, true));
   }
}

Retrieve the last entry in the TreeMap in Java

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");
      for(Map.Entry e:m.entrySet()){
         System.out.println(e.getKey()+" "+e.getValue());
      }
      System.out.println("Last Entry in Map is "+m.lastEntry());
   }
}

Get last entry from NavigableMap in Java

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      NavigableMap<String, Integer> n = new TreeMap<String, Integer>();
		n.put("A", 498);
		n.put("B", 389);
		n.put("C", 868);
		n.put("D", 988);
		n.put("E", 686);
		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);
		System.out.println("Last Entry is "+n.lastEntry());
   }
}

Get the first element from a Sorted Set in Java

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);
         System.out.println("\nFirst element of the sorted set = "+ (Integer)sorted.first());
      }
      catch(Exception e) {}
   }
}

Creating a Sorted Set in Java

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) {}
   }
}

Convert an ArrayList to HashSet in Java

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
public class Main {
   public static void main(String[] args) {
		List<String> l = new ArrayList<String>();
		l.add("Accent");
		l.add("Speech");
		l.add("Diction");
		l.add("Tone");
		l.add("Pronunciation");
		Set<String> s = new HashSet<String>(l);
		System.out.println("HashSet elements...");
		for (Object ob : s)
		   System.out.println(ob);
   }
}

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

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