• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

java : set arraylist, maps, sets, hashmaps

hello experts
I am new to Java
I am really too confused about all types that allow to store objects and data like
Arrays, sets, Map, hashmap, ArrayList, lists, tree etc...

Can someone tell me what are the most usefull (the 3-4 I will use in my projects) and in what case should I use this one rather then anorther one ?
2 Solutions
Java Collections (I strongly recommend reading http://java.sun.com/docs/books/tutorial/collections/index.html) contains several types. The main ones I work with are:

List interface (most common implementation is ArrayList) - a simple list of items with an order. You can insert an item anywhere in the list, take an item from anywhere in the list, etc.
An example for using a List is keeping all the lines from a file you read in a List<String>, each line at its appropriate index.

Set interface (msot common implementation is HashSet) - a simple Set of items. The main differences from List is the fact that a set doesn't have  inherent indexes for its items, is HashSet there is no guaranteed order for going over items, and can only hold one copy of items that are logically equal.
An example for using a Set is building a dictionary of all the words from a text in Set<String>. Note that even if a word appears more than once in the text, the Set<String> will only hold one copy of that string.
Some more implementations for set:
LinkedHashSet - in which the order of items you get from the set is identical to the order you insert them into it
SortedSet (interface) - A set where the items are kept sorted. If you're using classes which do not have a comparator to compare them, you'll have to create one.

Map interface (e.g., HashMap) - A map containing keys and values. Each key can only appear once.
Keeping all the words from a text with the number of times they appear in the text using a Map<String, Integer> where the key is the word and the value is the number of appearances.
Keeping all the people in company according to their employee ID, to allow fast access to an employee using his/her id: Map<String, Empoyee> where the key is the ID string.

Please note that when using Set/Map you may need to think about implementing hashCode() and equals() to the classes you use.
There are of course other collections (Queue, HashTable, Vector, Stack) but these are the most common ones.

One more thing - if you use collections, take a look at java.util.Collections class, it has some very useful static methods.
>Can someone tell me what are the most usefull (the 3-4 I will use in my projects)

If there was an answer to that question, there would not have been all of the rest, don't you think?

ozlevanon already gave you a great answer. If you are interested, here are also some code examples that come handy sometimes:
and the proverbial http://www.exampledepot.com/egs/java.util/pkg.html of course.

And http://www.coderfriendly.com/2009/05/12/java-collections-cheatsheet/ is a nice thing :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now