Collection colls = new ArrayList();
colls.add(100);
colls.add("string");
colls.add(10.12);
colls.add(new Object());
colls.add(true);
System.out.println(colls.isEmpty());
System.out.println(colls.contains(false));
Object[] objects = colls.toArray();
for (Object o : objects) {
System.out.println(o);
}
System.out.println(colls.size());
System.out.println(colls.remove(10.12));
System.out.println(colls.size());
colls.clear();
System.out.println(colls.size());
/*
false
false
100
string
10.12
java.lang.Object@1b6d3586
true
5
true
4
0
Process finished with exit code 0*/
contains、remove方法
以ArrayList源码为例。
contains
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
由源码可知,contains方法底层是通过被参数去调用equals方法,从而去比较集合中的各个元素
存储到集合的元素强烈建议重写equals方法,不然在判断集合中是否包含某个对象时比较的是地址值
例:contains比较两个String元素
Collection c = new ArrayList();
String s1 = new String("123");
c.add(s1);
String x = new String("123");
System.out.println(c.contains(x));
/*
true
Process finished with exit code 0*/
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
Collection c = new ArrayList();
String s1 = new String("123");
c.add(s1);
String s2 = new String("123");
c.add(s2);
System.out.println(c.size());// 2
Sting x = new String("123");
c.remove(x);
System.out.println(c.size());//1
/*
2
1
Process finished with exit code 0*/
Collection list = new ArrayList();
c.add("hello");
c.add("word");
c.add(12153);
Iterator iterator = list.iterator();
while(iterator.hasNext()){
Object o = iterator.next();
if (o.equals(10.12))
iterator.remove(o);
}
/*
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at com.collection.IteratorBase.main(IteratorBase.java:7)
Process finished with exit code 1*/
Collection colls = new ArrayList();
colls.add(100);
colls.add("string");
colls.add(10.12);
colls.add(new Object());
colls.add(true);
System.out.println(colls.isEmpty());
System.out.println(colls.contains(false));
Object[] objects = colls.toArray();
for (Object o : objects) {
System.out.println(o);
}
System.out.println(colls.size());
System.out.println(colls.remove(10.12));
System.out.println(colls.size());
colls.clear();
System.out.println(colls.size());
/*
false
false
100
string
10.12
java.lang.Object@1b6d3586
true
5
true
4
0
Process finished with exit code 0*/
contains、remove方法
以ArrayList源码为例。
contains
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
由源码可知,contains方法底层是通过被参数去调用equals方法,从而去比较集合中的各个元素
存储到集合的元素强烈建议重写equals方法,不然在判断集合中是否包含某个对象时比较的是地址值
例:contains比较两个String元素
Collection c = new ArrayList();
String s1 = new String("123");
c.add(s1);
String x = new String("123");
System.out.println(c.contains(x));
/*
true
Process finished with exit code 0*/
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
Collection c = new ArrayList();
String s1 = new String("123");
c.add(s1);
String s2 = new String("123");
c.add(s2);
System.out.println(c.size());// 2
Sting x = new String("123");
c.remove(x);
System.out.println(c.size());//1
/*
2
1
Process finished with exit code 0*/
Collection list = new ArrayList();
c.add("hello");
c.add("word");
c.add(12153);
Iterator iterator = list.iterator();
while(iterator.hasNext()){
Object o = iterator.next();
if (o.equals(10.12))
iterator.remove(o);
}
/*
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at com.collection.IteratorBase.main(IteratorBase.java:7)
Process finished with exit code 1*/