集合 list set 常用类型说明
2015-07-19 18:54
225 查看
import java.util.ArrayList; import java.util.Iterator; /* * list元素有序,和重复 * list集合元素判断对象是否相等 依靠equals方法 * object的equals方法 this method returns true if and only if x and y refer to the same object (x == y has the value true) * 即是否指向了同一个object(同一地址) * */ class Person{ private String name; private int score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Person(String name, int score) { // TODO Auto-generated constructor stub this.name = name; this.score = score; } public boolean equals(Object obj) { if(!(obj instanceof Person)) return false; Person person = (Person)obj; return this.name.equals(person.name) && this.score == person.score; } } public class arrayListDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Person> arrayList = new ArrayList<Person>(); arrayList.add(new Person("a", 1)); arrayList.add(new Person("b", 2)); arrayList.add(new Person("c", 3)); arrayList.add(new Person("d", 4)); arrayList.add(new Person("c", 3)); arrayList.add(new Person("b", 8)); Person person = null; ArrayList<Person> newArrayList = new ArrayList<Person>(); Iterator<Person> iterator = arrayList.iterator(); while(iterator.hasNext()){ person = iterator.next(); if(!newArrayList.contains(person)) newArrayList.add(person); } iterator = newArrayList.iterator(); while(iterator.hasNext()){ person = iterator.next(); System.out.println(person.getName() + '\t' + person.getScore()); } } }
import java.util.HashSet; import java.util.Iterator; /* * Set 集合,无序且不能重复 * |--hashset 底层数据结构是哈希表 * hashset如何确保元素的唯一性:通过hashcode方法和equals方法来确认 * 先验证元素的hashcode方法 同再去验证equals方法 * 。。。。。 不同不回去验证equals方法 * * TreeSet 底层数据结构是二叉树 保证元素的唯一性通过compareTo 方法
* */ class Teacher{ private String name; private int score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Teacher(String name, int score) { // TODO Auto-generated constructor stub this.name = name; this.score = score; } @Override public int hashCode() { // TODO Auto-generated method stub return this.getName().hashCode() + this.getScore() * 36; } public boolean equals(Object obj) { if(!(obj instanceof Teacher)) return false; Teacher teacher = (Teacher)obj; return this.name.equals(teacher.name) && this.score == teacher.score; } } public class hashSetDemo { public static void main(String[] args) { // TODO Auto-generated method stub HashSet<Teacher> hashSet = new HashSet<Teacher>(); hashSet.add(new Teacher("a", 1)); hashSet.add(new Teacher("b", 2)); hashSet.add(new Teacher("c", 3)); hashSet.add(new Teacher("c", 3)); hashSet.add(new Teacher("d", 4)); Teacher teacher = null; Iterator<Teacher> iterator = hashSet.iterator(); while(iterator.hasNext()){ teacher = iterator.next(); System.out.println(teacher.getName() + "\t" + teacher.getScore()); } hashSet.remove(new Teacher("c", 3)); Iterator<Teacher> newIterator = hashSet.iterator(); System.out.println("********************"); while(newIterator.hasNext()){ teacher = newIterator.next(); System.out.println(teacher.getName() + "\t" + teacher.getScore()); } System.out.println("&&&&&&&&&&"); } }
相关文章推荐
- CAS学习笔记(二)
- LightOJ 1153 Internet Bandwidth
- jquery
- 暑假训练.2
- Logistic应用于二分类
- 李建忠:我的互联网信仰
- 3D数学 矩阵和线性变换之旋转
- POJ1700:Crossing River(过河问题)
- struts2步骤
- Python里面search()和match()的区别?
- input文本框实现宽度自适应
- 设计模式之命令模式
- struts2.xml
- CPPSockets开源发布!!
- PHP中读写文件+实现代码
- StringBuilder 以及 StringBuffer默认大小与扩容
- pat 1050. String Subtraction (20)
- HTTP返回状态码说明
- 如何用Python来进行查询和替换一个文本字符串
- css字体颜色动画