MENU

Set

June 5, 2018 • Read: 2526 • Java阅读设置

  1. Set接口中不能加入重复的元素,但可以排序
  2. Set接口子类:

无重复:HashSet
有序:TreeSet

public static void main(String args[]){
    Set<String> s1 = new HashSet<String>();
    s1.add("a");
    s1.add("b");
    s1.add("a");
    System.out.println(s1);//[a b]
    
    Set<String> s2 = new TreeSet<String>();
    s2.add("c");
    s2.add("a");
    s2.add("b");
    s2.add("a");
    System.out.println(s2);//[a b c]
}
Last Modified: March 21, 2019
Archives Tip
QR Code for this page
Tipping QR Code