Head map in java

What is NavigableMap in Java ? TreeMap, headMap, tailMap, and subMap Examples

Hello guys, if you are wondering what is NavigableMap and why should Java developer know about it you are at right place. NavigableMap in Java is an extension of SortedMap like TreeMap which provides convenient navigation methods like lowerKey , floorKey , ceilingKey, and higherKey . NavigableMap is added on Java 1.6 and along with these popular navigation methods it also provides ways to create a Sub Map from existing Map in Java, like headMap whose keys are less than the specified key, tailMap whose keys are greater than specified key and a subMap which strictly contains keys that falls between toKey and fromKey .

All of these methods also provide a boolean to include the specified key or not. TreeMap and ConcurrentSkipListMap are two concrete implementations of NavigableMap in Java 1.6 API.

Though NavigableMap is not as popular as HashMap, ConcurrentHashMap or Hashtable but given that TreeMap implements NavigableMap you already get all good things in a well-known Map implementation.

And, I must tell you that TreeMap is an essential class for every Java developer. It’s often used to keep object in sorted order for example, if you are creating an order cache then you can use TreeMap to keep the order sorted based upon time and price priority.

Читайте также:  How to Set Up Expiration to Sessions using PHP

It is also one of the most popular Map class for interview point of view. Almost all Java interviews ask about TreeMap and that why you will find it in every Java Collection interview question list.

How to use NavigableMap in Java? TreeMpa Example

In this Java tutorial, we will explore some API methods of NavigableMap to show its functionality. This Java program shows an example of lowerKey which returns keys less than specified, floorKey returns key less than or equal to, ceilingKey return greater than or equal to and higherKey which returns keys which are greater than the specified key.

This Java example also demonstrates the use of headMap() , tailMap() and subMap() method which is used to create Map from an existing Map in Java. headMap returns a Map whose keys are lower than specified keys while tailMap returns Map which contains keys, that are higher than specified.

You can also see these Java Collections and Stream courses to learn more about Collection Framework and Maps in Java

Here is a complete code example of How to use NavigableMap in Java.

import java.util.NavigableMap ;
import java.util.TreeMap ;

/**
*
* Java program to demonstrate What is NavigableMap in Java and How to use NavigableMap

* like lowerKey(), floorKey, ceilingKey() and higherKey().
* There Entry counterpart and methods to create subMap e.g. headMap(), tailMap()

* and subMap().
*
* @author Javin Paul
*/
public class NavigableMapExample

public static void main ( String args [])

//NavigableMap extends SortedMap to provide useful navigation methods
NavigableMap < String, String > navigableMap = new TreeMap < String, String > () ;

navigableMap. put ( «C++» , «Good programming language» ) ;
navigableMap. put ( «Java» , «Another good programming language» ) ;
navigableMap. put ( «Scala» , «Another JVM language» ) ;
navigableMap. put ( «Python» , «Language which Google use» ) ;

System. out . println ( «SorteMap : » + navigableMap ) ;

//lowerKey returns key which is less than specified key
System. out . println ( «lowerKey from Java : »
+ navigableMap. lowerKey ( «Java» )) ;

// floorKey returns key which is less than
// or equal to specified key

System. out . println ( «floorKey from Java: »
+ navigableMap. floorKey ( «Java» )) ;

//ceilingKey returns key which is greater than
// or equal to specified key

System. out . println ( «ceilingKey from Java: »
+ navigableMap. ceilingKey ( «Java» )) ;

//higherKey returns key which is greater specified key
System. out . println ( «higherKey from Java: »
+ navigableMap. higherKey ( «Java» )) ;

//Apart from navigation methods,
// it also provides a useful method

//to create subMap from existing Map
// tailMap, headMap and subMap

//an example of headMap — returns NavigableMap whose key is less than specified
NavigableMap < String, String > headMap = navigableMap. headMap ( «Python» , false ) ;
System. out . println ( «headMap created form navigableMap : » + headMap ) ;

//an example of tailMap — returns NavigableMap whose key is greater than specified
NavigableMap < String, String > tailMap = navigableMap. tailMap ( «Scala» , false ) ;
System. out . println ( «tailMap created form navigableMap : » + tailMap ) ;

//an example of subMap — return NavigableMap from toKey to fromKey
NavigableMap < String, String > subMap
= navigableMap. subMap ( «C++» , false
, «Python» , false ) ;

System. out . println ( «subMap created form navigableMap : »
+ subMap ) ;
>
>

Output:
SorteMap : < C++=Good programming language, Java=Another good programming language, Python=Language which Google use, Scala=Another JVM language >
lowerKey from Java : C++
floorKey from Java: Java
ceilingKey from Java: Java
higherKey from Java: Python
headMap created form navigableMap : < C++=Good programming language, Java=Another good programming language >
tailMap created form navigableMap : <>
subMap created form navigableMap :

