What Collection/List to use - java

I've got Treasure objects in a TreasureCollectionDB class.
The TreasureCollectionDB class has a Map<Long, Treasure> (long being an id generated by the TreasureCollectionDB) called treasures
and a second data collection/list (available treasures).
The thing I need the other Collection or List to do is hold Treasures which I will add/remove through JSP pages. The Treasures in this list should be unique, but sorted alphabetically (if there's no data holder that does this by it self, I will write a sort method).
Anyone know what data holder I should use? Answers on the internet are confusing me as to which is most suitable.

You may use TreeSet, that should give you the desired results.
Set doesn't allow duplicates and Tree maintains sorted order.
The Treasures may implement Comparable interface so that you can sort on the desired field(s).

You would need to create equals and hash code methods and also write a comparator. That comparator you may pass to a TreeSet and use SortedSet interface.

Related

ArrayList - Creating comparators

I created an ArrayList that contains references to objects that contain various amounts of data.
I wish to sort the object by certain elements of their data.
I successfully did so using the collections.sort along with an anonymous class defining the new comparator.
I was successful in each of the new comparators I created which resulted in correct sorted order.
For the same ArrayList how could you make a comparator that would give the original input of the ArrayList order. I know that the ArrayList saves the order of the objects they are added, insertion order. But after sorting the ArrayList with other comparators, how would I then sort the ArrayList back to the original order that it was in? This is the one thing that is stumping me, and I can't seem to figure it out.
Edit: I should clarify this is for an assignment, in which the instructions say to include a default/original comparator to get the unsorted input. That just doesn't make sense to me, given what we mentioned. The ArrayList will save the insertion order when it is added, but when you sort that it is lost. i don't see how you could create a comparator that will sort what the original list was.
Edit2: I am given an ArrayList which is expected to store a few objects that contain some data elements which will be sorted, such as by their names or their age. This comparators were easy to make and I do so successfully. The assignment also wants to an original comparator that display the data as it was inserted. Now these comparators are being passed in a method call, in which that method will use the collections.sort on each different comparator. It is asking for the original comparator to be used first though.
The idea of the original comparator at all seems illogical. The fact that is being used first, so no other sorting has been done yet makes it seem that it was only made so it fits the argument list of calling the method to display the data. So in other words, I guess the original comparator should just return nothing..?
It is asking for the original comparator to be used first though.
This means that the comparator doesn't need to restore the original order, which is what wouldn't make any sense (impossible), but to do nothing when used to sort, i.e. leave the original order unchanged.
Quoting javadoc of sort():
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
This means that if you pass in a comparator that claims all objects to be equal, no reordered will take place.
// Java 8 example using lambda expression
Collections.sort(list, (a,b) -> 0); // No-op. All elements compare equal
Aside from making a clone (commented above) would be for the object to contain a separate field that tracks order inserted into the array and write a comparator for that.

How do i keep a hashset alphabetically ordered?

I have a collection of a big number of objects that are defined by name/value pairs.
I need to have fast access to any of their values and to be able to return them ordered alphabetically by name. First I thought I might use a HashMap to get fast access. But it gave me no ordering. I decided to switch to LinkedHashSet.
The problem with it is that I need to be able to insert new Objects in the right places of the list, but LinkedHashSet doesn't allow that. I also need to be able to access Objects by their index as well as by name.
Will be thankful for any ideas.
Why not try TreeSet. Does your list not allow duplicates? If so then the Set should be ok. As you are adding strings and this implements Comparator the set will be automatically sorted for you
If you had
Set<String> s = new TreeSet<String>();
s.add("B");
s.add("C");
s.add("A");
then the contents of the set would be A, B, C
You can use TreeMap
A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
I would use a TreeSet which is a SortedSet. You need to define your custom class as Comparable based on the name and your collection will always be sorted.
Note: sorted collections have an O(log N) access time.
Have you looked at TreeMap? It's based off of Red-Black trees which help maintain ordering, but still gives fast access.
A TreeMap should address your requirements. If your keys are not literals then use appropriate Comparator in TreeMap constructor.

Java - maintaining order of collection of objects

How would you go about maintaining an order of collection business objects
<BO1, BO2, BO3, BO4>
so that when you remove BO2, amd BO4 you get
<BO1, BO3>
and then when you add BO2
<BO1, BO2, BO3>
You have several ways of doing that but it depends of the type of collection you want to use. Obviously, you don't want to maintain the order of insertions but an order based on the type of elements in the list.
So, before saying use this or that, ask yourself the following question:
Can my collection hold duplicate elements?
1) If YES: then you could use an implementation of a List object (ArrayList, LinkedList, etc). But you will need to sort the list after each insertion:
List<MyObj> list = ...
list.add(myObjInstance);
Collections.sort(list);
To avoid having to sort the list on each insertion you could use the TreeList implementation from Apache Commons Collections.
2) If the answer to the previous question is NO. Then use a TreeSet, you won't need sort the collection on each insertion with that implementation.
Be aware that your object elements have to implement the Comparable interface in order to be sortable.
Unless you use a sorted order, I don't see how the collection is supposed to know that BO2 should go in the middle.
This will do what you want if your Business object implement Comparable
SortedSet<BusObj> bos = new TreeSet<>();
bos.addAll(Arrays.asList(bo1, bo2, bo3, bo4));
bos.removeAll(Arrays.asList(bo2, bo4));
bos.add(bo2);
Make your business object Comparable and use a sorted collection (like TreeSet which is a SortedSet).
Use a SortedSet
http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html
There are 2 options: use a List and do the sorting yourself by inserting at the proper location or use a sorted collection.
The sorted collection I think you want is SortedSet http://docs.oracle.com/javase/6/docs/api/java/util/SortedSet.html.
The SortedSet requires entries to implement the Comparable interface.
There is also another question that you should look at: Sorted collection in Java
Aswering my question:
I guess also PriorityQueue would be a solution, if one were not interested in the random access.

