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


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

Re: hash map / collection choice

From markspace <markspace@nospam.nospam>
Newsgroups comp.lang.java.programmer
Subject Re: hash map / collection choice
Date 2014-04-27 16:37 -0700
Organization A noiseless patient Spider
Message-ID <ljk4cm$6cp$1@dont-email.me> (permalink)
References <ljjnh1$852$1@online.de>

Show all headers | View raw


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;}
}

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


Thread

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

csiph-web