hashMap vs arrayList

Hi!

Could anyone please explain me, why the HashMap ist supposed to be better than the ArrayList?

The essay to be solved is as follows:

there will be a huge range, like a few hundret years. And there are Items, which belong to a day, week, year .... . One Item can belong to more than one point of time. At one point of time there can be more than one Item. The id of the items is til know the date.

The first try was putting it in an Arraylist. Each index is one point of time. I do not initialize all the Items of the Array, but only those, which items exist. I also could put more than one item to one index, managing this in an List.

The seccond thought or more or less "haveto" was: taking a hashMap.

But I really don't get why this should be better. Lets say the dates are my keys. If I want to put two Item, belonging to the same date into the map, then the first item I include, will be overwritten with the seccond one, won't it?

What about the memory?

Thanks for help

[1034 byte] By [Tille2a] at [2007-11-27 2:10:34]
# 1

They are totally different in usage, time per access, time per addition, memory usage, etc.

Obviously an arraylist is basically a list of things.

But, clearly you don't know what a HashMap is or how it's implemented.

Google for it. Come back if you have a specific question.

KathyMcDonnella at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 2
which is better, cheese or the eiffel tower?
gmac51a at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 3
Cheese.The Eiffel Tower won't fit in my toasted sandwich maker, and it has a metallic taste...
SeanJ@a at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 4
The Eiffel Tower.You can't charge tourists large sums of money to stand on top of cheese...
DrClapa at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 5
Take some cheese up with you and eat it as you enjoy the view!
DrLaszloJamfa at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 6

> Could anyone please explain me, why the HashMap ist

> supposed to be better than the ArrayList?

Well, it's not. In this case it's more a question of how fast you need to find an object from a "key".

If you just add them to an ArrayList you'll have to start from the beginning and search to the end to be sure to find all objects with the correct "key".

A HashMap finds a "key" in just one lookup. If a "key" has many objects you just store all of them in an ArrayList at the "key" position.

DrLaszloJamfa at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 7
Lists (as opposed to maps, sets, and bags) are also partially ordered which can be an important property.For more information, google for "computer science datastructures algorithms"
tjacobs01a at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 8

> Hi!

>

> Could anyone please explain me, why the HashMap ist

> supposed to be better than the ArrayList?

>

It's not they're both good for different things. At its most basic level (making a comparison to a table of information) a HashMap would be comparatively a row of data, and an ArrayList would be the table of rows. It's a loose comparison but illustrative.

> The essay to be solved is as follows:

>

> there will be a huge range, like a few hundret years.

> And there are Items, which belong to a day, week,

> year .... . One Item can belong to more than one

> point of time. At one point of time there can be more

> than one Item. The id of the items is til know the

> date.

>

> The first try was putting it in an Arraylist. Each

> index is one point of time. I do not initialize all

> the Items of the Array, but only those, which items

> exist. I also could put more than one item to one

> index, managing this in an List.

>

> The seccond thought or more or less "haveto" was:

> taking a hashMap.

> But I really don't get why this should be better.

> Lets say the dates are my keys. If I want to put two

> Item, belonging to the same date into the map, then

> the first item I include, will be overwritten with

> the seccond one, won't it?

>

> What about the memory?

>

>

> Thanks for help

From the sound of it what your assignment is looking for is neither a HashMap or an ArrayList but rather a multiply linked list. Which is an interesting connundrum in and of itself. At a glance to make what I think you're after would require.

1. a class which contained a list of (n) references to other objects like itself.

2. a class containing a list of instances of the above class.

3. this class would also have to provide methods for resolving references to other (not necessarily neighboring) instances

There are other things it would need as well. Not a trivial endeavor.

Now I may be totally off base, but that's the way I understand what you're after, if not then feel free to totally disregard this suggestion.

PS.

puckstopper31a at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 9

thanks for answering!!

Yes i kind of also think, I need something like lists in lists, at least I think, that just a hashmap won't work out for sure! Maybe I'd try a hashmap with arraylists for each key. But doing so I still would need a seccond key, won't I?

Well I think I'll come back here tomorrow, after trying some stuff.

Tille2a at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 10

> Maybe I'd try a hashmap with arraylists for each key.

Having an ArrayList for the value in a Map would be a perfectly reasonable thing to do, and it might well fit in with your problem description. But having an ArrayList for the key is very unlikely to be the correct idea. The key should always be an immutable object, or at least an object that you don't change once it becomes a key.

DrClapa at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 11
hm, so I have to have a key, which points to an ArrayList? Did I get this right?
Tille2a at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 12
> hm, so I have to have a key, which points to an> ArrayList? Did I get this right?Well, you said "One Item can belong to more than one point of time." So would you make the item the key and the list of points of time the value, or the other way around?
DrClapa at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 13
Sounds like you will need a map type data structure which will give you keys and values.key can be done automatically for you.sandyR
SandyReda at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 14

Hi,

as I understanded your problem and as other people have answered before, I believe you should think about another abstact data type to solve your essay.

You can explain your problem like a group of items wich have to access a group of dates each on, and a group of dates wich have to access a group of items each one too, so it seems (i think) a graph problem.

Let's think about a node (ItemNode) wich contains an Item and a list of Date references as childs. And now another node (DateNode) wich contains a Date and a list of Item references as childs too.

Then, for each ItemNode you have the Item and all its Dates so you can access for that ones all Items in it. There must be a simple cyclic tour control not to get allways the same Item and Date from the graph, but it can be what you're looking for.

It's another possible idea :)

alexpallarsa at 2007-7-12 2:02:47 > top of Java-index,Java Essentials,Java Programming...
# 15

> Hi,

> as I understanded your problem and as other people

> have answered before, I believe you should think

> about another abstact data type to solve your essay.

> You can explain your problem like a group of items

> wich have to access a group of dates each on, and a

> group of dates wich have to access a group of items

> each one too, so it seems (i think) a graph problem.

> Let's think about a node (ItemNode) wich contains an

> Item and a list of Date references as childs. And now

> another node (DateNode) wich contains a Date and a

> list of Item references as childs too.

> Then, for each ItemNode you have the Item and all its

> Dates so you can access for that ones all Items in

> it. There must be a simple cyclic tour control not to

> get allways the same Item and Date from the graph,

> but it can be what you're looking for.

> It's another possible idea :)

sorry, there's mistake at one thing...

the node must contain node references.

I said for example an ItemNode sould contain an Item and a list of Dates but it must contain an Item and a list of DateNodes. The same at the DateNode case, it must contain ItemNodes not Items...

alexpallarsa at 2007-7-21 20:20:33 > top of Java-index,Java Essentials,Java Programming...
# 16

thanks for your idea.

But I'm not really sure, if this got to be that complicated. It's all double-linked then, right? All Items know all their Dates they belong to, and all Dates know all their Items which they "have".

Til now, the date was supposed to be the key, this would be possible here, right? Would I have to put an id in the Items? ...

I keep on trying, thanks

Tille2a at 2007-7-21 20:20:34 > top of Java-index,Java Essentials,Java Programming...