Obtener submapa, mapa de cabecera y mapa de cola de Java TreeMap

TreeMap se utiliza para implementar la interfaz Map y Navigable Map con la clase AbstractMap en Java. Se pueden usar varios constructores en TreeMap para mantener el orden ordenado de sus claves.

A. método subMap() de TreeMap

Sintaxis:

newTreeMap = oldTreeMap.subMap(startKey,endKey)
  • startKey : el punto de inicio o el extremo inferior del mapa, incluidos los puntos que se van a considerar.
  • endKey : el punto final o el extremo superior del mapa excluyendo los puntos que se van a considerar.

Tipo de devolución:

Returns another map containing the portion of the map within the specified range.

Implementación:

Java

// Java code to illustrate submap() Method
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        TreeMap<Integer, String> tree
            = new TreeMap<Integer, String>();
  
        // Mapping String to Integer
        tree.put(47, "Sashi");
        tree.put(82, "Ridhi");
        tree.put(66, "Himanshu");
        tree.put(98, "Sarthak");
        tree.put(87, "Sonika");
        tree.put(85, "Ritesh");
        tree.put(89, "Yogesh");
  
        // printing the complete TreeMap
        System.out.println("The original map is: \n"
                           + tree);
  
        // printing the submap from key
        // 67(included) to 89(excluded)
        System.out.println("The subMap is: \n"
                           + tree.subMap(67, 89));
  
        // if start key and end key are same then
        // this method returns a null map
        System.out.println("Empty subMap: \n"
                           + tree.subMap(67, 67));
    }
}
Producción

The original map is: 
{47=Sashi, 66=Himanshu, 82=Ridhi, 85=Ritesh, 87=Sonika, 89=Yogesh, 98=Sarthak}
The subMap is: 
{82=Ridhi, 85=Ritesh, 87=Sonika}
Empty subMap: 
{}

B. método headMap() de TreeMap

las porciones

Sintaxis:

newTreeMap = oldTreeMap.headMap(specifiedKey)

Tipo de devolución:

The method returns the portion of the map whose keys are less than the specified key.

Implementación:

Java

// Java code to illustrate headmap() method
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        TreeMap<Integer, String> tree
            = new TreeMap<Integer, String>();
  
        // Mapping String to Integer
        tree.put(47, "Sashi");
        tree.put(82, "Ridhi");
        tree.put(66, "Himanshu");
        tree.put(98, "Sarthak");
        tree.put(87, "Sonika");
        tree.put(85, "Ritesh");
        tree.put(89, "Yogesh");
  
        // printing the complete TreeMap
        System.out.println("The original map is: \n"
                           + tree);
  
        // this will print all the key-value
        // pairs which are less than 67
        System.out.println("The headMap is: \n"
                           + tree.headMap(67));
    }
}
Producción

The original map is: 
{47=Sashi, 66=Himanshu, 82=Ridhi, 85=Ritesh, 87=Sonika, 89=Yogesh, 98=Sarthak}
The headMap is: 
{47=Sashi, 66=Himanshu}

C. método tailMap() de TreeMap

mapa de cola()

Sintaxis:

newMap = oldMap.tailMap(specifiedKey)

Tipo de devolución:

This method returns a map in which keys are greater than or equal to the specified key.

Implementación:

Java

// Java code to illustrate tailmap() Method
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        TreeMap<Integer, String> tree
            = new TreeMap<Integer, String>();
  
        // Mapping String to Integer
        tree.put(47, "Sashi");
        tree.put(82, "Ridhi");
        tree.put(66, "Himanshu");
        tree.put(98, "Sarthak");
        tree.put(87, "Sonika");
        tree.put(85, "Ritesh");
        tree.put(89, "Yogesh");
  
        // printing the complete TreeMap
        System.out.println("The original map is: \n"
                           + tree);
  
        // this will print all the key-value pairs
        // which are greater than or equal to 67
        System.out.println("The tailMap is: \n"
                           + tree.tailMap(67));
    }
}
Producción

The original map is: 
{47=Sashi, 66=Himanshu, 82=Ridhi, 85=Ritesh, 87=Sonika, 89=Yogesh, 98=Sarthak}
The tailMap is: 
{82=Ridhi, 85=Ritesh, 87=Sonika, 89=Yogesh, 98=Sarthak}

Punto importante: cualquier cambio realizado en el mapa devuelto por los métodos subMap(), headMap(), tailMap(), también se reflejará en el mapa original.

Implementación:

Java

