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.
First time you need to determine the list sizes and put them in the map. After that you can group the map by the value and set results to another map. For this map you can use a count. Now, this number you should use as a parameter to random.
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<Integer> set = new TreeSet<>();
set.add(list1.size());
set.add(list2.size());
set.add(list3.size());
set.add(list4.size());
List<Integer> list = new ArrayList<>(set);
int min = list.get(0);
System.out.println(min);
Map<List, Integer> 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<List, Integer> 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));
Here's how I'd approach your requirements:
Define a Multimap which would store list.size
values and map it to the Set
of labels (or lists, depeding what you're interested in - not 100% clear from the question). So in your case multimap will store the following:
2 -> Set("Diamond", "Emerald")
3 -> Set("Gold")
4 -> Set("Iron")
Find lowest value of all multimap keys.
Get the set of labels for the lowest value.
Choose one of the labels at random.