What is the difference between Lists, ArrayLists, Maps, Hashmaps, Collections etc..?

I've been using HashMaps since I started programming again in Java without really understanding these Collections thing.
Honestly I am not really sure if using HashMaps all the way would be best for me or for production code. Up until now it didn't matter to me as long as I was able to get the data I need the way I called them in PHP (yes, I admit whatever negative thing you are thinking right now) where $this_is_array['this_is_a_string_index'] provides so much convenience to recall an array of variables.
So now, I have been working with java for more than 3 months and came across the Interfaces I specified above and wondered, why are there so many of these things (not to mention, vectors, abstractList {oh well the list goes on...})?
I mean how are they different from each other?
And more importantly, what is the best Interface to use in my case?
The API is pretty clear about the differences and/or relations between them:
Collection
The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered.
http://download.oracle.com/javase/6/docs/api/java/util/Collection.html
List
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
http://download.oracle.com/javase/6/docs/api/java/util/List.html
Set
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
http://download.oracle.com/javase/6/docs/api/java/util/Set.html
Map
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
http://download.oracle.com/javase/6/docs/api/java/util/Map.html
Is there anything in particular you find confusing about the above? If so, please edit your original question. Thanks.
A short summary of common java collections:
'Map': A 'Map' is a container that allows to store key=>value pair. This enables fast searches using the key to get to its associated value. There are two implementations of this in the java.util package, 'HashMap' and 'TreeMap'. The former is implemented as a hastable, while the latter is implemented as a balanced binary search tree (thus also having the property of having the keys sorted).
'Set': A 'Set' is a container that holds only unique elements. Inserting the same value multiple times will still result in the 'Set' only holding one instance of it. It also provides fast operations to search, remove, add, merge and compute the intersection of two sets. Like 'Map' it has two implementations, 'HashSet' and 'TreeSet'.
'List': The 'List' interface is implemented by the 'Vector', 'ArrayList' and 'LinkedList' classes. A 'List' is basically a collection of elements that preserve their relative order. You can add/remove elements to it and access individual elements at any given position. Unlike a 'Map', 'List' items are indexed by an int that is their position is the 'List' (the first element being at position 0 and the last at 'List.size()'-1). 'Vector' and 'ArrayList' are implemented using an array while 'LinkedList', as the name implies, uses a linked list. One thing to note is, unlike php's associative arrays (which are more like a Map), an array in Java and many other languages actually represents a contiguous block of memory. The elements in an array are basically laid out side by side on adjacent "slots" so to speak. This gives very fast lookup and write times, much faster than associative arrays which are implemented using more complex data structures. But they can't be indexed by anything other than the numeric positions within the array, unlike associative arrays.
To get a really good idea of what each collection is good for and their performance characteristics I would recommend getting a good idea about data structures like arrays, linked lists, binary search trees, hashtables, as well as stacks and queues. There is really no substitute to learning this if you want to be an effective programmer in any language.
You can also read the Java Collections trail to get you started.
In Brief (and only looking at interfaces):
List - a list of values, something like a "resizable array"
Set - a container that does not allow duplicates
Map - a collection of key/value pairs
A Map vs a List.
In a Map, you have key/value pairs. To access a value you need to know the key. There is a relationship that exists between the key and the value that persists and is not arbitrary. They are related somehow. Example: A persons DNA is unique (the key) and a persons name (the value) or a persons SSN (the key) and a persons name (the value) there is a strong relationship.
In a List, all you have are values (a persons name), and to access it you need to know its position in the list (index) to access it. But there is no permanent relationship between the position of the value in the list and its index, it is arbitrary.
■ List — An ordered collection of elements that allows duplicate entries
Concrete Classes:
ArrayList — Standard resizable list.
LinkedList — Can easily add/remove from beginning or end.
Vector — Older thread-safe version of ArrayList.
Stack — Older last-in, first-out class.
■ Set — Does not allow duplicates
Concrete Classes:
HashSet—Uses hashcode() to find unordered elements.
TreeSet—Sorted and navigable. Does not allow null values.
■ Queue — Orders elements for processing
Concrete Classes:
LinkedList — Can easily add/remove from beginning or end.
ArrayDeque—First-in, first-out or last-in, first-out. Does not allow null values.
■ Map — Maps unique keys to values
Concrete Classes:
HashMap — Uses hashcode() to find keys.
TreeMap — Sorted map. Does not allow null keys.
Hashtable — Older version of hashmap. Does not allow null keys or values.
That is a question that ultimately has a very complex answer--there are entire college classes dedicated to data structures. The short answer is that they all have trade-offs in memory usage and the speed of various operations.
What would be really healthy is some time with a nice book on data structures--I can almost guarantee that your code will improve significantly if you get a nice understanding of data structures.
That said, I can give you some quick, temporary advice from my experience with Java. For most simple internal things, ArrayList is generally preferred. For passing collections of data about, simple arrays are generally used. HashMap is only really used for cases when there is some logical reason to have special keys corresponding to values--I haven't seen anyone use them as a general data structure for everything. Other structures are more complicated and tend to be used in special cases.
As you already know, they are containers for objects. Reading their respective APIs will help you understand their differences.
Since others have described what are their differences about their usage, I will point you to this link which describes complexity of various data structures.
This list is programming language agnostic, and, as always, real world implementations will vary.
It is useful to understand complexity of various operations for each of these structures, since in the real world, it will matter if you're constantly searching for an object in your 1,000,000 element linked list that's not sorted. Performance will not be optimal.
List Vs Set Vs Map
1) Duplicity: List allows duplicate elements. Any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes.
Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements.
Map stored the elements as key & value pair. Map doesn’t allow duplicate keys while it allows duplicate values.
2) Null values: List allows any number of null values.
Set allows single null value at most.
Map can have single null key at most and any number of null values.
3) Order: List and all of its implementation classes maintains the insertion order.
Set doesn’t maintain any order; still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order.
Similar to Set Map also doesn’t stores the elements in an order, however few of its classes does the same. For e.g. TreeMap sorts the map in the ascending order of keys and LinkedHashMap sorts the elements in the insertion order, the order in which the elements got added to the LinkedHashMap.enter code here
List Vs Set Vs Map
1) Duplicity: List allows duplicate elements. Any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes.
Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements.
Map stored the elements as key & value pair. Map doesn’t allow duplicate keys while it allows duplicate values.
2) Null values: List allows any number of null values.
Set allows single null value at most.
Map can have single null key at most and any number of null values.
3) Order: List and all of its implementation classes maintains the insertion order.
Set doesn’t maintain any order; still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order.
Similar to Set Map also doesn’t stores the elements in an order, however few of its classes does the same. For e.g. TreeMap sorts the map in the ascending order of keys and LinkedHashMap sorts the elements in the insertion order, the order in which the elements got added to the LinkedHashMap.
Difference between Set, List and Map in Java -
Set, List and Map are three important interface of Java collection framework and Difference between Set, List and Map in Java is one of the most frequently asked Java Collection interview question. Some time this question is asked as When to use List, Set and Map in Java. Clearly, interviewer is looking to know that whether you are familiar with fundamentals of Java collection framework or not. In order to decide when to use List, Set or Map , you need to know what are these interfaces and what functionality they provide. List in Java provides ordered and indexed collection which may contain duplicates. Set provides an un-ordered collection of unique objects, i.e. Set doesn't allow duplicates, while Map provides a data structure based on key value pair and hashing. All three List, Set and Map are interfaces in Java and there are many concrete implementation of them are available in Collection API. ArrayList and LinkedList are two most popular used List implementation while LinkedHashSet, TreeSet and HashSet are frequently used Set implementation. In this Java article we will see difference between Map, Set and List in Java and learn when to use List, Set or Map.
Set vs List vs Map in Java
As I said Set, List and Map are interfaces, which defines core contract e.g. a Set contract says that it can not contain duplicates. Based upon our knowledge of List, Set and Map let's compare them on different metrics.
Duplicate Objects
Main difference between List and Set interface in Java is that List allows duplicates while Set doesn't allow duplicates. All implementation of Set honor this contract. Map holds two object per Entry e.g. key and value and It may contain duplicate values but keys are always unique. See here for more difference between List and Set data structure in Java.
Order
Another key difference between List and Set is that List is an ordered collection, List's contract maintains insertion order or element. Set is an unordered collection, you get no guarantee on which order element will be stored. Though some of the Set implementation e.g. LinkedHashSet maintains order. Also SortedSet and SortedMap e.g. TreeSet and TreeMap maintains a sorting order, imposed by using Comparator or Comparable.
Null elements
List allows null elements and you can have many null objects in a List, because it also allowed duplicates. Set just allow one null element as there is no duplicate permitted while in Map you can have null values and at most one null key. worth noting is that Hashtable doesn't allow null key or values but HashMap allows null values and one null keys. This is also the main difference between these two popular implementation of Map interface, aka HashMap vs Hashtable.
Popular implementation
Most popular implementations of List interface in Java are ArrayList, LinkedList and Vector class. ArrayList is more general purpose and provides random access with index, while LinkedList is more suitable for frequently adding and removing elements from List. Vector is synchronized counterpart of ArrayList. On the other hand, most popular implementations of Set interface are HashSet, LinkedHashSet and TreeSet. First one is general purpose Set which is backed by HashMap , see how HashSet works internally in Java for more details. It also doesn't provide any ordering guarantee but LinkedHashSet does provides ordering along with uniqueness offered by Set interface. Third implementation TreeSet is also an implementation of SortedSet interface, hence it keep elements in a sorted order specified by compare() or compareTo() method. Now the last one, most popular implementation of Map interface are HashMap, LinkedHashMap, Hashtable and TreeMap. First one is the non synchronized general purpose Map implementation while Hashtable is its synchronized counterpart, both doesn' provide any ordering guarantee which comes from LinkedHashMap. Just like TreeSet, TreeMap is also a sorted data structure and keeps keys in sorted order.

