Java中TreeMap和TreeSet的相似之处

Java中TreeMap和TreeSet的相似之处

The TreeMap and TreeSet, both are the part of Collection Framework classes. There exist a few differences as well as a few similarities in their implementation and working. The TreeMap maintains key-value pair on the other hand the TreeSet does not have this feature. In this article, we will discuss the similarities between both classes of Collection Interface.

Collection Interface

In Java, collection is an object or we can say a container for simplicity that allows us to group several numbers of objects in a single unit. The collection interface is present at the root of all collection framework interfaces.

The following sub-interfaces of collection interface are implemented by TreeMap and TreeSet −

  • Map接口 − 它以键值对的形式存储元素。键是一个对象,用于获取和接收与之关联的值。

  • Set − It is the sub interface of java Collection Interface that doesn’t allow duplicate values. It is similar to mathematical set.

TreeMap

It is a class that is used to implement NavigableMap Interface. It stores the elements of the map in a tree structure. It provides an efficient alternative to store the key-value pairs in sorted order.

TreeMap的一般语法如下所示−

语法

TreeMap nameOfMap = new TreeMap(); 登录后复制