Re: Collection



Garg wrote:
but as Matt mentioned that the order deppends on the hashcode and
order they are inserted(i [sic] think it only depends on the hashcode).

Sort of, and it can change over the life of the Map.

I tried to implement
List <Map.Entry<K,V>> entryList = new ArrayList <Map.Entry<K,V>>

You do not need to mix Map and List in this way.

You should not embed the implementing type ("List") in the variable name. A name that documents the /logical/ purpose is superior, and will not need change as much if you refactor. Consider a name like "entries" for this one.

(myHashMap.entrySet());

Why make a list when you can iterate right over the Set? It's a lot of extra copying and complexity for no gain.

K key = entryList.get(4).getKey();
V value = entryList.get(4).getValue();

But i [sic] am couldn't be able to compile this.

Would you be willing to show us an SSCCE
<http://www.physci.org/codes/sscce.html>
and copy-and-paste the error messages for us?

Comments based on the lack of information so far:

- You left out the generic types K and V in the declaration of your class that has the "entryList" member. Thus K and V were not recognized types in the compilation.

- You should declare your own wrapper type to hold the pair of values you want to store, unless you truly want them to have a key-value relationship. The purpose of a Map is to hold associations; using a Map as a List is overkill. Instead, use a List from the get-go, and skip using a Map altogether.

- If, OTOH, you do need the associative lookup and only occasionally need to loop through the entrySet, then just iterate over the entrySet. For most implementations of Map, the order is irrelevant and will change between calls.

- The use of a Map to hold an association between keys and values is at variance with the notion of ordering a list of value pairs by some intrinsic order. For the latter, use a sorted
List <T extends Comparable<? super T>>
or List<T> sorted using a
Comparator<? super T>
or a SortedSet<T>. For the former where sort order really does matter, use a SortedMap<T>. Matt Humphrey called your attention to the TreeMap implementation. You should be able to simply iterate over that.

<http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List)>

<http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)>

<http://java.sun.com/javase/6/docs/api/java/util/SortedSet.html>
<http://java.sun.com/javase/6/docs/api/java/util/SortedMap.html>

- It is unlikely that you actually need both Mapness and sortedness.

--
Lew
.



Relevant Pages

  • Riemann surface for iteration of complex map?
    ... I once had a thread here where I mentioned the idea of using a Riemann ... surface to "continuously iterate" a complex map: ... real and theta any real. ...
    (sci.math)
  • Re: use remove_if in map
    ... RemoveInvalid(std::setvalidset) ... valid map elements into another map (possibly via a vector, ... log m) where s is the size of the valid set, and m that of the map. ... Option 2 is to iterate over the map and check each element for validity. ...
    (microsoft.public.vc.stl)
  • Why is not "sub" necessary? What is the difference: block and expression?
    ... I'm trying to understand the map function in perl and have been studying ... When David calls his iterate function, why does he not need to use the ... keyword "sub"? ...
    (perl.beginners)
  • Re: do u know ramanujan numbers algorithm
    ... You could also iterate only over two variables and save the values ... Before actually adding it to the map, ... keys in the map that are small enough that re-encountering them ... Any one else interested pay me $200 for the solution. ...
    (comp.lang.java.programmer)
  • Another kind of ConcurrentModificationException, how can I solve it?
    ... I will iterate a map and send out a message to ... pointing. ... completely handled the notification. ...
    (comp.lang.java.programmer)