有没有谁有希赛的网工视频,给我共享一下
2009-07-09 16:56
274 查看
set: 无序,不可以重复元素 |--HashSet : 数据结构是哈希表,线程是非同步的 保证元素唯一性的原理:判断元素的 hashCode值是否相同 |--Treeset : 可以对集合中的元素进行排序 两种比较方法:1.元素自身具有比较性 2.集合容器具有比较性 当两中排序都存在时,一比较器为主 定义一个类,实现Comparator接口,覆盖compare方法 import java.util.*; /* 当元素自身不具备比较性,或者具备的比较性不是所需要的。 这时需要让容器自身具备比较性。 定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。 当两种排序都存在时,以比较器为主。 定义一个类,实现Comparator接口,覆盖compare方法。 */ 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) { //return 0; 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; } } class TreeSetDemo2 { public static void main(String[] args) { TreeSet ts = new TreeSet(); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi02",21)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); ts.add(new Student("lisi06",18)); ts.add(new Student("lisi06",18)); ts.add(new Student("lisi007",29)); //ts.add(new Student("lisi007",20)); //ts.add(new Student("lisi01",40)); Iterator it = ts.iterator(); while(it.hasNext()) { Student stu = (Student)it.next(); System.out.println(stu.getName()+"..."+stu.getAge()); } } } class MyCompare implements Comparator { public int compare(Object o1,Object o2) { Student s1 = (Student)o1; Student s2 = (Student)o2; int num = s1.getName().compareTo(s2.getName()); if(num==0) { return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())); /* if(s1.getAge()>s2.getAge()) return 1; if(s1.getAge()==s2.getAge()) return 0; return -1; */ } return num; } }
相关文章推荐
- 哪位有没有reportbuilder的教程?共享一下。万分感谢!
- blackhat 2013 usa购买视频地址(谁买了能共享一下资源吗)
- 有没有人能够分享一下,音视频传输协议的源代码
- 有没有朋友可以共享一下会员账号的呀~
- 有没有大神能够逐行解释一下代码?Delphi
- 无责任共享 Coursera、Udacity 等课程视频(转载)
- 髙淸视频 窷迗室(耒懑⑩ハ岁 僸芷观看!)你看了没?-2012QQ群共享尾巴来啦
- 有没有大神级存在啊, 帮忙搞定一下
- 现在提交iOS应用,必须要提供 iPad Pro 的截图和视频么?有没有选项可以绕过去
- win7/win8通过媒体流(DLNA技术)共享音乐照片和视频
- 庆祝一下,Android视频采集+H264编码成功
- 今天整理了论坛很多关于dotnet(C#)的试题,大家共享一下
- 切尔西携手YouTube 英超第一家共享视频球队诞生
- dotnet(C#)的试题,大家共享一下
- 计划启动,有没有志向相投的伙伴,约一下!
- 全国各地DNS 和大家共享一下
- 共享一下SqlHelper(为后用)
- 视壮VS-RK3399开发板播放视频步骤共享
- 构造器练习,对着教学视频敲的,熟悉一下吧
- 关于做视频教程应该注意一下