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("&&&&&&&&&&");
}
}