contains依赖于equals方法 验证栈是先进后出



import java.util.ArrayList;

/*
 * 存储自定义对象并去掉重复值。
 * 需求:如果一个对象的成员和另一个对象的成员完全相同,我们则认为它是同一个对象。
 * 
 * 这是时候,我们发现居然没有满足我们的需求,请问为什么呢?
 * 通过分析,我们最终定位到了contains方法。
 * 这个时候,我们应该去学习contains方法的源码,看看它到底怎么比较的。
 * 最终我们通过查看源码,发现contains方法底层依赖于equals方法。
 * 而学生类中默认的equals是继承自Object类。默认比较的是地址值。
 * 每一个对象都是new出来的,所以地址值肯定不同。所以它们全部都是不同的。就添加到集合中了。
 * 那么,我们该怎么办?
 * 回想我们的需求,发现我们要比较的是成员的内容,而不是对象的地址值。所以我们只需要重写equals比较内容即可。
 * 
 * 通过这道题目我想告诉大家:contains是依赖于equals方法。
 * 很多时候,如果我们想要完成某个动作,却发现我们调用JDK提供方法后和我们想要的效果不太一样,那么,我们就应该查看源码。看看源码到底依赖于什么。
 */
public class ArrayListDemo {
	public static void main(String[] args) {
		// 创建集合对象
		ArrayList<Student> arrayList = new ArrayList<>();

		// 创建元素对象
		Student s1 = new Student("貂蝉", 18);
		Student s2 = new Student("大乔", 27);
		Student s3 = new Student("小乔", 20);
		Student s4 = new Student("蔡文姬", 22);
		Student s5 = new Student("大乔", 27);
		Student s6 = new Student("大乔", 17);
		Student s7 = new Student("貂蝉", 27);

		arrayList.add(s1);
		arrayList.add(s2);
		arrayList.add(s3);
		arrayList.add(s4);
		arrayList.add(s5);
		arrayList.add(s6);
		arrayList.add(s7);
	 
		//  新建一个集合对象
		ArrayList<Student> arrayTemp = new ArrayList<Student>();
		// 遍历旧集合
		for (Student s : arrayList) {
			// 判断s是否在arr中存在,如果是,就不添加,如果不是,就添加。
			if (!arrayTemp.contains(s)) {
				arrayTemp.add(s);
			}
		}

		// 遍历集合
		for (Student s : arrayTemp) {
			System.out.println(s.getName() + "---" + s.getAge());
		}
	}
}
<pre name="code" class="java">

public class Student {
	private String name;
	private int age;

	public Student() {
		super();
	}

	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		System.out.println("-----------");
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}


}


 



import java.util.Stack;

/*
* Stack:通过查看API,我们发现它的父亲是Vector。
* class Stack<E> extends Vector<E> 
* 所以,我们就按照它父亲具备的功能来存储和遍历。
 * 继续查看API,我们发现原来它是一个栈数据结构,而这个结构的特点是:数据先进后出。 */
public class StackDemo {
	public static void main(String[] args) {
		// 创建结合对象
		Stack<String> stack = new Stack<>();

		// 创建并添加元素
		stack.add("hello");
		stack.add("world");
		stack.add("java");

		// 遍历集合
		for (String str : stack) {
			System.out.println(str);
		}
		System.out.println("----------------");


		while (!stack.isEmpty()) {
			String string = stack.pop();
			System.out.println(string);
		}
		// 验证栈是先进后出
		// java
		// world
		// hello
	}
}



 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值