Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of Java collection should I use for this?

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.

like image 710
ShoeLace1291 Avatar asked Jan 18 '17 07:01

ShoeLace1291


People also ask

Which Java collection should I use?

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.

What types of collections does Java have?

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.

Should I use collection or list Java?

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.


1 Answers

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.

like image 137
Nicolas Filotto Avatar answered Nov 15 '22 15:11

Nicolas Filotto