Java SortedSet size()方法及示例
SortedSet接口 的 size() 方法用于获取SortedSet的大小或SortedSet中存在的元素数量。
语法
int size()
参数: 该方法不需要任何参数。
返回值: 该方法返回SortedSet中元素的 大小 或数量。
注意 :SortedSet中的size()方法是继承自Java中的Set接口。
下面的程序说明了java.util.Set.size()方法。
// Java code to illustrate Set.size() method import java.util.*; public class SetDemo { public static void main(String args[]) { // Creating an empty Set SortedSet<String> set = new TreeSet<String>(); // Use add() method to // add elements into the Set set.add("Welcome"); set.add("To"); set.add("Geeks"); set.add("4"); set.add("Geeks"); // Displaying the Set System.out.println("Set: " + set); // Displaying the size of the Set System.out.println( "The size of the SortedSet is: " + set.size()); }}
输出:
Set: [4, Geeks, To, Welcome]The size of the SortedSet is: 4
参考资料 : https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#size()
