我们有时候需要判断arraylist容器中是否已经存了某个元素,那么调用contains(object)方法就可以了,他的返回值是boolean类型,若有已经存在某元素则返回true,如果不存在则返回false。
很显然第三个元素和第一个元素相同,所以没有被加入到元素ArrayList中去。
如果我们加入一个自定义元素呢?比如要在arrayList中储存一个自己定义的类,他是否还能这么智能的判断是否contain某个元素
output:
结果这三个都加到了arraylist的集合中去了。显然arraylist的contain并没有那么智能,那么app文档中contains是怎么定义的呢?
contains会和另一个object比较,并且符合(o==null ? e==null : o.equals(e))的条件,如果object 不是null,则比较o.equals(e)从而返回true或这false,所以要复写类的equals方法来加以判断,并且覆写的灵活性极强
output:
done !
比如
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每个类中是可以由自己所覆写的,不同的写法也会导致不同的结果,要看具体情况。