import org.junit.Test;
import java.util.*;
/**
* 集合可以分为两个体系
* Collection接口:单列数据,定义了存取一组对象的方法集合。
* 子接口:
* List:元素有序,可重复的集合。--> “动态”数组
* 实现类:
* ArrayList,LinkedList,Vector
* Set:元素无序,不可重复的集合。--> 高中讲的“集合”
* 实现类:
* Hasset,LinkedHashSet,TreeSet
* Map接口:双列数据,保存具有映射关系“key-value”的集合。 --> 高中函数 y=f(x)
* 实现类:
* HashMap,LinkedHashMap,TreeMap,Hashtable,Properties
* @outor shkstart
* @create 2020-04-14 2:41
*/
public class CollectionTest {
@Test
public void test1(){
Collection coll =new ArrayList();
//add(Object o); 添加元素到集合coll中
coll.add("AA");
coll.add("BB");
coll.add("123"); //自动装箱
coll.add(new Date());
//size(); 获取添加的元素个数
System.out.println(coll.size());
//addAll(Collection e); //将另一个集合中的元素全部添加到当前集合中
Collection coll1=new ArrayList();
coll1.add(456);
coll1.add("cc");
coll1.addAll(coll);
System.out.println(coll1.size());
System.out.println(coll1);
//clear():清空集合
coll.clear();
//isEmpty(): 判断当前集合是否为空。
System.out.println(coll.isEmpty());
System.out.println(coll1.isEmpty());
}
//常用类与方法
@Test
public void test2(){
Collection coll =new ArrayList();
Person fengxin = new Person("fengxin", 20);
coll.add(false);
coll.add(123); //自动装箱
coll.add(new Date());
coll.add(new String("Tom"));
coll.add(fengxin);
/**1.contains(Object obj):判断当前集合是否包含该obj
* ①底层使用的是equals()
* ②向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()发法。
*/
boolean contains = coll.contains(123);
System.out.println(contains);
System.out.println(coll.contains(new String("Tom")));//String重写了equals()判断的是内容
System.out.println(coll.contains(fengxin)); //true
//未重写equals()方法System.out.println(coll.contains(new Person("fengxin",20)));//false 未重写equals(),判断的是引用
//重写equals()方法
System.out.println(coll.contains(new Person("fengxin",20)));//true
/**2.containsAll(Collection c):判断形参c中的所有元素是否都存在于当前集合中
*/
Collection coll1=Arrays.asList(123,456);
System.out.println(coll.containsAll(coll1));//false
}
@Test
public void test3(){
Collection coll =new ArrayList();
Person fengxin = new Person("fengxin", 20);
coll.add(false);
coll.add(123); //自动装箱
coll.add(new Date());
coll.add(new String("Tom"));
coll.add(fengxin);
/**
* 3.remove(Object obj):
* ①从当前集合中删除obj元素
* ②有返回值:boolean型,删除成功返回true反之false
*/
System.out.println(coll.remove(123));
System.out.println(coll);
/**
* 4.removeAll(Collection coll):从当前集合中移除coll1中所有的元素。
*/
Collection coll1=Arrays.asList(123,456);
coll.containsAll(coll1);
System.out.println(coll);
}
@Test
public void test4(){
Collection coll =new ArrayList();
Person fengxin = new Person("fengxin", 20);
coll.add(false);
coll.add(123); //自动装箱
coll.add(new Date());
coll.add(new String("Tom"));
coll.add(fengxin);
/**
*5. retainAll(Collection c):
* 1.获取当前集合和c集合的交集,返回并修改给当前集合(可以理解为删除当前集合中与c集合不一样的元素)
*/
// Collection coll1=Arrays.asList(123,456);
// coll.retainAll(coll1);
// System.out.println(coll);
/**
*6. equals(Object obj)
* 1.ArrayList()集合是有序的,如果元素一样,顺序不同返回false
*/
Collection coll1 =new ArrayList();
coll1.add(123); //自动装箱
coll1.add(false);
coll1.add(new Date());
coll1.add(new String("Tom"));
coll1.add(fengxin);
System.out.println(coll.equals(coll1));//true
}
@Test
public void test5(){
Collection coll =new ArrayList();
coll.add(123); //自动装箱
coll.add(false);
coll.add(new Date());
coll.add(new String("Tom"));
coll.add(new Person("fengxin",20));
//7. hashCode():返回哈希值
System.out.println(coll.hashCode());
/**
*7. 集合--->数组:toArray()
*/
Object[] objects = coll.toArray();
for (int i = 0; i <objects.length ; i++) {
System.out.println(objects[i]);
}
/**
*8. 数组--->集合:Arrays.asList()
*/
Collection coll1=Arrays.asList(new String[]{"AA","BB","CC"});
System.out.println(coll1);
List ints = Arrays.asList(new int[]{123, 456});
System.out.println(ints.size());//1 数组默认为一个元素
List ints1 = Arrays.asList(new Integer[]{123, 456});
System.out.println(ints1.size());//2 包装类不会出现这种情况
}
}
test1运行结果:
4
6
[456, cc, AA, BB, 123, Wed Apr 15 03:56:03 CST 2020]
true
false
test2运行结果:
true
true
true
true
false
test3运行结果:
true
[false, Wed Apr 15 03:56:51 CST 2020, Tom, Person{name=‘fengxin’, age=20}]
[false, Wed Apr 15 03:56:51 CST 2020, Tom, Person{name=‘fengxin’, age=20}]
test4运行结果:
false
test5运行结果:
444454928
123
false
Wed Apr 15 03:57:55 CST 2020
Tom
Person{name=‘fengxin’, age=20}
[AA, BB, CC]
1
2
自定义类
/**
* @outor shkstart
* @create 2020-04-14 17:27
*/
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = 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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", 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;
if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;
}
// @Override
// public int hashCode() {
// int result = name != null ? name.hashCode() : 0;
// result = 31 * result + age;
// return result;
// }
}