集合 list set 常用类型说明

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

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值