Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #5493
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!.POSTED!ipv6.urchin.earth.li!twic |
|---|---|
| From | Tom Anderson <twic@urchin.earth.li> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Something Better than ArrayList |
| Date | Tue, 21 Jun 2011 22:56:51 +0100 |
| Organization | Stack Usenet News Service |
| Lines | 76 |
| Message-ID | <alpine.DEB.2.00.1106212253080.6476@urchin.earth.li> (permalink) |
| References | <hoa007h62o1tas6h5p6qlls6i8i2nf9q84@4ax.com> <1337fe1a-2385-47c1-b966-9925f79d0ba3@j28g2000vbp.googlegroups.com> <jd2207949b23u2c20ea80cdqmer8i1515q@4ax.com> |
| NNTP-Posting-Host | ipv6.urchin.earth.li |
| Mime-Version | 1.0 |
| Content-Type | MULTIPART/MIXED; BOUNDARY="232016332-1442916366-1308693411=:6476" |
| X-Trace | mud.stack.nl 1308693411 53338 2001:ba8:0:1b4::6 (21 Jun 2011 21:56:51 GMT) |
| X-Complaints-To | abuse@stack.nl |
| NNTP-Posting-Date | Tue, 21 Jun 2011 21:56:51 +0000 (UTC) |
| User-Agent | Alpine 2.00 (DEB 1167 2008-08-23) |
| In-Reply-To | <jd2207949b23u2c20ea80cdqmer8i1515q@4ax.com> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5493 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Tue, 21 Jun 2011, Gene Wirchenko wrote:
> On Tue, 21 Jun 2011 02:22:46 -0700 (PDT), Paul Cager
> <paul.cager@googlemail.com> wrote:
>
>> On Jun 21, 6:34 am, Gene Wirchenko <ge...@ocis.net> wrote:
>>> Dear Java'ers:
>>> The name of the class is what I need. I assume I can find the
>>> docs once I know what it is called.
>>
>> Others have already pointed you to java.util.Map, which answers your
>> immediate question. You might also want to have a look at the Java
>> "Collections" tutorial for more general information:
>> http://download.oracle.com/javase/tutorial/collections/index.html
>
> I found it rather dry, but did manage to write a proof-of-concept
> program for a symbol table. However, I have to check for duplication
> before put()ing. Is there a way to combine a Map and a Set to avoid
> this?
>
> For the Map
>
> static Map<String,String> SymbolTable=new HashMap<String,String>();
>
> I would like to write something like
>
> static boolean TryToAdd
> (
> String theKey,
> String theData
> )
> {
> return SymbolTable.put(theKey,theData);
> }
>
> instead of
>
> static boolean TryToAdd
> (
> String theKey,
> String theData
> )
> {
> if (SymbolTable.containsKey(theKey))
> return false;
> else
> {
> SymbolTable.put(theKey,theData);
> return true;
> }
> }
>
> Am I missing something or is this not supported?
You can't do it with a normal Map. You can do it with a ConcurrentMap:
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ConcurrentMap.html
ConcurrentMap has it because you can't easily build an efficient
threadsafe implementation of putIfAbsent on top of the normal Map
interface. It's a bit of a shame Map doesn't have it, because it's useful
even if you're not dealing with multiple threads!
tom
--
No man ever steps in the same river twice, for it's not the same river
and he's not the same man. -- Heraclitus
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Something Better than ArrayList Gene Wirchenko <genew@ocis.net> - 2011-06-20 22:34 -0700
Re: Something Better than ArrayList Donkey Hottie <donkey@fredriksson.dy.fi> - 2011-06-21 09:55 +0300
Re: Something Better than ArrayList Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-21 09:00 +0000
Re: Something Better than ArrayList Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-06-21 10:48 +0000
Re: Something Better than ArrayList Tom Anderson <twic@urchin.earth.li> - 2011-06-21 22:57 +0100
Re: Something Better than ArrayList Paul Cager <paul.cager@googlemail.com> - 2011-06-21 02:22 -0700
Re: Something Better than ArrayList Gene Wirchenko <genew@ocis.net> - 2011-06-21 14:19 -0700
Re: Something Better than ArrayList Tom Anderson <twic@urchin.earth.li> - 2011-06-21 22:56 +0100
Re: Something Better than ArrayList Gene Wirchenko <genew@ocis.net> - 2011-06-21 16:39 -0700
Re: Something Better than ArrayList Gene Wirchenko <genew@ocis.net> - 2011-06-21 07:51 -0700
csiph-web