📌  相关文章
📜  如何从Java TreeMap 中删除第一个条目或最后一个条目?

📅  最后修改于: 2022-05-13 01:54:18.469000             🧑  作者: Mango

如何从Java TreeMap 中删除第一个条目或最后一个条目?

TreeMap是 Map 接口的实现类。它允许根据键对对象进行排序,排序可以是自然排序,也可以使用比较器。树图中也不保留插入顺序。

句法:

TreeMap map = new TreeMap<>();

在下面的示例中,我们将创建一个树图并使用 put() 方法在其中插入记录。由于树图根据键对记录进行排序,您可以看到输出是按照键从 a 到 z 的排序顺序进行的。为了迭代树映射,我们使用键 Set() 方法迭代树映射的所有键,该方法给出了一个集合中树映射的所有键。

执行:

Java
// Java Program to remove the first entry
// or last entry from TreeMap
import java.io.*;
import java.util.TreeMap;
import java.util.Set;
 
class GFG {
 
    public static void main(String[] args)
    {
 
        // treemap with keys as string and
        // values of type integer
        TreeMap gfg = new TreeMap<>();
 
        // adding values to treemap
        gfg.put("interviews", 97);
        gfg.put("fang", 86);
        gfg.put("competitve programming", 95);
        gfg.put("dsa", 99);
        gfg.put("java", 99);
        gfg.put("c++", 99);
 
        // iterating over the treemap
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
    }
}


Java
// Java Program remove the first entry
// or last entry from the TreeMap
import java.io.*;
import java.util.TreeMap;
import java.util.Set;
 
class GFG {
    public static void main(String[] args)
    {
 
        // treemap with keys as string and values of type
        // integer
        TreeMap gfg = new TreeMap<>();
 
        // adding values to tree map
        gfg.put("Interviews", 97);
        gfg.put("fang", 86);
        gfg.put("competitve programming", 95);
        gfg.put("dsa", 99);
        gfg.put("java", 99);
        gfg.put("c++", 99);
 
        System.out.println(
            "-------before removing first and last entry-------");
 
        // iterating over the treemap in java
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
 
        // removing and printing first entry
        System.out.println(
            "----printing the first removed entry----");
 
        System.out.println(gfg.pollFirstEntry());
 
        // removing and printing last entry
        System.out.println(
            "----printing the last removed entry");
 
        System.out.println(gfg.pollLastEntry());
 
        System.out.println(
            "-------after removing first and last entry-------");
 
        // iterating over the treemap
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
    }
}


Java
// Java Program to remove the first entry
// or last entry from the TreeMap
import java.io.*;
import java.util.TreeMap;
import java.util.Set;
 
class GFG {
    public static void main(String[] args)
    {
 
        // treemap with keys as string and values of type
        // integer
        TreeMap gfg = new TreeMap<>();
 
        gfg.put("Interviews", 97);
        gfg.put("fang", 86);
        gfg.put("competitve programming", 95);
        gfg.put("dsa", 99);
        gfg.put("java", 99);
        gfg.put("c++", 99);
 
        // iterating over the treemap in java
        System.out.println(
            "-------before removing first and last entry-------");
 
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
 
        // removing first entry through firstKey()
        gfg.remove(gfg.firstKey());
 
        // removing last entry through lastKey()
        gfg.remove(gfg.lastKey());
 
        System.out.println(
            "-------after removing first and last entry-------");
 
        // iterating over the treemap
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
    }
}


Java
// Java Program remove the first entry
// or last entry from the TreeMap
import java.io.*;
import java.util.TreeMap;
import java.util.Set;
 
class GFG {
    public static void main(String[] args)
    {
 
        // treemap with keys as string and values of type
        // integer
        TreeMap gfg = new TreeMap<>();
 
        gfg.put("Interviews", 97);
        gfg.put("fang", 86);
        gfg.put("competitve programming", 95);
        gfg.put("dsa", 99);
        gfg.put("java", 99);
        gfg.put("c++", 99);
 
        // iterating over the treemap in java
        System.out.println(
            "-------before removing first and last entry-------");
 
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
 
        // removing first entry through firstKey()
        String firstKey
            = gfg.keySet().stream().findFirst().get();
        gfg.remove(firstKey);
 
        // removing last entry through lastKey()
        String lastKey = gfg.keySet()
                             .stream()
                             .skip(gfg.size() - 1)
                             .findFirst()
                             .get();
        gfg.remove(lastKey);
 
        System.out.println(
            "-------after removing first and last entry-------");
 
        // iterating over the treemap
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
    }
}



输出
c++,99
competitve programming,95
dsa,99
fang,86
interviews,97
java,99

从 TreeMap 中删除第一个和最后一个条目

1. 使用 pollFirstEntry() 和 pollLastEntry() 方法:

TreeMap 有两种方法可以删除第一个和最后一个条目 .pollFirstEntry() 用于删除第一个条目和最后一个条目我们使用 pollLastEntry()。

句法:

mapObject.pollFirstEntry();
mapObject.pollLastEntry();

返回类型:两个方法的返回类型都是从地图中移除的条目对象。

首先,我们将创建一个 TreeMap 并使用 put() 方法在其中添加记录,然后使用 for 每个循环打印所有记录。对于打印,我们将使用通过 keySet() 方法可以获得的所有键。现在我们将在 TreeMap 上使用 pollFirstEntry() 和 pollLastEntry() 来删除第一个和最后一个条目。删除第一个和最后一个条目后,我们将再次使用 for each 循环遍历 TreeMap。

执行:

Java

// Java Program remove the first entry
// or last entry from the TreeMap
import java.io.*;
import java.util.TreeMap;
import java.util.Set;
 
