Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18447

Re: Question about HashMap and Map.Entry ...

Newsgroups comp.lang.java.programmer
Date 2012-08-30 13:42 -0700
References <slrnk3v9ba.u9l.avl@gamma.logic.tuwien.ac.at>
Message-ID <5c044912-a7e7-493f-8dd9-0ae1e35c26f0@googlegroups.com> (permalink)
Subject Re: Question about HashMap and Map.Entry ...
From Lew <lewbloch@gmail.com>

Show all headers | View raw


Andreas Leitgeb wrote:
> Given a (Hash)Map<Long,Long> map and two Long values k and v,
> Some task is to see if k is already in the map, and only if so,
> then update the value in the map based on previous value and v.
> 
> Of course, that is trivial, and I've already implemented
> it with containsKey(), get() and put().
> It's just, that it seems to me that my piece of code
> could be a bit *clearer*, if I could obtain the Map.Entry
> for "k", and (if that isn't null) do getValue() and
> setValue(...) on the Entry.

I dispute that that would be clearer. You'd be using part 
of the inner and should-be-hidden machinery of the Map 
to express what 'containsKey()' already directly and quite 
clearly expresses.

Also, modifying an 'Entry' might be synonymous with, but 
is not the same as "put the key and value in the Map", 
which 'Map#put(K key, V value)" is.

> What I'm missing, however, is:
>   How would I get the Entry for a given key?
> (apart from scanning all through the entrySet())

A quick scan of the Javadocs for 'Map.Entry' says, sorry, no.

> PS: using 1.6, but also interested in future (i.e. 1.7 or 

Java 7 is not the future. It's the present. It's up to 7u7 already.
As of November 2012, a few short months away, Java 6 will be 
the past.

>    even newer) prospects, and also in (any) explicit reasons
>    for *not* having some map.getEntry(K k).

Because 'Map' already has 'get(K k)'.

>    Also, such an Entry should be tied to the Map just like
>    those in the entrySet().  NavigableMap has methods that
>    return Entries with "snapshot"-semantics, which is *not*
>    what I'm looking for.

A quick scan of the Javadocs for 'Map.Entry' says, sorry, no.
"The only way to obtain a reference to a map entry is from the iterator 
of this collection-view. These Map.Entry objects are valid only for the 
duration of the iteration; more formally, the behavior of a map entry 
is undefined if the backing map has been modified after the entry was 
returned by the iterator, except through the setValue operation on the 
map entry."

"V setValue(V value) ...  (optional operation)."

-- 
Lew

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question about HashMap and Map.Entry ... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-08-30 17:39 +0000
  Re: Question about HashMap and Map.Entry ... markspace <-@.> - 2012-08-30 10:46 -0700
    Re: Question about HashMap and Map.Entry ... Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-08-30 11:15 -0700
      Re: Question about HashMap and Map.Entry ... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-08-30 19:24 +0000
        Re: Question about HashMap and Map.Entry ... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-08-30 19:32 +0000
          Re: Question about HashMap and Map.Entry ... Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-08-30 13:08 -0700
            Re: Question about HashMap and Map.Entry ... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-08-30 21:55 +0000
              Re: Question about HashMap and Map.Entry ... Robert Klemme <shortcutter@googlemail.com> - 2012-08-31 07:27 +0200
                Re: Question about HashMap and Map.Entry ... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-08-31 10:26 +0000
                Re: Question about HashMap and Map.Entry ... Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-08-31 11:22 -0700
                Re: Question about HashMap and Map.Entry ... Robert Klemme <shortcutter@googlemail.com> - 2012-09-01 11:06 +0200
        Re: Question about HashMap and Map.Entry ... markspace <-@.> - 2012-08-30 14:30 -0700
          Re: Question about HashMap and Map.Entry ... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-08-30 22:42 +0000
        Re: Question about HashMap and Map.Entry ... markspace <-@.> - 2012-08-30 14:34 -0700
  Re: Question about HashMap and Map.Entry ... Lew <lewbloch@gmail.com> - 2012-08-30 13:42 -0700
    Re: Question about HashMap and Map.Entry ... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-08-30 22:34 +0000

csiph-web