Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: markspace Newsgroups: comp.lang.java.programmer Subject: Re: hash map / collection choice Date: Sun, 27 Apr 2014 16:37:56 -0700 Organization: A noiseless patient Spider Lines: 53 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 27 Apr 2014 23:37:58 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="9358cda03e1e0dfd3c4b757daa608707"; logging-data="6553"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/l92BBqcO2Tym8jOphHdBKKUYdISnFOxo=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 In-Reply-To: Cancel-Lock: sha1:8sU8309A9da/dcP58a3bL+YDibM= Xref: csiph.com comp.lang.java.programmer:28284 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 { private final HashMap 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;} }