本文共 8397 字,大约阅读时间需要 27 分钟。
|----Collection接口:单列集合,用来存储一个一个的对象
|----List接口:存储有序的、可重复的数据。 -->“动态”数组 |----ArrayList、LinkedList、Vector |----Set接口:存储无序的、不可重复的数据 -->高中讲的“集合” |----HashSet、LinkedHashSet、TreeSetadd(Object obj)
将元素obj添加到集合中
@Test public void test1(){ Collection coll = new ArrayList(); coll.add("AA"); coll.add(123); coll.add(new Date()); System.out.println(coll); //[AA, 123, Thu Mar 18 23:25:47 CST 2021] }
addAll(Collection coll)
将另一个集合中的所有元素添加到当前集合中
@Test public void test1(){ Collection coll = new ArrayList(); coll.add("AA"); coll.add(123); coll.add(new Date()); System.out.println(coll); //[AA, 123, Thu Mar 18 23:25:47 CST 2021] System.out.println(coll.size());//3 Collection coll2 = new ArrayList(); coll2.add("sss"); coll2.add(456); coll.addAll(coll2); System.out.println(coll); //[AA, 123, Thu Mar 18 23:30:52 CST 2021, sss, 456] }
size()
获取添加元素的个数
@Test public void test1(){ Collection coll = new ArrayList(); coll.add("AA"); coll.add(123); coll.add(new Date()); System.out.println(coll); //[AA, 123, Thu Mar 18 23:25:47 CST 2021] System.out.println(coll.size());//3 }
boolean isEmpty()
判断是否是空集合
@Test public void test2(){ Collection coll3 = new ArrayList(); System.out.println(coll3.isEmpty()); //true }
/** * Returns true if this list contains no elements. * * @return true if this list contains no elements */ public boolean isEmpty() { return size == 0; }
void clear()
清空集合中所有的元素
@Test public void test2(){ Collection coll3 = new ArrayList(); coll3.add(111); System.out.println(coll3.isEmpty()); //false coll3.clear(); System.out.println(coll3.isEmpty()); //true }
boolean contains(Object obj)
是通过元素的equals
方法来判断是否是同一个对象,向Collection
接口的实现类的对象中添加数据obj
时,要求obj
所在类要重写equals()
@Test public void test3(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); System.out.println(coll4.contains(123)); //true System.out.println(coll4.contains(new String("hello"))); //true }
boolean containsAll(Collection c)
也是调用元素的equals
方法来比较的。拿两个集合的元素挨个比较。
@Test public void test3(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); Collection coll5 = new ArrayList(); coll5.add(456); coll5.add(false); System.out.println(coll4.containsAll(coll5)); //true }
boolean remove(Object obj)
通过元素的equals
方法判断是否是要删除的那个元素。只会删除找到的第一个元素。
@Test public void test3(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); System.out.println(coll4); //[123, 456, hello, false, Person{name='Tom', age=24}] coll4.remove(123); System.out.println(coll4); //[456, hello, false, Person{name='Tom', age=24}] }
boolean removeAll(Collection coll)
从当前集合中移除集合coll
中的所有元素
@Test public void test3(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); Collection coll5 = new ArrayList(); coll5.add(456); coll5.add(false); System.out.println(coll4); //[123, 456, hello, false, Person{name='Tom', age=24}] coll4.removeAll(coll5); System.out.println(coll4); //[123, hello, Person{name='Tom', age=24}] }
boolean retainAll(Collection c)
把交集的结果存在当前集合中,不影响集合c
@Test public void test3(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); Collection coll5 = new ArrayList(); coll5.add(456); coll5.add(789); System.out.println(coll4); //[123, 456, hello, false, Person{name='Tom', age=24}] coll4.retainAll(coll5); System.out.println(coll4); //[456] System.out.println(coll5); //[456, 789] }
boolean equals(Object obj)
比较两个集合是否相等
@Test public void test3(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); Collection coll5 = new ArrayList(); coll5.add(123); coll5.add(456); coll5.add(new String("hello")); coll5.add(false); coll5.add(new Person("Tom",24)); System.out.println(coll4.equals(coll5)); //true,要注意集合元素是有序的 }
hashCode()
返回当前对象的哈希值
@Test public void test3(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); System.out.println(coll4.hashCode()); //964169686 }
Object[] toArray()
返回Object类型数组
@Test public void test3(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); Object[] arr = coll4.toArray(); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } }
数组也可以转成集合,调用Arrays
类的静态方法asList()
Listlist = Arrays.asList(new String[]{ "AA", "BB", "CC"});
但是要注意的是下面这种情况
Listlist = Arrays.asList(new int[]{ 123,456});System.out.println(list); //[[I@22927a81]
如果是int
类型的数组,输出的结果是一个一维数组的地址,元素为int
类型。即相当于把整体结构也就是可变形参new int[]{123,456}
当成了一个元素,而不是把数组里面的int
类型的值当成多个元素。
应该修改为
List list = Arrays.asList(123,456);System.out.println(list); //[123, 456]//或者使用Integer包装类List list = Arrays.asList(new Integer[]{ 123,456});
iterator()
返回Iterator
接口实例对象,即迭代器对象,用于集合遍历。Iterator
对象称为迭代器(设计模式的一种),主要用于遍历 Collection
集合中的元素。Collection
接口继承了java.lang.Iterable
接口,该接口有一个iterator()
方法,那么所有实现了Collection
接口的集合类都有一个iterator()
方法,用以返回一个实现了Iterator
接口的对象。Iterator
仅用于遍历集合,Iterator
本身并不提供承装对象的能力。如果需要创建Iterator
对象,则必须有一个被迭代的集合。iterator()
方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。@Test public void IteratorTest(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); Iterator iterator = coll4.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } }
迭代器执行原理
//hasNext()方法判断是否还有下一个元素 while (iterator.hasNext()){ //next()指针下移,然后将下移以后集合位置上的元素返回 System.out.println(iterator.next()); }
Iterator
接口remove()
方法,可以删除集合的元素,但是是遍历过程中通过迭代器对象的remove
方 法,不是集合对象的remove
方法。 @Test public void IteratorTest(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); Iterator iterator = coll4.iterator(); while (iterator.hasNext()){ Object obj = iterator.next(); if ("Tom".equals(obj)){ //删除集合中Tom数据 iterator.remove(); } } iterator = coll4.iterator(); //需要重新获得一个迭代器从头开始遍历 while (iterator.hasNext()){ System.out.println(iterator.next()); } }}
Collection
或数组的长度,无需使用索引访问元素。Iterator
完成操作。foreach
还可以用来遍历数组。@Test public void ForeachTest(){ Collection coll4 = new ArrayList(); coll4.add(123); coll4.add(456); coll4.add(new String("hello")); coll4.add(false); coll4.add(new Person("Tom",24)); //for(集合元素的类型 局部变量 : 集合对象) for(Object obj : coll4){ System.out.println(obj); } }
转载地址:http://cnpi.baihongyu.com/