Trying to get lowest integers

前端 未结 3 489
广开言路
广开言路 2021-01-29 11:53

So I have 4 list, I want to find out which list.size(); is the lowest. I can find the lowest but some turn out to be the same, this is what I have.

                      


        
3条回答
  •  天涯浪人
    2021-01-29 12:46

    after lot of logic research i got solution,it works 100% and i tested it

    first found minimum sized list's size using treeset

    put all list and their sizes into map

    iterate map to find all lowest sized list(if there are more than one) using above lowest size as a key and put only those into finallist

    then take finalist and take random number using its size

    then pass this random number to final list get method get corresponding list randomly.

    List list1 = new ArrayList();
        list1.add(1);
        list1.add(2);
        List list2 = new ArrayList();
        list2.add(1);
        list2.add(2);
        List list3 = new ArrayList();
        list3.add(1);
        list3.add(2);
        list3.add(1);
        List list4 = new ArrayList();
        list4.add(1);
        list4.add(2);
        list4.add(1);
        list4.add(2);
    
    
        Set set = new TreeSet<>();
        set.add(list1.size());
        set.add(list2.size());
        set.add(list3.size());
        set.add(list4.size());
    
        List list = new ArrayList<>(set);
        int min = list.get(0);
        System.out.println(min);
    
    
        Map map = new HashMap<>();
    
        map.put(list1,list1.size());
        map.put(list2,list2.size());
        map.put(list3,list3.size());
        map.put(list4,list4.size());
    
    
    
        List finalList = new ArrayList<>();
    
        for (Map.Entry entry : map.entrySet())
        {
            if(entry.getValue().equals(min)){
                finalList.add(entry.getKey());
    
            }
    
        }
    
    
        int finalKey = new Random().nextInt(finalList.size());
    
        System.out.println(finalList.get(finalKey));
    

提交回复
热议问题