LinkedHashMap next method ?

A

Albert

Hi, since LinkedHashMap keep the insertion order, why sun didn't add the
next method: K next(K key) ?

I need it because of speed of direct hash lookups (the hash part) and
order-based lookups (the next method). Is there an easy way to get that?

Thanks
 
R

Roedy Green

Hi, since LinkedHashMap keep the insertion order, why sun didn't add the
next method: K next(K key) ?

I need it because of speed of direct hash lookups (the hash part) and
order-based lookups (the next method). Is there an easy way to get that?

You can traverse the entire set in order with keySet

It is a slightly more strenuous requirement to be able to traverse,
starting at any point, but the way LinkedHashMap was implemented would
make it easy.

You could probably easily add the next method by extending the class
and using some protected fields and methods.

LinkedHash map links in insertion time order, right? which is not that
interesting. Normally you extract and sort if you want to traverse
the entire set.

see http://mindprod.com/jgloss/hashmap.html

I agree, the lack of the next method looks like an oversight. Perhaps
they fretted over how to de define it in a multithread environment.

--
Roedy Green Canadian Mind Products
http://mindprod.com

"It wasn’t the Exxon Valdez captain’s driving that caused the Alaskan oil spill. It was yours."
~ Greenpeace advertisement New York Times 1990-02-25
 
T

Tom Anderson

Hi, since LinkedHashMap keep the insertion order, why sun didn't add the next
method: K next(K key) ?

Probably because they (and by 'they' i mean Josh Bloch, who wrote it)
didn't think of it, or didn't think it was important enough to include.

There are lots of potentially useful methods that classes in the standard
library could have but don't; that;s inevitable, and not an entirely bad
thing, as the alternative is classes with million of methods.
I need it because of speed of direct hash lookups (the hash part) and
order-based lookups (the next method). Is there an easy way to get that?

Copy:

https://openjdk.dev.java.net/source.../classes/java/util/LinkedHashMap.java?rev=257

Paste. Edit:

package albert.util;
import java.util.*;

public K nextKey(K key) {
Entry<K, V> entry = (Entry<K, V>)getEntry(key);
if (entry == null) return null;
Entry<K, V> next = entry.after;
e.recordAccess(this);
if (next == null) return null;
return next.key;
}

Save. Profit!

Bear in mind that OpenJDK code is GPL'd, so if you want to redistribute
this class, you'l need to ship the source with it (and package it in a
separate package and JAR from the rest of your app to prevent GPL
infection).

tom
 
T

Tom Anderson

You can traverse the entire set in order with keySet

It is a slightly more strenuous requirement to be able to traverse,
starting at any point, but the way LinkedHashMap was implemented would
make it easy.

You could probably easily add the next method by extending the class
and using some protected fields and methods.

All the interesting bits are private, sadly.
I agree, the lack of the next method looks like an oversight. Perhaps
they fretted over how to de define it in a multithread environment.

I doubt it - the collections classes (the modern non-concurrent ones, that
is) pretty explicitly ignore threading issues.

tom
 
S

Seamus MacRae

Tom said:
All the interesting bits are private, sadly.

And to make matters worse:

362 private abstract class LinkedHashIterator<T> implements
Iterator<T> {

The least they could have done was made it possible to cast the
keySet().iterator() to ListIterator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top