[Java study notes] Common methods of Collection interface

table of Contents

1. Collection interface overview

2. Collection function overview

2.1 Add function

2.2 Delete function

2.3 Judgment function

2.4 Get function

2.5 Length function

2.6 Intersection function

2.7 Convert a collection to an array

3. Collection case

3.1 Store string and traverse

3.2 Store custom objects and traverse


1. Collection interface overview

Collection is a set of top level interface, Collection represents a group of objects, which are also referred to as C ollection elements. Some Collections allow duplicate elements, while others do not. Some collections are ordered, while others are disordered.

2. Collection function overview

2.1 Add function

  • boolean add(Object obj): add an element
  • boolean addAll(Collection c): add elements of a collection

2.2 Delete function

  • void clear(): remove all elements
  • boolean remove(Object obj): remove an element
  • boolean removeAll(Collection c): remove the elements of a collection

2.3 Judgment function

  • boolean contains(Object obj): Determine whether the set contains the specified element
  • boolean containsAll(Collection c): Determine whether the collection contains the specified collection element
  • boolean isEmpty(): Determine whether the collection is empty

2.4 Get function

  • Iterator<E> iterator(): Iterator, a special traversal method for collections

The member methods of the Iterator interface:

  • boolean hasNext(): Determine whether the current cursor has the next element.
  • Object next(): Get the element and move to the next position.
package com.hw.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Iterator iterator():迭代器,集合的专用遍历方式
 * 		boolean hasNext():判断当前游标是否有下一个元素。
 * 		Object next():获取元素,并移动到下一个位置。
 * 
 * @author HW
 *
 */
public class CollectionDemo3 {
	public static void main(String[] args) {
		// 创建集合对象
		Collection<String> c = new ArrayList<String>();
		// 添加元素
		c.add("Hello");
		c.add("World");
		c.add("JavaSE");
		
		// 通过集合对象获取迭代器对象
		Iterator<String> it = c.iterator();
		// 通过迭代器对象的hasNext()方法判断是否有元素
		while (it.hasNext()) {
			// 通过迭代器对象的next()方法获取元素,并移动到下一个位置
			System.out.println(it.next());
		}
	}
}

2.5 Length function

  • int size(): the number of elements

2.6 Intersection function

  • boolean retainAll(Collection c): elements that both collections have

For specific usage, refer to the following code:

// 创建集合对象c1
Collection<String> c1 = new ArrayList<String>();
// 添加元素
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
		
// 创建集合对象c2
Collection<String> c2 = new ArrayList<String>();
// 添加元素
c2.add("abc1");
c2.add("abc2");
c2.add("abc3");
c2.add("abc4");
		
/*
 * c1对c2做交集,最终的结果保存在c1中,c2不变
 * retainAll()返回值表示的是c1是否发生改变
 */
System.out.println("c1.retainAll(c2):" + c1.retainAll(c2));
System.out.println(c1);
System.out.println(c2);
		
System.out.println("=========================");
		
/*
 * c2对c1做交集,最终的结果保存在c2中,c1保持不变
 * retainAll()返回值表示的是c2是否发生改变
 */
System.out.println("c2.retainAll(c1):" + c2.retainAll(c1));
System.out.println(c1);
System.out.println(c2);

The results of the operation are as follows:

c1.retainAll(c2):false
[abc1, abc2, abc3]
[abc1, abc2, abc3, abc4]
=========================
c2.retainAll(c1):true
[abc1, abc2, abc3]
[abc1, abc2, abc3]

2.7 Convert a collection to an array

  • Object toArray(): Convert the collection to an array, which can realize the traversal of the collection

For specific usage, please refer to the following code:

// 创建集合对象
Collection<String> c = new ArrayList<String>();
// 添加元素
c.add("Hello");
c.add("World");
c.add("JavaSE");
		
// 把集合转换成数组
Object[] objs = c.toArray();
for (Object obj : objs) {
	String str = (String) obj;
	System.out.println(str + "---" + str.length());
}

3. Collection case

3.1 Store string and traverse

Analysis steps:

  • Create a collection object
  • Add element
  • Iterate over the collection
    • Obtain the iterator object through the collection object
    • Determine whether there are elements through the hasNext() method of the iterator object
    • Get the element through the next() method of the iterator object and move to the next position

code show as below:

package com.hw.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 存储字符串并遍历
 * 
 * @author HW
 * 
 */
public class CollectionTest {
	public static void main(String[] args) {
		// 创建集合对象
		Collection<String> c = new ArrayList<String>();
		// 添加元素
		c.add("Jmeter");
		c.add("Selenium");
		c.add("AirTest");

		// 通过集合对象获取迭代器对象
		Iterator<String> it = c.iterator();
		// 通过迭代器对象的hasNext()方法判断是否有元素
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}

3.2 Store custom objects and traverse

  • Student(name, age)

Analysis steps:

  • Create a collection object
  • Create student objects
  • Add the student object to the collection object
  • Iterate over the collection
    • Obtain the iterator object through the collection object
    • Determine whether there are elements through the hasNext() method of the iterator object
    • Get the element through the next() method of the iterator object and move to the next position

code show as below:

package com.hw.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 存储自定义对象并遍历
 * 
 * @author HW
 * 
 */
public class CollectionTest2 {
	public static void main(String[] args) {
		// 创集合对象
		Collection<Student> c = new ArrayList<Student>();

		// 创建学生对象
		Student s1 = new Student("貂蝉", 25);
		Student s2 = new Student("小乔", 16);
		Student s3 = new Student("黄月英", 20);
		Student s4 = new Student();
		s4.setName("西施");
		s4.setAge(23);

		// 把学生对象添加到集合中
		c.add(s1);
		c.add(s2);
		c.add(s3);
		c.add(s4);
		// 匿名对象
		c.add(new Student("杨贵妃", 18));

		// 遍历集合
		// 通过集合对象获取迭代器对象
		Iterator<Student> it = c.iterator();
		// 通过迭代器对象的hasNext()方法判断是否有元素
		while (it.hasNext()) {
			// 通过迭代器对象的next()方法获取元素,并移动到下一个位置
			Student s = it.next();
			System.out.println(s.getName() + "---" + s.getAge());
		}
	}
}

 

Guess you like

Origin blog.csdn.net/weixin_44679832/article/details/105447924