Java基础17-Set集合
Created At :
Count:814
Views 👀 :
Demo1_HashSet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public class Demo1_HashSet {
public static void main(String[] args) { HashSet<Person> hs = new HashSet<>(); hs.add(new Person("张三", 23)); hs.add(new Person("张三", 23)); hs.add(new Person("李四", 24)); hs.add(new Person("李四", 24)); hs.add(new Person("李四", 24)); hs.add(new Person("李四", 24)); System.out.println(hs); }
public static void demo1() { HashSet<String> hs = new HashSet<>(); boolean b1 = hs.add("a"); boolean b2 = hs.add("a"); hs.add("b"); hs.add("c"); hs.add("d"); System.out.println(hs); System.out.println(b1); System.out.println(b2); for (String string : hs) { System.out.println(string); } }
}
|
Demo2_LinkedHashSet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class Demo2_LinkedHashSet {
public static void main(String[] args) { LinkedHashSet<String> lhs = new LinkedHashSet<>(); lhs.add("a"); lhs.add("a"); lhs.add("a"); lhs.add("a"); lhs.add("b"); lhs.add("c"); lhs.add("d"); System.out.println(lhs); }
}
|
Demo3_TreeSet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| public class Demo3_TreeSet {
public static void main(String[] args) { TreeSet<String> ts = new TreeSet<>(new CompareByLen()); ts.add("aaaaaaaa"); ts.add("z"); ts.add("wc"); ts.add("nba"); ts.add("cba"); System.out.println(ts); }
public static void demo4() { TreeSet<Person> ts = new TreeSet<>(); ts.add(new Person("zhangsan", 23)); ts.add(new Person("lisi", 13)); ts.add(new Person("wangwu", 33)); ts.add(new Person("zhaoliu", 43)); ts.add(new Person("aaaa", 53)); System.out.println(ts); }
public static void demo3() { TreeSet<Person> ts = new TreeSet<>(); ts.add(new Person("李四", 13)); ts.add(new Person("张三", 23)); ts.add(new Person("王五", 43)); ts.add(new Person("赵六", 33)); System.out.println('张' + 0); System.out.println('李' + 0); System.out.println('王' + 0); System.out.println('赵' + 0); System.out.println(ts); }
public static void demo2() { TreeSet<Person> ts = new TreeSet<>(); ts.add(new Person("张三", 23)); ts.add(new Person("李四", 13)); ts.add(new Person("周七", 13)); ts.add(new Person("王五", 43)); ts.add(new Person("赵六", 33)); System.out.println(ts); }
public static void demo1() { TreeSet<Integer> ts = new TreeSet<>(); ts.add(3); ts.add(1); ts.add(1); ts.add(2); ts.add(2); ts.add(3); ts.add(3); System.out.println(ts); }
}
class CompareByLen /*extends Object*/ implements Comparator<String> {
@Override public int compare(String s1, String s2) { int num = s1.length() - s2.length(); return num == 0 ? s1.compareTo(s2) : num; } }
|
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1210331079@qq.com
Title:Java基础17-Set集合
Count:814
Author:千 羽
Created At:2020-06-16, 11:15:22
Updated At:2020-10-11, 18:49:29
Url:https://nateshao.github.io/2020/06/16/Java%E5%9F%BA%E7%A1%8017-Set%E9%9B%86%E5%90%88/
Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.