LinkedHashSet or ArrayList

I wish to
Avoid duplicated item being inserted.
When I iterate through the collection class, the returned item is same as insertion order.
May I know, what thing I should consider, to choose either ArrayList (explicitly perform contains check before insertion) or LinkedHashSet?
Thanks.
Definitely use LinkedHashSet. It is made for what you need. Searching entire ArrayList every time you need to insert something will be performance killer (O(n) every time))
Use LinkedHashSet if you don't want duplicate items inserted.
A LinkedHashSet seems to fit the bill perfectly.
When you build your own objects, and plan to use them in a Collection like LinkedHashSet here. Don't forget to override both equals and hashcode for the item you are going to store in it.
Please check this out:
http://wiki3.cosc.canterbury.ac.nz/images/e/e9/JavaCollections.png
LinkedHashSet is what you need, because it's an implementation of the Set interface. Set has one very cool habit: it doesn't allows duplicates by default. So, we are done with your 1.
What about 2?
We know, that we need one of the Set implementation, but which ?
HashMap - you are able to store K,V pairs, but there is no order.
TreeSet - this is the slowest solution, because it's using a compareTo method to keep every item sorted and ordered. This is why you can pass a comparator to it, when you are constructing a TreeSet.
LinkedHashSet - Gives back the elements in order of INSERTING them. It is the ordered version of a HashSet.
Please find a cool description here:
http://java67.blogspot.co.uk/2014/01/when-to-use-linkedhashset-vs-treeset-vs-hashset-java.html?_sm_au_=iVVMtMLHSDQ5P0P7

Categories