Tutorialspoint

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

Remove a key from 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 a Key = "+m.remove(3));
      System.out.println("Updated TreeMap Elements = "+m);
   }
}

TreeMap in Java and add key-value pairs

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());
      }
   }
}

Remove a value from 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,"PHP");
      m.put(2,"jQuery");
      m.put(3,"JavaScript");
      m.put(4,"Ruby");
      m.put(5,"Java");
      m.put(6,"AngularJS");
      m.put(7,"ExpressJS");
      System.out.println("TreeMap Elements...");
      Collection res = m.values();
      Iterator i = res.iterator();
      while (i.hasNext()){
         System.out.println(i.next());
      }
      System.out.println("TreeMap size = "+m.size());
      // Remove an element
      Object ob = m.remove(5);
      System.out.println("\nTreeMap after removing a single element...\n "+m);
      System.out.println("Size of TreeMap now = "+m.size());
   }
}

Retrieve TreeMap size 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,"PHP");
      m.put(2,"jQuery");
      m.put(3,"JavaScript");
      m.put(4,"Ruby");
      m.put(5,"Java");
      m.put(6,"AngularJS");
      m.put(7,"ExpressJS");
      System.out.println("TreeMap Elements...\n"+m);
      System.out.println("\nSize of TreeMap: " + m.size());
   }
}

Java Program to get highest key stored in TreeMap

import java.util.*;
public class Demo{
   public static void main(String args[]){
      TreeMap<Integer,String> m = new TreeMap<Integer,String>();
      m.put(1,"PHP");
      m.put(2,"jQuery");
      m.put(3,"JavaScript");
      m.put(4,"Ruby");
      m.put(5,"Java");
      m.put(6,"AngularJS");
      m.put(7,"ExpressJS");
      System.out.println("TreeMap Elements...\n"+m);
      System.out.println("Highest key in TreeMap: " + m.lastKey());
   }
}

Retrieve the last element from a LinkedList in Java

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");
		System.out.println("The last element of the Linked List is : " + l.getLast());
   }
}

Delete first and last element from a LinkedList in Java

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);
      l.removeFirst();
      l.removeLast();
      System.out.println("The LinkedList is: " + l);
   }
}

Add elements at beginning and end of LinkedList in Java

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");
      System.out.println("The LinkedList is: " + l);
      l.addFirst("Orange");
      l.addLast("Guava");
      System.out.println("The LinkedList is: " + l);
   }
}

Advertisements
Loading...

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