set集合特点级子类的特点
来源:互联网 发布:mmd古风动作数据 编辑:程序博客网 时间:2024/04/27 20:45
Set集合,无序,不可以重复元素
Set集合中的子类HashSet和TreeSet集合的特点
首先说一下HashSet: 数据结构是哈希表。线程是非同步的。
保证元素唯一性的原理:判断元素的hashCode值是否相同
如果相同,还会继续判断元素的equals方法,是否为true.
TressSet : 可以对Set集合中的元素进行排序。
底层数据结构是二叉树,
保证元素唯一性。
TreeSet排序的第一中方式:让元素自身具备比较性
元素需要实现Complare
注意:当主要条件相同时,一定要判断次要条件
上代码:
import java.util.*;
class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet ts=new TreeSet();
ts.add(new Student("学生1",22));
ts.add(new Student("学生2",20));
ts.add(new Student("学生3",20));
ts.add(new Student("学生4",40));
Iterator it=ts.iterator();
while(it.hasNext())
{
Student stu=(Student)it.next();
System.out.println(stu.getName()+"...."+stu.getAge());
}
}
}
class Student implements Comparable//该接口强制让学生具备比较性。
{
private String name;
private int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
public int compareTo(Object obj)
{
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s=(Student)obj;
System.out.println(this.name+"...compareto..."+s.name);
if(this.age>s.age)
return 1;
if(this.age==s.age)
{
return this.name.compareTo(s.name);//当主要条件满足判断次要条件
}
return -1;
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}}
- set集合特点级子类的特点
- Map集合的几个子类的特点
- 浅谈Set集合特点
- 集合框架--List和Set的特点
- 集合框架中子类特点
- List的子类特点
- 集合框架List的三个子类的特点
- 集合框架_List的三个子类的特点
- 集合框架(List的三个子类的特点)
- Set 接口常用子类及其特点
- Set 接口常用子类及其特点
- Set 接口常用子类及其特点
- set的特点
- java集合的特点
- Map集合的特点
- java 集合的特点
- 常用对象API(集合框架-List和Set的特点)
- python set集合的特点,功能and常见方法
- zlib编译
- Read a whole file into a string in C++
- CentOS-6.4-DVD系统中安装Oracle-11.2.0.4
- linux 的date命令及系统时间设置
- hdu 1059 Dividing
- set集合特点级子类的特点
- 将java的console日志写入文件
- Football Score
- [连载]Java程序设计(02)---任务驱动方式:个人所得税计算器
- 如何使用fio测试服务器的IOPS
- OC学习笔记之Foundatio框架集合类
- Codeforces Round #246 (Div. 2) A. Choosing Teams
- oracle根据列名查找包含此列名的表的名称
- expression动态构成