import java.util.Objects;
/**
* @author xianyu
* @time 2019-08-12-16:30
*/
public class Person {
String name;
int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
/**
*
* 集合 collection
*
*
*
*
* @author xianyu
* @time 2019-08-12-16:28
*/
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class CollectionTest1 {
@Test
public void test1(){
Collection coll = new ArrayList();
//1. add()方法
coll.add(123); // 自动装箱 相当于 coll.add(new Integer(123))
coll.add(456);
coll.add(false); // 自动装箱
coll.add(new Person("Tom",23));
coll.add(new String("Hello"));
System.out.println(coll); //[123, 456, false, Person{name='Tom', age=23}, Hello] Person需要重写toString()方法
//2. addAll()方法 将指定集合中的所有元素添加到此集合
Collection col = Arrays.asList(250,500);
coll.addAll(col);
System.out.println(coll); //[123, 456, false, Person{name='Tom', age=23}, Hello, 250, 500]
//3. contains(Object o)方法 如果此集合包含指定的元素,则返回 true 会调用obj对象所在类的equals()。
boolean contains = coll.contains(123);
System.out.println(contains); //true
boolean contains1 = coll.contains(new Person("Tom",23));
System.out.println(contains1); //true 因为Person类重写了equals方法
//4. containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中。
Collection coll1 = Arrays.asList(123,456);
System.out.println(coll.containsAll(coll1)); //true
Collection coll2 = Arrays.asList(123,4567);
System.out.println(coll.containsAll(coll2)); //false
}
@Test
public void test2(){
Collection coll = new ArrayList();
//1. add()方法
coll.add(123); // 自动装箱 相当于 coll.add(new Integer(123))
coll.add(456);
coll.add(false); // 自动装箱
coll.add(new Person("Tom",23));
coll.add(new String("Hello"));
System.out.println(coll); //[123, 456, false, Person{name='Tom', age=23}, Hello]
//5. size() 返回此集合中的元素数。
System.out.println(coll.size()); //5
//6. isEmpty() 如果此集合不包含元素,则返回 true
System.out.println(coll.isEmpty()); //false
//7. 从此集合中删除所有元素(可选操作)
// coll.clear();
// System.out.println(coll); //[]
//
//8. hashCode() 返回此集合的哈希码值。
System.out.println(coll.hashCode()); //-1695552493
//9. remove(Object o) 从该集合中删除指定元素的单个实例(如果存在)
coll.remove("Hello");
System.out.println(coll); //[123, 456, false, Person{name='Tom', age=23}]
//10. removeall(Collection o) 删除指定集合中包含的所有此集合的元素(可选操作) 差集
Collection coll1 = Arrays.asList(123,456);
coll.removeAll(coll1);
System.out.println(coll); //[false, Person{name='Tom', age=23}]
}
@Test
public void test3(){
Collection coll = new ArrayList();
//1. add()方法
coll.add(123); // 自动装箱 相当于 coll.add(new Integer(123))
coll.add(456);
coll.add(false); // 自动装箱
coll.add(new Person("Tom",23));
coll.add(new String("Hello"));
Collection coll1 = new ArrayList();
coll1.add(123); // 自动装箱 相当于 coll.add(new Integer(123))
coll1.add(456);
coll1.add(false); // 自动装箱
coll1.add(new Person("Tom",23));
coll1.add(new String("Hello"));
//11. equals(Object o) 将指定的对象与此集合进行比较以获得相等性。要想返回true,需要当前集合和形参集合的元素都相同
System.out.println(coll.equals(coll1)); //true
//12. toArray() 返回一个包含此集合中所有元素的数组。
/*
* 123
* 456
* false
* Person{name='Tom', age=23}
* Hello
*
* */
Object[] arr = coll.toArray();
for (int i = 0; i< arr.length; i++){
System.out.println(arr[i]);
}
//13. retainAll(Collection<?> c) 仅保留此集合中包含在指定集合中的元素 交集
Collection coll2 = Arrays.asList(123,456,789);
coll.retainAll(coll2); //求coll和coll2的交集并赋给coll
System.out.println(coll); // [123, 456]
}
@Test
public void test4(){
Collection coll = new ArrayList();
coll.add(123); // 自动装箱 相当于 coll.add(new Integer(123))
coll.add(456);
coll.add(false); // 自动装箱
coll.add(new Person("Tom",23));
coll.add(new String("Hello"));
//14. iterator() 返回此集合中的元素的迭代器
Iterator iter = coll.iterator();
//方式1
// System.out.println(iter.next()); //123
// System.out.println(iter.next()); //456
// System.out.println(iter.next()); //false
// System.out.println(iter.next()); //Person{name='Tom', age=23}
// System.out.println(iter.next()); //Hello
//方式2 hasNext():判断是否还有下一个元素
// while(iter.hasNext()){
// //next():①指针下移 ②将下移以后集合位置上的元素返回
// System.out.println(iter.next());
// }
// 测试Iterator中的remove() ,删除Hello
while(iter.hasNext()){
Object obj = iter.next();
if("Hello".equals(obj)){
iter.remove();
}
}
iter = coll.iterator();
//遍历
while(iter.hasNext()){
System.out.println(iter.next());
}
}
}