Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Something Better than ArrayList

Reply
Thread Tools

Something Better than ArrayList

 
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-21-2011
Dear Java'ers:

I have been using ArrayList to serve as an expandable array. I
have not cared about the searching efficency since the arrays have
been small.

Now, I need symbol table processing. There will be a lot of
lookups. What class is like ArrayList in being an expandable array
but that has an order? I also want to have more than one data item
per entry. I am looking at
symbol name (which is what the array is to be sorted by)
symbol value
and possibly something else by the time I think on this further.
Symbol name and symbol value will be of type String though I would
like a general answer.

The name of the class is what I need. I assume I can find the
docs once I know what it is called.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
 
 
 
Donkey Hottie
Guest
Posts: n/a
 
      06-21-2011
21.6.2011 8:34, Gene Wirchenko kirjoitti:
> Dear Java'ers:
>
> I have been using ArrayList to serve as an expandable array. I
> have not cared about the searching efficency since the arrays have
> been small.
>
> Now, I need symbol table processing. There will be a lot of
> lookups. What class is like ArrayList in being an expandable array
> but that has an order? I also want to have more than one data item
> per entry. I am looking at
> symbol name (which is what the array is to be sorted by)
> symbol value
> and possibly something else by the time I think on this further.
> Symbol name and symbol value will be of type String though I would
> like a general answer.
>
> The name of the class is what I need. I assume I can find the
> docs once I know what it is called.
>
> Sincerely,
>
> Gene Wirchenko


I think you need a Map<String,String>

That is the interface, the implementation could be
HashMap<String,String> or TreeMap<String,String>

HashMap does not keep ordering, but TreeMap does. Both offer the lookup
with symbol name though, and that is what you need, I think.



--

An exotic journey in downtown Newark is in your future.
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      06-21-2011
Donkey Hottie <(E-Mail Removed)> wrote:
> 21.6.2011 8:34, Gene Wirchenko kirjoitti:
>> Now, I need symbol table processing. There will be a lot of
>> lookups. What class is like ArrayList in being an expandable array
>> but that has an order?


> I think you need a Map<String,String>


>> I also want to have more than one data item
>> per entry.


Map<String,List<DataItem>>

> That is the interface, the implementation could be
> HashMap<String,String> or TreeMap<String,String>
> HashMap does not keep ordering, but TreeMap does. Both offer the lookup
> with symbol name though, and that is what you need, I think.
>

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-21-2011
Andreas Leitgeb <(E-Mail Removed)> writes:
>>>I also want to have more than one data item
>>>per entry.

>Map<String,List<DataItem>>


Or, Map<String,Set<DataItem>>.

 
Reply With Quote
 
Paul Cager
Guest
Posts: n/a
 
      06-21-2011
On Jun 21, 6:34*am, Gene Wirchenko <(E-Mail Removed)> 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/tu...ons/index.html
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      06-21-2011
Stefan Ram <(E-Mail Removed)-berlin.de> wrote:
> Andreas Leitgeb <(E-Mail Removed)> writes:
>>>> I also want to have more than one data item
>>>> per entry.

>> Map<String,List<DataItem>>

> Or, Map<String,Set<DataItem>>.


Ok, you win
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-21-2011
On Mon, 20 Jun 2011 22:34:44 -0700, Gene Wirchenko <(E-Mail Removed)>
wrote:

>Dear Java'ers:


Terminology can be a bear. TLA poisoning and all that, but it is
even worse when one does not know the term.

Thank you for your replies. I will be hitting the docs and
tutorials a bit later today.

[snip]

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-21-2011
On Tue, 21 Jun 2011 02:22:46 -0700 (PDT), Paul Cager
<(E-Mail Removed)> wrote:

>On Jun 21, 6:34*am, Gene Wirchenko <(E-Mail Removed)> 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/tu...ons/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?

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      06-21-2011
On Tue, 21 Jun 2011, Gene Wirchenko wrote:

> On Tue, 21 Jun 2011 02:22:46 -0700 (PDT), Paul Cager
> <(E-Mail Removed)> wrote:
>
>> On Jun 21, 6:34*am, Gene Wirchenko <(E-Mail Removed)> 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/tu...ons/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/...urrentMap.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
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      06-21-2011
On Tue, 21 Jun 2011, Andreas Leitgeb wrote:

> Stefan Ram <(E-Mail Removed)-berlin.de> wrote:
>> Andreas Leitgeb <(E-Mail Removed)> writes:
>>>>> I also want to have more than one data item
>>>>> per entry.
>>> Map<String,List<DataItem>>

>> Or, Map<String,Set<DataItem>>.

>
> Ok, you win


SortedMap<String, Collection<DataItem>>!

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GL2 better than the XLs? Consumer grade HDs better than pro-sumer Mini DVs? dh@. DVD Video 1 08-28-2008 07:20 PM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments