集合Set和List相互转换

import java.util.*;

public class Set3 {
    public static void main(String[] args) {
        Random rand = new Random();
        //red 6 1-33
        Set<Integer> red = new HashSet<>();
        while(red.size()<6){
            red.add(rand.nextInt(1,34));
        }
        //blue 1 1-16
        int blue = rand.nextInt(1,17);
        System.out.println(red);
        System.out.println(blue);

        List<List> list = new ArrayList<>();
        var list1 = new ArrayList<Integer>(red);
        Collections.sort(list1);

        var list2 = new ArrayList<Integer>(List.of(blue));

        list.add(list1);
        list.add(list2);
        System.out.println(list);

        //Set - List 相互转换
        List<Integer> a = new ArrayList<>(List.of(11,22,33,11,44,11,55,11,66,77,88));
        System.out.println(a);

        //List -> Set
        Set<Integer> b = new HashSet<>(a);
        System.out.println(b);

        //Set->List
        List<Integer> c = new ArrayList<>(b);
        c.addAll(List.of(11,11,11,222,33));
        System.out.println(c);
    }
}


版权声明:本文为qq_58862767原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。