That’s all on What is NavigableMap in Java and How to use NavigableMap for example. We have seen examples of popular navigation methods on TreeMap e.g. floorKey . You can also use similar methods like lowerEntry , floorEntry , ceilingEntry and higherEntry to retrieve Entry instead of a key. NavigableMap is also a good utility to create a subset of a Map in Java.

Источник

Get Head Map from TreeMap in Java

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 −

If you want the value 4 to be included as well, then set it as true −

The following is an example to get Head Map from Java TreeMap −

Example

import java.util.*; public class Demo < public static void main(String args[]) < TreeMapm = 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"); System.out.println("TreeMap Elements. 
"+m); System.out.println("
Head Map has = "+m.headMap(4)); > >

Output

TreeMap Elements. Head Map has =

karthikeya Boyini

I love programming (: That’s all I know

  • Related Articles
  • Get Sub Map from TreeMap in Java
  • Get Tail Map from TreeMap in Java
  • Java Program to get Tail Map from TreeMap
  • Java Program to get Sub Map from TreeMap
  • Get Head Set from Java TreeSet
  • Get lowest key stored in Java TreeMap
  • Create NavigableMap from TreeMap in Java
  • Remove all values from TreeMap in Java
  • Remove a value from TreeMap in Java
  • Remove a key from TreeMap in Java
  • Java Program to get highest key stored in TreeMap
  • How to get head element from ArrayBlockingQueue in android?
  • Retrieve TreeMap size in Java
  • Java TreeMap Special Methods
  • Map to get substring and convert to int in Java

Источник

java.util.TreeMap.headMap() Method

TheheadMap(K toKey) method is used to return a view of the portion of this map whose keys are strictly less than toKey.

Declaration

Following is the declaration for java.util.TreeMap.headMap() method.

public SortedMap headMap(K toKey)

Parameters

toKey − This is the high endpoint (exclusive) of the keys in the returned map.

Return Value

The method call returns a view of the portion of this map whose keys are strictly less than toKey.

Exception

  • ClassCastException − This exception is thrown if toKey is not compatible with this map’s comparator (or, if the map has no comparator, if toKey does not implement Comparable). Implementations may, but are not required to, throw this exception if toKey cannot be compared to keys currently in the map.
  • NullPointerException − This exception is thrown if toKey is null and this map uses natural ordering, or its comparator does not permit null keys.
  • IllegalArgumentException − This exception is thrown if this map itself has a restricted range, and toKey lies outside the bounds of the range.

Example

The following example shows the usage of java.util.TreeMap.headMap() method.

package com.tutorialspoint; import java.util.*; public class TreeMapDemo < public static void main(String[] args) < // creating maps TreeMaptreemap = new TreeMap(); SortedMap treemaphead = new TreeMap(); // populating tree map treemap.put(2, "two"); treemap.put(1, "one"); treemap.put(3, "three"); treemap.put(6, "six"); treemap.put(5, "five"); // getting head map treemaphead = treemap.headMap(3); System.out.println("Checking values of the sorted map"); System.out.println("Value is: "+ treemaphead); > >

Let us compile and run the above program, this will produce the following result.

Checking values of the sorted map Value is:

Источник

java.util.TreeMap.headMap() Method

TheheadMap(K toKey,boolean inclusive) method is used to return a view of the portion of this map whose keys are less than (or equal to, if inclusive is true)toKey.

Declaration

Following is the declaration for java.util.TreeMap.headMap() method.

public NavigableMap headMap(K toKey,boolean inclusive)

Parameters

  • toKey − This is the high endpoint of the keys in the returned map.
  • inclusive − This is true if the high endpoint is to be included in the returned view.

Return Value

The method call returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.

Exception

  • ClassCastException − This exception is thrown if toKey is not compatible with this map’s comparator.
  • NullPointerException − This exception is thrown if toKey is null and this map uses natural ordering, or its comparator does not permit null keys.
  • IllegalArgumentException − This exception is thrown if this map itself has a restricted range, and toKey lies outside the bounds of the range.

Example

The following example shows the usage of java.util.TreeMap.headMap() method.

package com.tutorialspoint; import java.util.*; public class TreeMapDemo < public static void main(String[] args) < // creating maps TreeMaptreemap = new TreeMap(); NavigableMap treemapheadincl = new TreeMap(); // populating tree map treemap.put(2, "two"); treemap.put(1, "one"); treemap.put(3, "three"); treemap.put(6, "six"); treemap.put(5, "five"); // getting head map inclusive 3 treemapheadincl = treemap.headMap(3,true); System.out.println("Checking values of the map"); System.out.println("Value is: "+ treemapheadincl); > >

Let us compile and run the above program, this will produce the following result.

Checking values of the map Value is:

Источник

Оцените статью