Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #28271 > unrolled thread
| Started by | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| First post | 2014-04-27 21:58 +0200 |
| Last post | 2014-04-27 16:49 -0700 |
| Articles | 18 — 9 participants |
Back to article view | Back to comp.lang.java.programmer
hash map / collection choice Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-04-27 21:58 +0200
Re: hash map / collection choice leif.roar@invalid.com (Leif Roar Moldskred) - 2014-04-27 15:51 -0500
Re: hash map / collection choice Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-04-27 17:33 -0400
Re: hash map / collection choice leif.roar@invalid.com (Leif Roar Moldskred) - 2014-04-27 20:58 -0500
Re: hash map / collection choice Jeff Higgins <jeff@invalid.invalid> - 2014-04-27 22:04 -0400
Re: hash map / collection choice Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-04-27 17:05 -0400
Re: hash map / collection choice Jeff Higgins <jeff@invalid.invalid> - 2014-04-27 17:52 -0400
Re: hash map / collection choice Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-04-28 07:38 +0200
Re: hash map / collection choice leif.roar@invalid.com (Leif Roar Moldskred) - 2014-04-28 01:13 -0500
Re: hash map / collection choice Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-04-28 22:20 +0200
Re: hash map / collection choice Joerg Meier <joergmmeier@arcor.de> - 2014-04-28 23:26 +0200
Re: hash map / collection choice Arne Vajhøj <arne@vajhoej.dk> - 2014-04-28 18:41 -0400
Re: hash map / collection choice Joerg Meier <joergmmeier@arcor.de> - 2014-04-29 10:48 +0200
Re: hash map / collection choice Arne Vajhøj <arne@vajhoej.dk> - 2014-04-29 17:40 -0400
Re: hash map / collection choice Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-04-28 08:03 -0400
Re: hash map / collection choice Marcel Müller <news.5.maazl@spamgourmet.org> - 2014-04-28 00:30 +0200
Re: hash map / collection choice markspace <markspace@nospam.nospam> - 2014-04-27 16:37 -0700
Re: hash map / collection choice Roedy Green <see_website@mindprod.com.invalid> - 2014-04-27 16:49 -0700
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2014-04-27 21:58 +0200 |
| Subject | hash map / collection choice |
| Message-ID | <ljjnh1$852$1@online.de> |
Hello,
I need help to create a data structure. I have got a class
class myEdge {
private int m_edgeid = 0;
….
@Override
public int compareTo(myEdgep_edgelink) {
if (m_edgeid > p_edgelink.m_edgeid)
return 1;
if (m_edgeid < p_edgelink.m_edgeid)
return -1;
return 0;
}
@Override
public boolean equals(Object p_object) {
if ( (p_object == null) || (!(p_object instanceof myEdge)) )
return false;
return this.m_edgeid == ((myEdge)p_object).m_edgeid;
}
@Override
public int hashCode() {
return m_edgeid;
}
}
The edge ID is always unique. So I would like to create a collection in
which I can do something like
myEdge x = edgecollection.get( searching edge id )
So the collection stores the edge objects and I can get an object with
the internal id. The collection
should use the the edgeid value to identify the object.
A set should be the correct structure, but I can not get the object
itself of the map without iteration over all
items. A HashMap needs a key, value pair, so I need also a key class
which stores the edge id (redundant data).
I'm a little bit uncertain, which collection is the correct choice.
Thanks a lot
Phil
[toc] | [next] | [standalone]
| From | leif.roar@invalid.com (Leif Roar Moldskred) |
|---|---|
| Date | 2014-04-27 15:51 -0500 |
| Message-ID | <gdCdnZ-4k5Yj8MDOnZ2dnUVZ7tudnZ2d@giganews.com> |
| In reply to | #28271 |
Philipp Kraus <philipp.kraus@flashpixx.de> wrote:
>
> A set should be the correct structure, but I can not get the object
> itself of the map without iteration over all
> items. A HashMap needs a key, value pair, so I need also a key class
> which stores the edge id (redundant data).
No, you can just use /Integer/ for the key:
private static Map<Integer, MyEdge> toIdentityMap( MyEdge ... edges ) {
Map<Integer, MyEdge> identityMap = new HashMap<>( edges.length );
for( MyEdge item : edges ) {
identityMap.put( item.getEdgeID(), item );
}
return identityMap;
}
On a second, unrelated note, the style of your code is pretty far from
the Java style conventions, and if it's not throwaway code you're
writing, you should probably read through the document "Code
Conventions for the Java Programming Language" [1] and try to adhere
to it as much as practical.
[1] You can find it at
http://www.oracle.com/technetwork/java/index-135089.html
--
Leif Roar Moldskred
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2014-04-27 17:33 -0400 |
| Message-ID | <ljjt3m$nh5$1@dont-email.me> |
| In reply to | #28272 |
On 4/27/2014 4:51 PM, Leif Roar Moldskred wrote:
> [...]
> On a second, unrelated note, the style of your code is pretty far from
> the Java style conventions, and if it's not throwaway code you're
> writing, you should probably read through the document "Code
> Conventions for the Java Programming Language" [1] and try to adhere
> to it as much as practical.
>
> [1] You can find it at
> http://www.oracle.com/technetwork/java/index-135089.html
Unfortunately, the links on that promisingly-named page
are all 404's or inoperative. Bug 8039151 was filed a little
over three weeks ago but hasn't yet been resolved; I guess an
Oracle committee is preparing to start discussions on a suitable
framework and process for repairing links. If so, the links may
get repaired in time for Java 9 or 10. ;-)
--
Eric Sosman
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | leif.roar@invalid.com (Leif Roar Moldskred) |
|---|---|
| Date | 2014-04-27 20:58 -0500 |
| Message-ID | <ueidnackTc0mKMDOnZ2dnUVZ8n6dnZ2d@giganews.com> |
| In reply to | #28276 |
Eric Sosman <esosman@comcast-dot-net.invalid> wrote: > > Unfortunately, the links on that promisingly-named page > are all 404's or inoperative. Ack. My bad, apologies. I should have checked that the link actually worked. > Bug 8039151 was filed a little > over three weeks ago but hasn't yet been resolved; I guess an > Oracle committee is preparing to start discussions on a suitable > framework and process for repairing links. If so, the links may > get repaired in time for Java 9 or 10. ;-) > *wince* It's pretty bad that a document this important has gone awol. There seems to be an old copy of it here: http://yohanan.org/steve/projects/java-code-conventions/ The guide _is_ a little out-of-date by now, mainly in that it doesn't cover the later language features such as generics or varargs, but it's still worth a read through. -- Leif Roar Moldskred
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2014-04-27 22:04 -0400 |
| Message-ID | <ljkd14$ij6$1@dont-email.me> |
| In reply to | #28290 |
On 04/27/2014 09:58 PM, Leif Roar Moldskred wrote: > Eric Sosman <esosman@comcast-dot-net.invalid> wrote: >> >> Unfortunately, the links on that promisingly-named page >> are all 404's or inoperative. > > Ack. My bad, apologies. I should have checked that the link > actually worked. > >> Bug 8039151 was filed a little >> over three weeks ago but hasn't yet been resolved; I guess an >> Oracle committee is preparing to start discussions on a suitable >> framework and process for repairing links. If so, the links may >> get repaired in time for Java 9 or 10. ;-) >> > > *wince* It's pretty bad that a document this important has gone awol. > > There seems to be an old copy of it here: > http://yohanan.org/steve/projects/java-code-conventions/ > > The guide _is_ a little out-of-date by now, mainly in that it doesn't > cover the later language features such as generics or varargs, but > it's still worth a read through. > Not much better here. :-o <http://openjdk.java.net/guide/codeConventions.html> Committee having cold beer.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2014-04-27 17:05 -0400 |
| Message-ID | <ljjrf4$clb$1@dont-email.me> |
| In reply to | #28271 |
On 4/27/2014 3:58 PM, Philipp Kraus wrote:
> Hello,
>
> I need help to create a data structure. I have got a class
>
> class myEdge {
Aside: It is customary to start class and interface names with
a capital letter.
> private int m_edgeid = 0;
> ….
>
> @Override
Aside: I think an `implements Comparable<myEdge>' is missing
from the class declaration -- otherwise, what are you overriding?
> public int compareTo(myEdgep_edgelink) {
I think there's a space character missing here ...
> if (m_edgeid > p_edgelink.m_edgeid)
> return 1;
> if (m_edgeid < p_edgelink.m_edgeid)
> return -1;
> return 0;
Aside: `return Integer.compare(m_edgeid, p_edgelink.m_edgeid);'
does the whole job in one line of code.
> }
>
>
> @Override
> public boolean equals(Object p_object) {
> if ( (p_object == null) || (!(p_object instanceof myEdge)) )
> return false;
Aside: Since the class isn't final it might someday be extended,
and then equals() could mistake a MyDecoratedEdge instance for a
plain myEdge. Try something like
if (p_object == null || p_object.getClass() != getClass())
return false;
> return this.m_edgeid == ((myEdge)p_object).m_edgeid;
> }
>
>
> @Override
> public int hashCode() {
> return m_edgeid;
> }
> }
>
> The edge ID is always unique. So I would like to create a collection in
> which I can do something like
>
> myEdge x = edgecollection.get( searching edge id )
>
> So the collection stores the edge objects and I can get an object with
> the internal id. The collection
> should use the the edgeid value to identify the object.
>
> A set should be the correct structure, but I can not get the object
> itself of the map without iteration over all
> items. A HashMap needs a key, value pair, so I need also a key class
> which stores the edge id (redundant data).
>
> I'm a little bit uncertain, which collection is the correct choice.
You want some kind of Map<Integer, myEdge>, most probably a
HashMap if iteration order is unimportant, or a TreeMap if you'd
like to traverse by edge ID. (There are other Map candidates, too,
but these are "the usual suspects.") Whatever kind of Map you
use, you'd insert (or update) with
myEdge edge = ...;
theMap.put(edge.getID(), edge);
... and retrieve with
myEdge edge = theMap.get(someRandomIdValue);
if (edge == null) {
// no such thing in the Map
}
For traversals, you can iterate over the Map's values:
for (myEdge edge : theMap.values()) {
...
}
... or you can use those newfangled Java 8 stream thingummies.
--
Eric Sosman
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2014-04-27 17:52 -0400 |
| Message-ID | <ljju92$und$1@dont-email.me> |
| In reply to | #28275 |
On 04/27/2014 05:05 PM, Eric Sosman wrote: > ... or you can use those newfangled Java 8 stream thingummies. Durn. Streaming thingummies on my new lambda colored glasses.
[toc] | [prev] | [next] | [standalone]
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2014-04-28 07:38 +0200 |
| Message-ID | <ljkphe$1o6$1@ariadne.rz.tu-clausthal.de> |
| In reply to | #28275 |
Hello, thanks for the helpful code lines, but On 2014-04-27 21:05:39 +0000, Eric Sosman said: > return Integer.compare(m_edgeid, p_edgelink.m_edgeid); my Integer does not have a method "compare" Phil
[toc] | [prev] | [next] | [standalone]
| From | leif.roar@invalid.com (Leif Roar Moldskred) |
|---|---|
| Date | 2014-04-28 01:13 -0500 |
| Message-ID | <WrCdndBvltgTbMDOnZ2dnUVZ8iGdnZ2d@giganews.com> |
| In reply to | #28292 |
Philipp Kraus <philipp.kraus@flashpixx.de> wrote: > > my Integer does not have a method "compare" > Which version of Java are you using? The Integer.compare() method was introduced in 1.7 -- Leif Roar Moldskred
[toc] | [prev] | [next] | [standalone]
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2014-04-28 22:20 +0200 |
| Message-ID | <ljmd6q$l45$1@online.de> |
| In reply to | #28293 |
On 2014-04-28 06:13:34 +0000, Leif Roar Moldskred said: > Philipp Kraus <philipp.kraus@flashpixx.de> wrote: >> >> my Integer does not have a method "compare" >> > > Which version of Java are you using? The Integer.compare() > method was introduced in 1.7 I'm using 1.6 (OSX)
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2014-04-28 23:26 +0200 |
| Message-ID | <5hucb1c9ddum.1830b0rrlv3n0$.dlg@40tude.net> |
| In reply to | #28302 |
On Mon, 28 Apr 2014 22:20:42 +0200, Philipp Kraus wrote: > On 2014-04-28 06:13:34 +0000, Leif Roar Moldskred said: >> Philipp Kraus <philipp.kraus@flashpixx.de> wrote: >>> my Integer does not have a method "compare" >> Which version of Java are you using? The Integer.compare() >> method was introduced in 1.7 > I'm using 1.6 (OSX) Then you should upgrade as soon as possible. As I understand, that should be trivial for a developer even on a Mac. Liebe Gruesse, Joerg -- Ich lese meine Emails nicht, replies to Email bleiben also leider ungelesen.
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2014-04-28 18:41 -0400 |
| Message-ID | <535ed911$0$299$14726298@news.sunsite.dk> |
| In reply to | #28304 |
On 4/28/2014 5:26 PM, Joerg Meier wrote: > On Mon, 28 Apr 2014 22:20:42 +0200, Philipp Kraus wrote: >> On 2014-04-28 06:13:34 +0000, Leif Roar Moldskred said: >>> Philipp Kraus <philipp.kraus@flashpixx.de> wrote: >>>> my Integer does not have a method "compare" >>> Which version of Java are you using? The Integer.compare() >>> method was introduced in 1.7 >> I'm using 1.6 (OSX) > > Then you should upgrade as soon as possible. As I understand, that should > be trivial for a developer even on a Mac. Unless he has a Java version vs MacOS X version issue. Arne
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2014-04-29 10:48 +0200 |
| Message-ID | <1pa7lbizx2gi9$.149elgsa03e4t$.dlg@40tude.net> |
| In reply to | #28305 |
On Mon, 28 Apr 2014 18:41:20 -0400, Arne Vajhøj wrote: > On 4/28/2014 5:26 PM, Joerg Meier wrote: >> On Mon, 28 Apr 2014 22:20:42 +0200, Philipp Kraus wrote: >>> On 2014-04-28 06:13:34 +0000, Leif Roar Moldskred said: >>>> Philipp Kraus <philipp.kraus@flashpixx.de> wrote: >>>>> my Integer does not have a method "compare" >>>> Which version of Java are you using? The Integer.compare() >>>> method was introduced in 1.7 >>> I'm using 1.6 (OSX) >> Then you should upgrade as soon as possible. As I understand, that should >> be trivial for a developer even on a Mac. > Unless he has a Java version vs MacOS X version issue. As far as I know, Macs can now simply download current Java versions from Oracle and need no longer wait for MacOS to provide a newer version. If I am mistaken I apologize. Liebe Gruesse, Joerg -- Ich lese meine Emails nicht, replies to Email bleiben also leider ungelesen.
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2014-04-29 17:40 -0400 |
| Message-ID | <53601c42$0$298$14726298@news.sunsite.dk> |
| In reply to | #28308 |
On 4/29/2014 4:48 AM, Joerg Meier wrote: > On Mon, 28 Apr 2014 18:41:20 -0400, Arne Vajhøj wrote: > >> On 4/28/2014 5:26 PM, Joerg Meier wrote: >>> On Mon, 28 Apr 2014 22:20:42 +0200, Philipp Kraus wrote: >>>> On 2014-04-28 06:13:34 +0000, Leif Roar Moldskred said: >>>>> Philipp Kraus <philipp.kraus@flashpixx.de> wrote: >>>>>> my Integer does not have a method "compare" >>>>> Which version of Java are you using? The Integer.compare() >>>>> method was introduced in 1.7 >>>> I'm using 1.6 (OSX) >>> Then you should upgrade as soon as possible. As I understand, that should >>> be trivial for a developer even on a Mac. >> Unless he has a Java version vs MacOS X version issue. > > As far as I know, Macs can now simply download current Java versions from > Oracle and need no longer wait for MacOS to provide a newer version. They can. If the MacOS X version they are running is supported by the Java version they want. According to Oracle "certified system configurations" then: 1.8 requires 10.8.3+ or 10.9+ 1.7 requires 10.7.3 or 10.8.3+ or 10.9+ Arne
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2014-04-28 08:03 -0400 |
| Message-ID | <ljlg3f$mg5$1@dont-email.me> |
| In reply to | #28292 |
On 4/28/2014 1:38 AM, Philipp Kraus wrote:
> Hello,
>
> thanks for the helpful code lines, but
>
> On 2014-04-27 21:05:39 +0000, Eric Sosman said:
>
>> return Integer.compare(m_edgeid, p_edgelink.m_edgeid);
>
> my Integer does not have a method "compare"
Integer.compare() was introduced in Java 1.7, just under three
years ago (the current Java version is 1.8). Is there some special
reason you need to stick with 1.6-or-even-older?
--
Eric Sosman
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | Marcel Müller <news.5.maazl@spamgourmet.org> |
|---|---|
| Date | 2014-04-28 00:30 +0200 |
| Message-ID | <535d8506$0$6707$9b4e6d93@newsspool2.arcor-online.net> |
| In reply to | #28271 |
On 27.04.14 21.58, Philipp Kraus wrote: > The edge ID is always unique. So I would like to create a collection in > which I can do something like > > myEdge x = edgecollection.get( searching edge id ) > > So the collection stores the edge objects and I can get an object with > the internal id. The collection > should use the the edgeid value to identify the object. > > A set should be the correct structure, but I can not get the object > itself of the map without iteration over all > items. A HashMap needs a key, value pair, so I need also a key class > which stores the edge id (redundant data). This is a well known limitation of the Java collections. They cannot deal with objects that have an embedded key. There are some good reasons for this restriction. I.e. if you modify the embedded key of an object in a container the entire container may get inconsistent. > I'm a little bit uncertain, which collection is the correct choice. You need a key value collection rather than a set. You can't come around the redundant keys because of the above restriction. Marcel
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2014-04-27 16:37 -0700 |
| Message-ID | <ljk4cm$6cp$1@dont-email.me> |
| In reply to | #28271 |
On 4/27/2014 12:58 PM, Philipp Kraus wrote:
> A set should be the correct structure, but I can not get the object
> itself of the map without iteration over all
> items. A HashMap needs a key, value pair, so I need also a key class
If you'd prefer a set, I'd just make one. It isn't that much trouble to
do so. The implementation might be a little slow, but if it gives you
trouble then you just have to optimize it more. Most time code like
this won't be the bottle-neck.
Not tested. I've renamed your get method to "findById", and you'll need
a proper hash/equals method, one that compares all fields in MyEdge, for
this to work 100%.
package quicktest;
import java.util.AbstractSet;
import java.util.HashMap;
import java.util.Iterator;
public class MySet extends AbstractSet<MyEdge>
{
private final HashMap<Integer,MyEdge> map = new HashMap<>();
@Override
public Iterator iterator() {
return map.values().iterator();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean add( MyEdge edge ) { // edge @NotNull
MyEdge temp = map.get( edge.getID() );
map.put( edge.getID(), edge);
return !edge.equals( temp );
}
public MyEdge findById( int id ) {
return map.get(id);
}
}
// Dummy for compilation
class MyEdge{
public int getID() {return 0;}
}
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-04-27 16:49 -0700 |
| Message-ID | <i35rl9d44ls05vgeog2s2rmhcl7n9ci019@4ax.com> |
| In reply to | #28271 |
On Sun, 27 Apr 2014 21:58:25 +0200, Philipp Kraus <philipp.kraus@flashpixx.de> wrote, quoted or indirectly quoted someone who said : >I'm a little bit uncertain, which collection is the correct choice. Generic advice. see http://mindprod.com/jgloss/hashmap.html http://mindprod.com/jgloss/treemap.html http://mindprod.com/jgloss/hashset.html http://mindprod.com/jgloss/arraylist.html There are comments on why you might use each one. Avoid Hashtable,Vector. I also talk about digested HashMaps. -- Roedy Green Canadian Mind Products http://mindprod.com Computers are like Old Testament gods; lots of rules and no mercy. ~ Joseph Campbell (born: 1904-03-26 died: 1987-10-31 at age: 83)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web