ArrayList 中 contains remove 判断元素相同 底层调用了 equals方法

50 篇文章 0 订阅
我们有时候需要判断arraylist容器中是否已经存了某个元素,那么调用contains(object)方法就可以了,他的返回值是boolean类型,若有已经存在某元素则返回true,如果不存在则返回false。

比如

package info.dyndns.oszc;

import java.util.*;

public class Utils {

	public static <E>void print(E e)
	{
		System.out.println(e);
	}
	public static <E>void printElements(ArrayList<E> al)
	{
		Iterator<E> it = al.iterator();
	
		while (it.hasNext()){
			print(it.next());
		}
	}
	public static <E>void put (ArrayList al,E e)
	{
		if (!al.contains(e))
			al.add(e);
	}
}

package info.dyndns.oszc;

import java.util.ArrayList;

public class ArrayListContainsTest {
	
	ArrayList<String> al;

	public void arrayListDemo(){
		al = new ArrayList<String>();
		Utils.put (al, "123");
		Utils.put (al, "234");
		Utils.put (al, "123");
		Utils.printElements(al);
	}
	
	public static void main(String argc[])
	{
		ArrayListContainsTest test = new ArrayListContainsTest();
		test.arrayListDemo();				
	}
	
}

output:

123
234

很显然第三个元素和第一个元素相同,所以没有被加入到元素ArrayList中去。
如果我们加入一个自定义元素呢?比如要在arrayList中储存一个自己定义的类,他是否还能这么智能的判断是否contain某个元素

public class telephone{

	private int type;
	private String name;

	public telephone (int typeVal, String nameVal){
		type = typeVal;
		name = nameVal;
	}

	public int getType(){
		return type;
	}

	public String getName(){
		return name;
	}
	public String toString(){
		return type +" "+ name;
	}
}

public class Test2{
	public static void main(String[] argc)
	{
		ArrayList<telephone> al = new ArrayList<telephone>();
		Utils.put(al, new telephone(1, "No.1"));
		Utils.put(al, new telephone(2, "No.2"));
		Utils.put(al, new telephone(1, "No.1"));
		
		Utils.printElements (al);
		
	}
	
}

output:

1 No.1
2 No.2
1 No.1

结果这三个都加到了arraylist的集合中去了。显然arraylist的contain并没有那么智能,那么app文档中contains是怎么定义的呢?

contains
public boolean contains(Object o)
如果此列表中包含指定的元素,则返回 true。更确切地讲,当且仅当此列表包含至少一个满足 (o==null ? e==null : o.equals(e)) 的元素 e 时,则返回 true。 

指定者:
接口 Collection<E> 中的 contains
指定者:
接口 List<E> 中的 contains
覆盖:
类 AbstractCollection<E> 中的 contains
参数:
o - 测试此列表中是否存在的元素 
返回:
如果此列表包含特定的元素,则返回 true

contains会和另一个object比较,并且符合(o==null ? e==null : o.equals(e))的条件,如果object 不是null,则比较o.equals(e)从而返回true或这false,所以要复写类的equals方法来加以判断,并且覆写的灵活性极强

程序如下 

package info.dyndns.oszc;
import java.util.ArrayList;

public class telephone {
	
	private int type;
	private String name;
	
	public boolean equals(Object o){  //覆写equals方法 这是关键
		if (!(o instanceof telephone))
		{
			return false;
		}

		telephone t = (telephone) o;
		return name.equals(t.getName()) && type == t.getType();   //返回值灵活性由自己控制

	}

	public telephone (int typeVal, String nameVal){
		type = typeVal;
		name = nameVal;
	}

	public int getType(){
		return type;
	}

	public String getName(){
		return name;
	}
	public String toString(){
		return type +" "+ name;
	}
	
	public static void main(String[] argc)
	{
		ArrayList<telephone> al = new ArrayList<telephone>();
		Utils.put(al, new telephone(1, "No.1"));
		Utils.put(al, new telephone(2, "No.2"));
		Utils.put(al, new telephone(1, "No.1"));
		
		Utils.printElements (al);
		
	}
}

output:

1 No.1
2 No.2

done ! 

我们在arraylist中使用contains时候千万注意其真正的底层调用的equals,而这个equals每个类中是可以由自己所覆写的,不同的写法也会导致不同的结果,要看具体情况。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值