Tutorialspoint

Convert a List to a Set in Java

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
   public static void main(String[] args) {
		List<String> l = new ArrayList<String>();
		l.add("Excellent");
		l.add("Awesome");
		l.add("Amazing");
		l.add("Incredible");
		l.add("Awesome");
		l.add("Extraordinary");
		l.add("Unbelievable");
		Set<String> s = new HashSet<String>(l);
		for (Object ob : s)
		   System.out.println(ob);
   }
}

Get higher key from NavigableMap in Java

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("Higher Key = "+n.higherKey(15));
   }
}

Get ceiling key from NavigableMap in Java

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(30, "Tiger");
		n.put(24, "Jacob");
		System.out.println("NavigableMap elements...\n"+n);
		System.out.println("Ceiling Key = "+n.ceilingKey(20));
   }
}

Add an element to specified index of ArrayList in Java

import java.util.ArrayList;
import java.util.List;
public class Demo {
   public static void main(String args[]) throws Exception {
		List aList = new ArrayList();
		aList.add(0, "Dog");
		aList.add(1, "Cat");
		aList.add(2, "Horse");
		aList.add(2, "Pig");
		aList.add(3, "Cow");
		System.out.println("The ArrayList elements are: " + aList);
   }
}

Create a Stack and Queue using ArrayDeque in Java

import java.util.ArrayDeque;
import java.util.Deque;
public class Demo {
   public static void main(String args[]) {
		Deque<String> s = new ArrayDeque<String>();
		Deque<String> q = new ArrayDeque<String>();
		// stack
		s.push("Bat");
		s.push("Mat");
		s.push("Cat");
		s.push("Rat");
		s.push("Hat");
		s.push("Fat");
		while (!s.isEmpty())
		   System.out.print(s.pop() + " ");
		   System.out.print("\n");
			// queue
			q.push("Bat");
			q.push("Mat");
			q.push("Cat");
			q.push("Rat");
			q.push("Hat");
			q.push("Fat");
			while (!q.isEmpty())
			   System.out.print(q.remove() + " ");
   }
}

Get last key 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 Key is "+n.lastKey());
   }
}

Get first 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("First Entry is "+n.firstEntry());
   }
}

Get first key 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("First Key = "+n.firstKey());
   }
}

Remove the first entry of 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");
      System.out.println("TreeMap Elements = "+m);
      System.out.println("Removing First Entry : "+m.pollFirstEntry());
      System.out.println("Updated TreeMap Elements = "+m);
   }
}

Java Program to remove a key from a 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);
      // removing a key associated with a given value
      System.out.println("Key removed? "+m.remove(3, "Australia"));
      System.out.println("Updated TreeMap Elements = "+m);
   }
}

Advertisements
Loading...

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