从头认识java-15.5 使用LinkedHashSet须要注意的地方
2017-07-19 14:47
513 查看
再接着上一个章节。我们来聊一下使用LinkedHashSet须要注意的地方。
LinkedHashSet特点:
(1)元素是有顺序的
(2)元素是不反复的
(3)底层数据结构是依照链表的结构存储的
(4)须要又一次hashcode和equals方法
样例:(我们再次改动上一章节的代码)
输出:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
注意:在TreeType里面的Comparable接口是没什么用的。
总结:我们这一章节简单展示了使用LinkedHashSet须要注意的地方。
这一章节就到这里,谢谢。
-----------------------------------
文件夹
LinkedHashSet特点:
(1)元素是有顺序的
(2)元素是不反复的
(3)底层数据结构是依照链表的结构存储的
(4)须要又一次hashcode和equals方法
样例:(我们再次改动上一章节的代码)
package com.ray.ch15; import java.lang.reflect.InvocationTargetException; import java.util.LinkedHashSet; import java.util.Set; public class Test<T> { public static <T> Set<T> fill(Set<T> set, Class<T> type) throws InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException { for (int i = 0; i < 10; i++) { set.add(type.getConstructor(int.class).newInstance(i)); } return set; } public static <T> void test(Set<T> set, Class<T> type) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { fill(set, type); fill(set, type); fill(set, type); System.out.println(set); } public static void main(String[] args) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { test(new LinkedHashSet<TreeType>(), TreeType.class); test(new LinkedHashSet<SetType>(), SetType.class); test(new LinkedHashSet<HashType>(), HashType.class); } } class SetType { private int id = 0; public int getId() { return id; } public void setId(int id) { this.id = id; } public SetType(int i) { id = i; } @Override public String toString() { return id + ""; } @Override public boolean equals(Object obj) { return obj instanceof SetType && (id == ((SetType) obj).id); } } class HashType extends SetType { public HashType(int i) { super(i); } @Override public int hashCode() { return getId(); } } class TreeType extends HashType implements Comparable<TreeType> { public TreeType(int i) { super(i); } @Override public int compareTo(TreeType o) { if (o.getId() > getId()) {// 排序 return -1; } else { if (o.getId() == getId()) {// 去重 return 0; } else { return 1; } } } }
输出:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
注意:在TreeType里面的Comparable接口是没什么用的。
总结:我们这一章节简单展示了使用LinkedHashSet须要注意的地方。
这一章节就到这里,谢谢。
-----------------------------------
文件夹
相关文章推荐
- 从头认识java-15.5 使用LinkedHashSet需要注意的地方
- 从头认识java-15.3 使用HashSet须要注意的地方
- 从头认识java-15.3 使用HashSet需要注意的地方
- 从头认识java-15.4 使用TreeSet需要注意的地方
- 使用 Java HashSet 时要注意的一些地方
- 从头认识java-12.2 Class对象(2)-使用与注意点
- 从头认识多线程-4.3 ThreadLocal使用时需要注意的地方
- 从头认识java-12.2 Class对象(2)-使用与注意点
- java二维数组的使用需要注意的地方
- java中HashSet、LinkedHashSet、TreeSet、ArrayList、ArrayDeque、LinkedList使用区别
- CMD下使用javac和java命令需要注意的地方
- Java中的Object的Wait() 和notify()方法使用时应注意的地方和Thread中的sleep()方法
- Java分割字符串spilt方法使用需注意的地方
- 使用Java中的final变量需要注意的地方。
- Java基础知识强化之集合框架笔记42:Set集合之LinkedHashSet的概述和使用
- java的clone()方法使用需要注意的地方
- 在java中使用正则表达式注意的地方
- Java中的Object的Wait() 和notify()方法使用时应注意的地方和Thread中的sleep()方法
- 使用Java的RTTI(运行时类型信息)要注意的地方
- java分割字符串split()使用需注意的地方