I am fairly new to Java and am having trouble understanding the differences between different collections. I have a list of 19 resources. Each resource is assigned a hex color. I want to get a random resource with its color and use that resource with my code. Once I am finished with the current resource, I want to remove it from the list so that only a certain number of resources are used.
Should I use a dictionary, map, or hashtable? Or any others that I am missing.
The best general purpose or 'primary' implementations are likely ArrayList , LinkedHashMap , and LinkedHashSet . Their overall performance is better, and you should use them unless you need a special feature provided by another implementation. That special feature is usually ordering or sorting.
There are three generic types of collection: ordered lists, dictionaries/maps, and sets. Ordered lists allows the programmer to insert items in a certain order and retrieve those items in the same order. An example is a waiting list.
If the data is sequential such that the order of the elements matter and you are allowing duplicates, then use a list. If order of elements does not matter and duplicates may or may not be allowed, then use a collection.
What you could do, is store your resources into a List
, then randomly permutes it thanks to Collections.shuffle(List<?> list)
or shuffle(List<?> list, Random rnd)
, and finally call iterator()
on the resulting list to get an instance of Iterator
that you could store into a member field to be able to iterate over your list (thanks to hasNext()
/next()
) and remove your resource once done with remove()
.
Here is a pseudo code using String
as resource just to show the idea:
// Create a read/write list (Arrays.asList returns a read-only list)
List<String> resources = new ArrayList<>(Arrays.asList("1", "2", "3"));
System.out.printf("Initial state = %s%n", resources);
Collections.shuffle(resources);
System.out.printf("Permuted List = %s%n", resources);
Iterator<String> iterator = resources.iterator();
if (iterator.hasNext()) {
String resource = iterator.next();
// do something
iterator.remove();
System.out.printf("After remove = %s%n", resources);
}
Output:
Initial state = [1, 2, 3]
Permuted List = [3, 1, 2]
After remove = [1, 2]
NB: This approach makes sense in your case as you have a small list, please note that if you have a big list and you intend to retrieve only a small part of it, you should consider using a Random
to get randomly the index of the next element (using nextInt(int bound)
with list.size()
as parameter) to get (using get(int index)
) and remove (using remove(int index)
) instead of using Collections.shuffle(List<?> list)
as it would cause an overhead.
ArrayList
wouldn't work because I need to assign the color(value) to the resource(key)
Yes it can work if you use a List
of a wrapper class that will contain both, your color and your resource (for example AbstractMap.SimpleImmutableEntry
or simply a custom class). It is good enough as you don't seem to need to retrieve the color by resource. If you do, you could simply have a Map
with resource as key and color as value and use new ArrayList<>(map.keySet())
to initialize your list of resources, then you will be able to apply what is proposed previously in this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With