码迷,mamicode.com
首页 > 其他好文 > 详细

Set

时间:2015-05-27 08:34:20      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:

  1. HashSet doesn’t maintain any kind of order of its elements.
  2. TreeSet sorts the elements in ascending order.
  3. LinkedHashSet maintains the insertion order. Elements gets sorted in the same sequence in which they have been added to the Set.
import java.util.LinkedHashSet;
public class LinkedHashSetExample {
     public static void main(String args[]) {
         // LinkedHashSet of String Type
         LinkedHashSet<String> lhset = new LinkedHashSet<String>();

         // Adding elements to the LinkedHashSet
         lhset.add("Z");
         lhset.add("PQ");
         lhset.add("N");
         lhset.add("O");
         lhset.add("KK");
         lhset.add("FGH");
         System.out.println(lhset);

         // LinkedHashSet of Integer Type
         LinkedHashSet<Integer> lhset2 = new LinkedHashSet<Integer>();

         // Adding elements
         lhset2.add(99);
         lhset2.add(7);
         lhset2.add(0);
         lhset2.add(67);
         lhset2.add(89);
         lhset2.add(66);
         System.out.println(lhset2);
    }
}
[Z, PQ, N, O, KK, FGH]
[99, 7, 0, 67, 89, 66]

 http://beginnersbook.com/2013/12/linkedhashset-class-in-java-with-example/

 

Set

标签:

原文地址:http://www.cnblogs.com/lnas01/p/4532388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!