Java TreeMap和TreeSet的相似之处

Java TreeMap和TreeSet的相似之处

TreeMap和TreeSet都是集合框架的一部分类。它们在实现和工作方式上存在一些差异和相似之处。TreeMap维护键值对,而TreeSet则没有这个特性。在本文中,我们将讨论Collection接口的这两个类之间的相似之处。

Collection接口

在Java中,集合是一个对象或者可以说是一个容器,它允许我们将多个对象分组成一个单元。集合接口位于所有集合框架接口的根部。

TreeMap和TreeSet都实现了集合接口的以下子接口:

  • Map接口 - 它以键值对的方式存储元素。键是用于获取和接收与之关联的值的对象。
  • Set - 这是java Collection接口的子接口,它不允许重复的值。它类似于数学中的集合。

TreeMap

它是一个用于实现NavigableMap接口的类。它以树结构存储地图的元素。它为按排序顺序存储键值对提供了一个高效的替代方法。

TreeMap的通用语法如下:

语法

TreeMap<TypeOfKey, TypeOfValue> nameOfMap = new TreeMap<>();

TreeSet

它是一个用于实现NavigableSet接口的类。它以树结构存储集合的元素。所有元素按照排序顺序存储,从而减少了检索时间。

TreeSet的一般语法如下所示−

语法

TreeSet<TypeOfSet> nameOfSet = new TreeSet<>();

TreeMap和TreeSet的Java程序

示例1

以下示例说明了TreeSet的使用方法。我们使用了该类的一些内置方法。

import java.util.*;
public class Srtset {
   public static void main(String args[]) {
     // Creating tree set
     TreeSet<String> treeSt = new TreeSet<>();
     // Adding elements in tree set
     treeSt.add("Tutorix");
     treeSt.add("Simply");
     treeSt.add("Easy");
     treeSt.add("Learning");
     treeSt.add("Tutorials");
     treeSt.add("Point");
     System.out.println("Elements in the given set: " + treeSt);
     String frst = treeSt.first(); 
     // to access first element
     System.out.println("Accessing First element of the given set: " + frst);
     String end = treeSt.last(); 
     // to access last element
     System.out.println("Accessing Last element of the given set: " + end);
     System.out.println("Accessing subsets of the given set: " + treeSt.subSet("Simply", "Tutorix"));
     System.out.println("Accessing first two elements of set: " + treeSt.headSet("Point"));
     System.out.println("Accessing last three elements of set: " + treeSt.tailSet("Simply"));
   }
}

输出

Elements in the given set: [Easy, Learning, Point, Simply, Tutorials, Tutorix]
Accessing First element of the given set: Easy
Accessing Last element of the given set: Tutorix
Accessing subsets of the given set: [Simply, Tutorials]
Accessing first two elements of set: [Easy, Learning]
Accessing last three elements of set: [Simply, Tutorials, Tutorix]

示例2

下面的示例说明了如何实现TreeMap。我们使用了该类的一些内置方法。

import java.util.*;
public class Srt {
   public static void main(String[] args) {
     TreeMap<String, Integer> workers = new TreeMap<>();
     // Adding elements in the workers map
     workers.put("Vaibhav", 4000);
     workers.put("Ansh", 3000);
     workers.put("Vivek", 1500);
     workers.put("Aman", 2000);
     workers.put("Tapas", 2500);
     // printing details workers map 
     System.out.println("Elements of the map: ");
     for (String unKey : workers.keySet()) {
       System.out.println("Name: " + unKey + ", Salary: " + workers.get(unKey));
     }
     String frstKey = workers.firstKey(); 
     // accessing first key
     String lstKey = workers.lastKey(); 
     // accessing last key
     System.out.println("Accessing name of first key in Map: " + frstKey);
     System.out.println("Accessing name of first key in Map: " + lstKey);
   }
}

输出

Elements of the map: 
Name: Aman, Salary: 2000
Name: Ansh, Salary: 3000
Name: Tapas, Salary: 2500
Name: Vaibhav, Salary: 4000
Name: Vivek, Salary: 1500
Accessing name of first key in Map: Aman
Accessing name of first key in Map: Vivek

TreeMap和TreeSet的相似之处

  • 默认情况下,它们的元素按照自然排序进行排序。例如,它们以字典顺序存储字符串和以数字顺序存储数字。

  • 由于元素已经排序,访问和检索时间变得更快。因此,TreeMap和TreeSet经常用于存储需要快速搜索的大量信息。

  • 不允许空值。

  • 它们定义在java.util包中。

  • 两者都支持Comparable接口,可以实现自定义排序顺序。

结论

在本文中,我们学习了集合框架的Map和Set接口。我们还了解了用于实现上述接口的TreeMap和TreeSet类。最后,我们讨论了一些解释了这些类之间相似之处的要点。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程