- package com.heima.hashset;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.Random;
- import java.util.TreeSet;
- public class Test_9 {
- public static void main(String[] args) {
- Random r1 = new Random();
- ArrayList<Integer> lis1 = new ArrayList<>();
- while (lis1.size()<5) {
- lis1.add(r1.nextInt(11));
- }
- TreeSet<Integer> t1 =new TreeSet<>(new Comparator<Integer>() {
- @Override
- public int compare(Integer o1, Integer o2) {
- int num = o1.compareTo(o2);
- return num ==0 ? 1 : num;
- }
- });
- t1.addAll(lis1);
- for (Integer i1 : t1) {
- System.out.println(i1);
-
- }
- }
- }
复制代码 |