class GFG {
    public static void main(String[] args)
    {
 
        // treemap with keys as string and values of type
        // integer
        TreeMap gfg = new TreeMap<>();
 
        // adding values to tree map
        gfg.put("Interviews", 97);
        gfg.put("fang", 86);
        gfg.put("competitve programming", 95);
        gfg.put("dsa", 99);
        gfg.put("java", 99);
        gfg.put("c++", 99);
 
        System.out.println(
            "-------before removing first and last entry-------");
 
        // iterating over the treemap in java
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
 
        // removing and printing first entry
        System.out.println(
            "----printing the first removed entry----");
 
        System.out.println(gfg.pollFirstEntry());
 
        // removing and printing last entry
        System.out.println(
            "----printing the last removed entry");
 
        System.out.println(gfg.pollLastEntry());
 
        System.out.println(
            "-------after removing first and last entry-------");
 
        // iterating over the treemap
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
    }
}


输出
-------before removing first and last entry-------
Interviews,97
c++,99
competitve programming,95
dsa,99
fang,86
java,99
----printing the first removed entry----
Interviews=97
----printing the last removed entry
java=99
-------after removing first and last entry-------
c++,99
competitve programming,95
dsa,99
fang,86

2. 使用lastKey() 和 firstKey()

句法:

map.remove(map.lastKey());
map.remove(map.firstKey());

方法:

首先,我们将创建 TreeMap 并使用 put() 方法在其中插入一条记录。现在我们将打印每个循环使用的所有记录,输出将按照从 a 到 z 的键的排序顺序。为了删除第一个条目和最后一个条目,我们将使用 remove(key) 方法。在 remove 方法中,我们需要将键作为参数传递。现在,如果我们以某种方式获得第一个和最后一个键,我们将能够删除第一个和最后一个条目。 TreeMap 为我们提供了两个这样的方法,它们可以为我们提供第一个和最后一个键,即 firstKey() 和 lastkey(),在获取键后,我们将在 remove() 方法中传递这些键,然后再次使用对于每个循环。

执行:

Java

// Java Program to remove the first entry
// or last entry from the TreeMap
import java.io.*;
import java.util.TreeMap;
import java.util.Set;
 
class GFG {
    public static void main(String[] args)
    {
 
        // treemap with keys as string and values of type
        // integer
        TreeMap gfg = new TreeMap<>();
 
        gfg.put("Interviews", 97);
        gfg.put("fang", 86);
        gfg.put("competitve programming", 95);
        gfg.put("dsa", 99);
        gfg.put("java", 99);
        gfg.put("c++", 99);
 
        // iterating over the treemap in java
        System.out.println(
            "-------before removing first and last entry-------");
 
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
 
        // removing first entry through firstKey()
        gfg.remove(gfg.firstKey());
 
        // removing last entry through lastKey()
        gfg.remove(gfg.lastKey());
 
        System.out.println(
            "-------after removing first and last entry-------");
 
        // iterating over the treemap
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
    }
}


输出
-------before removing first and last entry-------
Interviews,97
c++,99
competitve programming,95
dsa,99
fang,86
java,99
-------after removing first and last entry-------
c++,99
competitve programming,95
dsa,99
fang,86

3. 使用 Streams():

在这里,我们将使用流获取第一个和最后一个键,然后对其调用 remove 方法。

句法:

firsrkey = map.entrySet().stream().findFirst().get()
treemap.remove(firstkey);

lastkey = map.entrySet().stream().skip(map.size()-1).findFirst().get()
treemap.remove(key);

方法:

在这里,我们将创建一个 TreeMap 并使用 put() 方法在其中添加记录,然后使用 for 每个循环打印每条记录。现在我们将使用流来获取第一个和最后一个键。对于第一个键,我们将使用 keySet() 方法获取一组所有键,然后我们将在其上使用 stream(),现在我们将使用 findFirst() 从流中获取第一项(一条记录) ) 和 get() 以获取第一个键,然后在 remove 方法中传递它以删除第一个条目(一条记录)。

对于最后一个键,我们将再次对所有键使用 keySet(),然后使用 stream(),现在我们将跳过 n-1 个元素,因为如果我们跳过前 n-1 条记录,那么最终我们将到达最后一个记录。为了获得 n(总记录数或 TreeMap 的大小),我们使用 size() 方法。

执行:

Java

// Java Program remove the first entry
// or last entry from the TreeMap
import java.io.*;
import java.util.TreeMap;
import java.util.Set;
 
class GFG {
    public static void main(String[] args)
    {
 
        // treemap with keys as string and values of type
        // integer
        TreeMap gfg = new TreeMap<>();
 
        gfg.put("Interviews", 97);
        gfg.put("fang", 86);
        gfg.put("competitve programming", 95);
        gfg.put("dsa", 99);
        gfg.put("java", 99);
        gfg.put("c++", 99);
 
        // iterating over the treemap in java
        System.out.println(
            "-------before removing first and last entry-------");
 
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
 
        // removing first entry through firstKey()
        String firstKey
            = gfg.keySet().stream().findFirst().get();
        gfg.remove(firstKey);
 
        // removing last entry through lastKey()
        String lastKey = gfg.keySet()
                             .stream()
                             .skip(gfg.size() - 1)
                             .findFirst()
                             .get();
        gfg.remove(lastKey);
 
        System.out.println(
            "-------after removing first and last entry-------");
 
        // iterating over the treemap
        for (String key : gfg.keySet()) {
            System.out.println(key + "," + gfg.get(key));
        }
    }
}


输出
-------before removing first and last entry-------
Interviews,97
c++,99
competitve programming,95
dsa,99
fang,86
java,99
-------after removing first and last entry-------
c++,99
competitve programming,95
dsa,99
fang,86