Found 34488 Articles for Programming

Remove a key from TreeMap in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

451 Views

Use the remove() method to remove a key from TreeMap.Let us first create a TreeMap and add some elements −TreeMap m = new TreeMap(); m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");Let us remove a key now. Here, we are removing key 3 now −m.remove(3)The following is an example to remove a key from TreeMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "India");       m.put(2, "US");       m.put(3, "Australia");       m.put(4, "Netherlands");   ... Read More

Create a TreeMap in Java and add key-value pairs

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

1K+ Views

A TreeMap cannot contain duplicate keys. TreeMap cannot contain the null key. However, It can have null values.Let us first see how to create a TreeMap −TreeMap m = new TreeMap();Add some elements in the form of key-value pairs −m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");The following is an example to create a TreeMap and add key-value pairs −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "India");       m.put(2, "US");       m.put(3, "Australia");     ... Read More

Remove a value from TreeMap in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

648 Views

Use the remove() method to remove a value from TreeMap.Create a TreeMap first and add some elements −TreeMap m = new TreeMap(); 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");Now, let’s say you need to remove a value with key 5. For that, use the remove() method −Object ob = m.remove(5);The following is an example to remove a value from TreeMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "PHP");       m.put(2, "jQuery");   ... Read More

Retrieve TreeMap size in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

132 Views

Get the TreeMap size using the size() method.First, create a TreeMap and add elements to it −TreeMap m = new TreeMap(); 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");Count the number of elements in the above TreeMap or get the size using the size() method as shown below −m.size()To get the size of TreeMap, the following is the code −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "PHP");       m.put(2, "jQuery");       ... Read More

Java Program to get highest key stored in TreeMap

Samual Sam
Updated on 30-Jul-2019 22:30:24

1K+ Views

Use the lastKey() method to get the highest key stored in TreeMap.Let us first set the TreeMap and add some elements to it −TreeMap m = new TreeMap(); 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");Now get the highest key −m.lastKey()The following is an example to get the highest key in TreeMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "PHP");       m.put(2, "jQuery");       m.put(3, "JavaScript");       m.put(4, "Ruby"); ... Read More

Retrieve the last element from a LinkedList in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

2K+ Views

The last element of a Linked List can be retrieved using the method java.util.LinkedList.getLast() respectively. This method does not require any parameters and it returns the last element of the LinkedList.A program that demonstrates this is given as follows −Example Live Demoimport java.util.LinkedList; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       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());    } }OutputThe last ... Read More

Delete first and last element from a LinkedList in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

317 Views

The first element can be deleted from a LinkedList by using the method java.util.LinkedList.removeFirst(). This method does not have any parameters and it returns the first element of the LinkedList.The last element can be deleted from a LinkedList by using the method java.util.LinkedList.removeLast(). This method does not have any parameters and it returns the last element of the LinkedList.A program that demonstrates this is given as follows −Example Live Demoimport java.util.LinkedList; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("Apple");       l.add("Mango");       ... Read More

Add elements at beginning and end of LinkedList in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

2K+ Views

Elements can be added at the beginning of a LinkedList by using the method java.util.LinkedList.addFirst(). This method has a single argument i.e. the element that is to be added at the beginning of the LinkedList.Elements can be added at the end of a LinkedList by using the method java.util.LinkedList.addLast(). This method has a single argument i.e. the element that is to be added at the end of the LinkedList.A program that demonstrates this is given as follows −Example Live Demoimport java.util.LinkedList; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();   ... Read More

Get lowest key stored in Java TreeMap

Samual Sam
Updated on 30-Jul-2019 22:30:24

1K+ Views

Use the firstKey() method to get the lowest key stored in TreeMap.Let us first set the TreeMap and add some elements to it −TreeMap m = new TreeMap(); 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");Now get the lowest key −m.firstKey()The following is an example to get the lowest key in TreeMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "PHP");       m.put(2, "jQuery");       m.put(3, "JavaScript");       m.put(4, "Ruby"); ... Read More

Get Head Map from TreeMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

149 Views

Use the headMap() method in Java to get ahead map. It returns a view of the portion of this map whose keys are less than or equal to.Let’s say you need to get the portion of the map above the set map (and not inclusive of it). For that, use the headMap() method like this −m.headMap(4)If you want the value 4 to be included as well, then set it as true −m.headMap(4, true)The following is an example to get Head Map from Java TreeMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {     ... Read More

Advertisements