2.2.1 添加元素
添加元素对象到当前集合中:
boolean add(E e);
添加另一个集合中的所有元素到当前集合中:
boolean addAll(Collection<? extends E> c);
示例:
package com.github.demo1;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("world");
System.out.println("collection = " + collection); // collection = [hello, world]
}
}
示例:
package com.github.demo2;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<>();
c1.add("aa");
c1.add("bb");
c1.add("cc");
Collection<String> c2 = new ArrayList<>();
c2.add("ee");
c2.add("ff");
// 将c2中的所有元素添加到c1中
c1.addAll(c2);
System.out.println("c1 = " + c1); // c1 = [aa, bb, cc, ee, ff]
}
}
示例:
package com.github.demo3;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<Object> c1 = new ArrayList<>();
c1.add("aa");
c1.add("bb");
c1.add("cc");
Collection<Object> c2 = new ArrayList<>();
c2.add("ee");
c2.add("ff");
c1.add(c2);
System.out.println("c1 = " + c1); // c1 = [aa, bb, cc, [ee, ff]]
}
}
2.2.2 删除元素
boolean remove(Object o);
从当前集合中删除所有与 coll 集合中相同的元素,即 this = this - this ∩ coll :
boolean removeAll(Collection<?> coll)
清空集合:
void clear();
删除满足指定条件的集合中所有元素:
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
示例:
package com.github.demo4;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<>();
c1.add("aa");
c1.add("bb");
c1.add("cc");
System.out.println("c1 = " + c1); // c1 = [aa, bb, cc]
c1.remove("aa");
System.out.println("c1 = " + c1); // c1 = [bb, cc]
}
}
示例:
package com.github.demo5;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<Object> c1 = new ArrayList<>();
c1.add("aa");
c1.add("bb");
c1.add("cc");
Collection<Object> c2 = new ArrayList<>();
c2.add("ee");
c2.add("ff");
c1.add(c2);
System.out.println("c1 = " + c1); // c1 = [aa, bb, cc]
c1.remove(c2);
System.out.println("c1 = " + c1); // c1 = [aa, bb, cc]
}
}
示例:
package com.github.demo6;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<>();
c1.add("aa");
c1.add("bb");
c1.add("cc");
Collection<String> c2 = new ArrayList<>();
c2.add("ee");
c2.add("ff");
c1.addAll(c2);
System.out.println("c1 = " + c1); // c1 = [aa, bb, cc, ee, ff]
c1.removeAll(c2);
System.out.println("c1 = " + c1); // c1 = [aa, bb, cc]
}
}
示例:
package com.github.day11.collection1;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
* @since 2023-01-25 23:02:42
*/
public class CollectionTest {
/**
* 删除满足指定条件的此集合中的所有元素
*/
@Test
public void test06() {
Collection<Integer> col = new ArrayList<>();
col.add(1);
col.add(2);
col.add(3);
col.add(4);
// 删除 >2 的元素
col.removeIf((i) -> i > 2);
System.out.println("col = " + col);// col = [1, 2]
}
}
2.2.3 判断
判断当前集合是否为空集合:
boolean isEmpty();
判断当前集合中是否存在一个与 obj 对象 equals 返回 true 的元素:
boolean contains(Object o);
判断 c 集合中的元素是否在当前集合中都存在,即 c 集合是否为当前集合的子集:
boolean containsAll(Collection<?> c)
示例:
package com.github.demo7;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<>();
c1.add("aa");
c1.add("bb");
c1.add("cc");
c1.add("dd");
System.out.println("c1 = " + c1.isEmpty()); // c1 = false
c1.clear();
System.out.println("c1 = " + c1.isEmpty()); // c1 = true
}
}
示例:
package com.github.demo8;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<>();
c1.add("aa");
c1.add("bb");
c1.add("cc");
c1.add("dd");
System.out.println("c1 = " + c1.contains("aa")); // c1 = true
System.out.println("c1 = " + c1.contains("aaa")); // c1 = false
}
}
示例:
package com.github.demo9;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<>();
c1.add("aa");
c1.add("bb");
c1.add("cc");
c1.add("dd");
Collection<String> c2 = new ArrayList<>();
c2.add("aa");
c2.add("bb");
c2.add("ee");
System.out.println("c1.containsAll(c2) = " + c1.containsAll(c2)); // c1.containsAll(c2) = false
Collection<String> c3 = new ArrayList<>();
c2.add("aa");
c2.add("bb");
System.out.println("c1.containsAll(c3) = " + c1.containsAll(c3)); // c1.containsAll(c3) = true
}
}
2.2.4 获取集合中元素的个数
int size();
示例:
package com.github.demo10;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<>();
c1.add("aa");
c1.add("bb");
c1.add("cc");
c1.add("dd");
System.out.println("c1.size() = " + c1.size()); // c1.size() = 4
c1.clear();
System.out.println("c1.size() = " + c1.size()); // c1.size() = 0
}
}
2.2.5 交集
boolean retainAll(Collection<?> c);
示例:
package com.github.demo11;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<>();
c1.add("aa");
c1.add("bb");
c1.add("cc");
c1.add("dd");
Collection<String> c2 = new ArrayList<>();
c2.add("bb");
c1.retainAll(c2);
System.out.println("c1 = " + c1); // c1 = [bb]
}
}
2.2.6 转数组
返回包含当前集合中所有元素的数组:
Object[] toArray();
<T> T[] toArray(T[] a);
示例:
package com.github.demo12;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
c.add("aa");
c.add("bb");
c.add("cc");
c.add("dd");
Object[] objects = c.toArray();
System.out.println("objects = " + Arrays.toString(objects)); // objects = [aa, bb, cc, dd]
String[] strings = c.toArray(new String[c.size()]);
System.out.println("strings = " + Arrays.toString(strings)); // strings = [aa, bb, cc, dd]
}
}