博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从头认识java-15.5 使用LinkedHashSet须要注意的地方
阅读量:7056 次
发布时间:2019-06-28

本文共 2290 字,大约阅读时间需要 7 分钟。

再接着上一个章节。我们来聊一下使用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
{ public static
Set
fill(Set
set, Class
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
void test(Set
set, Class
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.class); test(new LinkedHashSet
(), SetType.class); test(new LinkedHashSet
(), 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
{ 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须要注意的地方。

这一章节就到这里,谢谢。

-----------------------------------

转载地址:http://ongol.baihongyu.com/

你可能感兴趣的文章
jq 学习资料
查看>>
Android apk打包及反编译
查看>>
借助 SublimeLinter 编写高质量的 JavaScript & CSS 代码
查看>>
dos删除文件包括权限有问题的文件
查看>>
Jquery——动画效果
查看>>
mysql存储过程获取sqlstate message_text
查看>>
My97DatePicker控件
查看>>
shell--read命令
查看>>
职称考试
查看>>
[转]HIVE UDF/UDAF/UDTF的Map Reduce代码框架模板
查看>>
OA系统配置文件
查看>>
我想知道的是这个月哪种商品销售量最高,比上个月怎么样?销量近几个月的走势是什么?有没有未达标的?有没有超额完成的?超额完成了多少?我可不关心这个月到底售出了多少件,几点售出的,谁买的(转)...
查看>>
百度面试
查看>>
Oracle数据库备份、恢复及常见问题
查看>>
把博客园的博客导出为MovableType的文本格式
查看>>
Entity Framework 6 Recipes 2nd Edition(10-9)译 -> 在多对多关系中为插入和删除使用存储过程...
查看>>
广告点击率预估中的特征选择
查看>>
LeetCode-Range Sum Query - Immutable
查看>>
VPython 3D图形库
查看>>
svn:revert to this version 和 revert changes from this version的区别 假设我们有许多个版本,版本号分别是1-10...
查看>>