0. 先创建一个Person
类为例
public class Person {
private String name;
private int 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;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Person))
return false;
Person person = (Person) o;
if (getAge() != person.getAge())
return false;
return getName() != null ? getName().equals(person.getName()) : person.getName() == null;
}
@Override
public int hashCode() {
int result = getName() != null ? getName().hashCode() : 0;
result = 31 * result + getAge();
return result;
}
}
1. add()
:添加一个元素到集合中
public void testAdd(){
Collection<Object> collection = new ArrayList<>();
System.out.println(collection.size());
collection.add("AAA");
collection.add(new Person("Tom", 12));
collection.add(new Person("Jerry", 13));
collection.add(new Person("Lucy", 14));
System.out.println(collection.size());
}
2. addAll(Collection coll)
:添加一组元素到集合中
public void testAddAll(){
Collection<Object> collection2 = new ArrayList<>();
collection2.add("Citicbank");
Collection<Object> collection = new ArrayList<>();
collection.add("AAA");
collection.add(new Person("Tom", 12));
collection.add(new Person("Jerry", 13));
collection.add(new Person("Lucy", 14));
collection2.addAll(collection);
System.out.println("collection2.size() = " + collection2.size());
for(Object c : collection2){
System.out.println(c);
}
}
3. Iterator
迭代器
/*
* 在Collection 中无法获取指定元素 但可以遍历所有的元素
* 1. 使用增强的for循环
* 2. 使用Iterator迭代器
* 2.1 获取迭代器对象:调用Collection中的iterator()方法,获取Iterator接口的对象
* 2.2 调用Iterator 接口的方法进行迭代
*/
public void testIterator(){
Collection<Object> collection = new ArrayList<>();
collection.add("AAA");
collection.add(new Person("Tom", 12));
collection.add(new Person("Jerry", 13));
collection.add(new Person("Lucy", 14));
for(Object obj : collection){
System.out.println(obj);
}
System.out.println();
Iterator it = collection.iterator();
while(it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
}
4. 移除元素的方法
/*
* 1. clear():清空集合
* 2. remove():移除指定的元素 通过equals()方法在集合中查找指定的元素
* 若存在 则移除
*
* 3. removeAll(Collection coll) : 移除coll中有的元素
* 4. retainAll(Collection coll) : 保存coll中有的元素
*/
public void testRemove(){
Collection<Object> collection = new ArrayList<>(
collection.add("AAA");
Person p = new Person("Tom", 12);
collection.add(p);
collection.add(new Person("Jerry", 13));
collection.add(new Person("Lucy", 14));
Collection<Object> collection1 = new ArrayList<>
collection1.add("DEF");
collection1.add(new Person("Mike", 13));
collection1.add(new Person("Jerry", 13));
System.out.println("返回值为:" + collection.retainAl
System.out.println("collection = " + collection)
collection.remove(p);
collection.remove(new Person("Lucy", 14));
System.out.println(collection.size());
collection.clear();
System.out.println(collection.size());
}
5. 工具方法
public void testToolMethod(){
Collection<Object> collection = new ArrayList<>();
collection.add("AAA");
Person p = new Person("Tom", 12);
collection.add(p);
collection.add(new Person("Jerry", 13));
collection.add(new Person("Lucy", 14));
boolean flag = collection.contains(new Person("Jerry", 13));
System.out.println(flag);
Collection<Object> collection2 = new ArrayList<>();
collection2.add("ABCD");
collection2.add(new Person("Mike", 15));
System.out.println(collection.containsAll(collection2));
System.out.println(collection.isEmpty());
Object[] objs = collection.toArray();
System.out.println(objs.length);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)