// Combine operation on getting submap,
// headmap, and tailmap from Java TreeMap
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        TreeMap<Integer, String> originalTree
            = new TreeMap<Integer, String>();
  
        // Mapping String to Integer
        originalTree.put(82, "Ridhi");
        originalTree.put(87, "Sonika");
        originalTree.put(85, "Ritesh");
        originalTree.put(89, "Yogesh");
        originalTree.put(80, "Yashdeep");
        originalTree.put(99, "Raushan");
        originalTree.put(67, "Shishya");
  
        // printing the complete TreeMap
        System.out.println("The original map is: \n"
                           + originalTree);
  
        // submap from key 67(included) to 89(excluded)
        Map<Integer, String> newSubMap
            = originalTree.subMap(67, 89);
  
        // doing some modification in map named
        // newSubMap and this modification is going to
        // be refelected in the original treemap also
        newSubMap.remove(67);
  
        // printing the original treemap to see the
        // modification
        System.out.println(
            "The original map after remove operation is: \n"
            + originalTree);
  
        // creating a new map named newHeadMap
        Map<Integer, String> newHeadMap
            = originalTree.headMap(87);
  
        // doing some modification in map named
        // newHeadMap and this modification is going to
        // be refelected in the original treemap also
        newHeadMap.remove(82);
  
        // printing the original
        // treemap to see the modification
        System.out.println(
            "The original map after remove operation: \n"
            + originalTree);
  
        // creating a new map named newTailMap
        Map<Integer, String> newTailMap
            = originalTree.tailMap(85);
  
        // doing some modification in map named
        // newHeadMap and this modification is going to
        // be refelected in the original treemap also
        newTailMap.put(94, "Ankit");
  
        // printing the original treemap
        // to see the modification
        System.out.println(
            "The original map after put operation is: \n"
            + originalTree);
    }
}
Producción

The original map is: 
{67=Shishya, 80=Yashdeep, 82=Ridhi, 85=Ritesh, 87=Sonika, 89=Yogesh, 99=Raushan}
The original map after remove operation is: 
{80=Yashdeep, 82=Ridhi, 85=Ritesh, 87=Sonika, 89=Yogesh, 99=Raushan}
The original map after remove operation: 
{80=Yashdeep, 85=Ritesh, 87=Sonika, 89=Yogesh, 99=Raushan}
The original map after put operation is: 
{80=Yashdeep, 85=Ritesh, 87=Sonika, 89=Yogesh, 94=Ankit, 99=Raushan}

Nota: si intenta colocar una entrada en el mapa que está fuera del rango del mapa devuelto por esos tres métodos, el código generará una IllegalArgumentException .

Implementación:

Java

// Exception when trying to put an entry in
// the map which is out of the range of the map
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        TreeMap<Integer, String> originalTree
            = new TreeMap<Integer, String>();
  
        // Mapping String to Integer
        originalTree.put(47, "Sashi");
        originalTree.put(82, "Ridhi");
        originalTree.put(66, "Himanshu");
        originalTree.put(98, "Sarthak");
        originalTree.put(87, "Sonika");
        originalTree.put(85, "Ritesh");
        originalTree.put(89, "Yogesh");
        originalTree.put(80, "Yashdeep");
        originalTree.put(99, "Raushan");
        originalTree.put(67, "Shishya");
  
        // printing the complete TreeMap
        System.out.println("The original map is: \n"
                           + originalTree);
  
        // submap from key 67(included) to 89(excluded)
        Map<Integer, String> newSubMap
            = originalTree.subMap(67, 89);
  
        // This will throw an exception because
        // we can give key in between 67 - 89
        newSubMap.put(89, "Sandra");
  
        Map<Integer, String> newHeadMap
            = originalTree.headMap(87);
  
        // This will throw an exception because
        // in newHeadMap we can only put key < 87
        newHeadMap.put(98, "Aman");
  
        Map<Integer, String> newTailMap
            = originalTree.tailMap(87);
  
        // This will throw an exception because
        // in newTailMap we can only put key >= 87
        newTailMap.put(86, "Aman");
    }
}

Producción:

Exception in thread "main" java.lang.IllegalArgumentException: key out of range
    at java.base/java.util.TreeMap$NavigableSubMap.put(TreeMap.java:1513)
    at GFG.main(File.java:29)

Implementación en la clase definida por el usuario:

En este ejemplo, en lugar de usar una clase contenedora como valor, use la clase definida por el usuario como valor y usará los métodos subMap(), headMap() y tailMap(). Este ejemplo es solo para una mejor comprensión y un mejor uso de esos tres métodos.

