s-day03--Collections中的addAll和shuffle方法

This commit is contained in:
xx
2026-10-14 15:45:35 +08:00
parent 494b79dbf5
commit a344d77a44
@@ -0,0 +1,32 @@
package com.inmind.colletions04;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
/*
Collections的常用方法
static <T> boolean addAll(Collection<? super T> c, T... elements) 将所有指定的元素添加到指定的集合。
static void shuffle(List<?> list) 使用默认的随机源随机排列指定的列表。
*/
public class Demo11 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
/*list.add("刘备");
list.add("关羽");
list.add("张飞");
list.add("张飞");*/
//static <T> boolean addAll(Collection<? super T> c, T... elements) 将所有指定的元素添加到指定的集合。
Collections.addAll(list, "刘备1", "刘备2", "刘备3", "刘备3");
System.out.println(list);
System.out.println("----------------------");
HashSet<String> sets = new HashSet<>();
Collections.addAll(sets, "刘备5", "刘备6", "刘备6","刘备4");
System.out.println(sets);
//static void shuffle(List<?> list) 使用默认的随机源随机排列指定的列表。
Collections.shuffle(list);
System.out.println(list);
}
}