This question already has answers here:
Java Class that implements Map and keeps insertion order?
(8 answers)
Closed 7 years ago.
I'm using a HashMap. When I iterate over the map, the data is returned in (often the same) random order. But the data was inserted in a specific order, and I need to preserve the insertion order. How can I do this?
LinkedHashMap is precisely what you're looking for.
It is exactly like HashMap, except that when you iterate over it, it presents the items in the insertion order.
HashMap is unordered per the second line of the documentation:
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
Perhaps you can do as aix suggests and use a LinkedHashMap, or another ordered collection. Take a look on javapractices.com's guide on Choosing the right Collection.
Related
This question already has answers here:
Java Class that implements Map and keeps insertion order?
(8 answers)
Closed 7 years ago.
I'm using a HashMap. When I iterate over the map, the data is returned in (often the same) random order. But the data was inserted in a specific order, and I need to preserve the insertion order. How can I do this?
LinkedHashMap is precisely what you're looking for.
It is exactly like HashMap, except that when you iterate over it, it presents the items in the insertion order.
HashMap is unordered per the second line of the documentation:
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
Perhaps you can do as aix suggests and use a LinkedHashMap, or another ordered collection. Take a look on javapractices.com's guide on Choosing the right Collection.
This question already has answers here:
Iteration order of HashSet
(9 answers)
Closed 5 years ago.
I was wondering, both HashMap and HashSet do not return values in order?
Please someone clarify.
I am confused and why do you need these two?
In a word - yes. Neither HashMap or HashSet give any guarantee on the order of iteration.
The HashMap API does not define the order of iteration.
However, if you look at the implementation of HashMap, you can deduce that there is a complex transient relationship between the iteration order, the keys' hash values, the order in which the keys were inserted and the size of the hashtable. This relationship gets scrambled if the hashtable resizes itself.
Please refer:
Is the order of values retrieved from a HashMap the insertion order
If you want the values in the order you insert you have to use LinkedHasmap other wise you can use TreeMap where sort by the key. hash does not give any order because it uses the hascode to order values it may vary depend on the object.
A HashMap stores key value pairs. A HashSet is an unordered collection of objects in which each there can be no repeats. Neither are necessarily iterated in order insertion or otherwise. There are ordered implementations but you would not use the standard HashMap or HashSet classes.
Talked about further here
Linked Hash Map does maintain insertion order
HashMap is a implementation of Map interface. Map is a data structure to say that A corresponds to B.
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(new Integer(1), "Test");
In the case above we know that every time that we look for 1, the correspondent is "Test".
Sets are something completely different. Sets ensures that no duplicate object will exists in your collection. HashSet is an implementation of Set interface.
To insert and retrieve something in order you could use LinkedHashMap, LinkedHashSet, ArrayList (implementation of List interface) and others
Wrapping it up:
Map - correspond objects
Set - Ensure unique objects in a collection
This question already has answers here:
Java Class that implements Map and keeps insertion order?
(8 answers)
Closed 6 years ago.
This is the code:
for(String key : mymap.stringPropertyNames()) {
//mycode
}
This works correctly but I noticed I get the values I need in random order, is there a way to loop through the map using a particular order?
EDIT: Mymap is a properties object.
This is because you are using a Map without sorting like HashMap
[...] This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
Instead of this, you can use some concrete implementation like:
TreeMap:
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.
LinkedHashMap if you need no duplicates...
Hash table and linked list implementation of the Map interface, with predictable iteration order.
If you want predictable iteration order (insertion order) then use a LinkedHashMap
If you want the elements to be sorted you need to use a TreeMap , in this case the Keys need to implement Comparable interface
Either change the Map implementation into one of the ones that support ordering or sort the keys before iterating over them. I am a bit confused though, normally one gets the keys of a map by the keySet method. I am not familiar with stringPropertyNames but if it is a Map you should be able to do something like (untested code):
List keys = new ArrayList(mymap.keySet())
Collections.sort(keys)
for ( String key : keys ) {
[...]
}
This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 7 years ago.
I have to sort a large list (more than 10,000 elements). On adding an element I have to insert it on the right place. I saw that an ArrayList will shift all the element that are after the insertion point.
How do all the different implementation of the List interface behave in such a case? And what are the pros and the cons when choosing one implementation over the other?
The two main implementations of List are ArrayList and LinkedList. There are others but they are generally used in specialis situations.
ArrayList can be accessed very quickly by index because it is backed by an array - you just need array[i] - but modifying the list requires moving much of the underlying array around so that is not efficient.
You can add/remove items with LinkedList very efficiently but finding the nth entry is slow because it has to start at the head and walk the list counting nodes until it gets to the required location.
Probably duplicate, but i will give you a hint.
To sort your data you can use:
Collections.sort(List list);
method, it will convert List to Array anyway, so you don't have to care about too much about type of List implementation. All it needs is interface Comparable implemented in your objects.
Can your data contain duplicates?
If NO, you can use a TreeSet<?>
If YES, you can use a TreeMap<?, Integer> where the Integer is the count per item
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Rule of thumb for choosing an implementation of a Java Collection?
My situation is this:
I have a collection of objects that I need to hold and every now and then iterate through
The size of the collection is dynamic
The iteration should access each element
The collection does not need to be sorted
Creating or updating the collection has no time constraints but I'd like to iterate through the collection as fast as possible.
What would be the best Collection to use (or would you perhaps suggest using an array?)
You can use a List collection probaly ArrayList.
It depends on the parameters like whether it is an ordered collection, whether you want to maintain the insertion order, whether you want to maintain uniqueness etc
List vs Set
Set: Unique, unordered collection
List: ordered collection, allows duplicate elements
ArrayList vs LinkedList
In general : If you don't have particular constraints, an ArrayList is you best bet. Unless you have extremely tight performance control, don't go for a blank array, you have too much chance for errors (and a big chance of not outperforming the ArrayList implementation).
In your case the fast iteration requirements means ArrayList is a good choice.