Implementación:

Java

// submap, headmap, and tailmap from Java
// TreeMap on User-Defined class
import java.util.Map;
import java.util.TreeMap;
// user defined class
class friendsDetail {
  
    // class field
    private String name;
    private String nickName;
  
    // parameterised constructor
    public friendsDetail(String name, String nickName)
    {
        this.name = name;
        this.nickName = nickName;
    }
  
    // getter for name
    public String getName() { return name; }
  
    // setter for name
    public void setName(String name) { this.name = name; }
  
    // getter for nickname
    public String getnickName() { return nickName; }
  
    // setter for nickname
    public void setNickName(int id)
    {
        this.nickName = nickName;
    }
  
    // overriding toString method
    public String toString()
    {
        // return super.toString();
        return "(" + this.getName() + ":"
            + this.getnickName() + ")";
    }
}
public class GFG {
  
    public static void main(String[] args)
    {
        // Mapping user defined class to Integer
        TreeMap<Integer, friendsDetail> originalTree
            = new TreeMap<>();
        originalTree.put(2029, new friendsDetail(
                                   "Raushan", "Chamgader"));
        originalTree.put(
            2022, new friendsDetail("Yashdeep", "Dopa"));
        originalTree.put(
            2019, new friendsDetail("Shishya", "Gorilla"));
        originalTree.put(
            2021, new friendsDetail("Sonika", "Chipkali"));
        originalTree.put(
            2025, new friendsDetail("Himanshu", "Lalten"));
        originalTree.put(
            2023, new friendsDetail("Sarthak", "Nagin"));
        originalTree.put(
            2020, new friendsDetail("Tsering", "Battak"));
        originalTree.put(
            2018, new friendsDetail("Abhishek", "Liquid"));
  
        // printing our original class
        System.out.println("This is original treemap");
        for (Map.Entry<Integer, friendsDetail> map :
             originalTree.entrySet()) {
            System.out.println("Name: " + map.getKey()
                               + " \t Name & Nickname: "
                               + map.getValue());
        }
  
        // printing submap from 2020 to 2024
        System.out.println("\n\nThis is submap");
        Map<Integer, friendsDetail> subMap
            = originalTree.subMap(2020, 2024);
        for (Map.Entry<Integer, friendsDetail> map :
             subMap.entrySet()) {
            System.out.println("Name: " + map.getKey()
                               + " \t Name & Nickname: "
                               + map.getValue());
        }
  
        // printing headmap for keys < 2020
        System.out.println("\n\nThis is headmap");
        Map<Integer, friendsDetail> headMap
            = originalTree.headMap(2020);
        for (Map.Entry<Integer, friendsDetail> map :
             headMap.entrySet()) {
            System.out.println("Name: " + map.getKey()
                               + " \t Name & Nickname: "
                               + map.getValue());
        }
  
        // printing tailmap for keys >=2025
        System.out.println("\n\nThis is tailmap");
        Map<Integer, friendsDetail> tailMap
            = originalTree.tailMap(2025);
        for (Map.Entry<Integer, friendsDetail> map :
             tailMap.entrySet()) {
            System.out.println("Name: " + map.getKey()
                               + " \t Name & Nickname: "
                               + map.getValue());
        }
    }
}
Producción

This is original treemap
Name: 2018      Name & Nickname: (Abhishek:Liquid)
Name: 2019      Name & Nickname: (Shishya:Gorilla)
Name: 2020      Name & Nickname: (Tsering:Battak)
Name: 2021      Name & Nickname: (Sonika:Chipkali)
Name: 2022      Name & Nickname: (Yashdeep:Dopa)
Name: 2023      Name & Nickname: (Sarthak:Nagin)
Name: 2025      Name & Nickname: (Himanshu:Lalten)
Name: 2029      Name & Nickname: (Raushan:Chamgader)


This is submap
Name: 2020      Name & Nickname: (Tsering:Battak)
Name: 2021      Name & Nickname: (Sonika:Chipkali)
Name: 2022      Name & Nickname: (Yashdeep:Dopa)
Name: 2023      Name & Nickname: (Sarthak:Nagin)


This is headmap
Name: 2018      Name & Nickname: (Abhishek:Liquid)
Name: 2019      Name & Nickname: (Shishya:Gorilla)


This is tailmap
Name: 2025      Name & Nickname: (Himanshu:Lalten)
Name: 2029      Name & Nickname: (Raushan:Chamgader)

Publicación traducida automáticamente

Artículo escrito por le0 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *