Which collection to Choose?

I will implement the following scenario. Please let me know which java colletion to use.

I will be adding 1000's of data inside, the order in which the data is inserted
 is more important. It should not be sorted automatically.

The collection should not have duplicate values. I will be doing a check(if neccessary) to see if duplicate exists.

Scrolling the container should be faster. While scrolling it should not change the order.

LVL 3
srivenkyAsked:
Who is Participating?
 
sudhakar_koundinyaCommented:
import java.util.*;
import java.text.*;
public class OrderedAccess {
  public static void main(String args[]) {
    String months[] =
      new DateFormatSymbols().getMonths();
    String italianMonths[] =
      new DateFormatSymbols(Locale.ITALIAN).getMonths();
    List list = Arrays.asList(months);
    Set orderedSet = new LinkedHashSet(list);
    Set unorderedSet = new HashSet(list);
    System.out.println("Ordered:   " + orderedSet);
    System.out.println("Unordered: " + unorderedSet);
    Map orderedMap = new LinkedHashMap();
    Map unorderedMap = new HashMap();
    for (int i=0, n=months.length; i < n; i++) {
      orderedMap.put(months[i], italianMonths[i]);
      unorderedMap.put(months[i], italianMonths[i]);
    }
    System.out.println("Ordered:   " + orderedMap);
    System.out.println("Unordered: " + unorderedMap);
    Collection values = orderedMap.values();
    for (Iterator i = values.iterator(); i.hasNext();
      System.out.println(i.next()));
    Map accessorderedMap =
      new LinkedHashMap(20, .80f, true);
    for (int i=0, n=months.length; i < n; i++) {
      accessorderedMap.put(months[i], italianMonths[i]);
    }
    accessorderedMap.get("June");
    accessorderedMap.get("April");
    accessorderedMap.get("February");
    System.out.println(accessorderedMap);
  }
}
0
 
petmagdyCommented:
U can use Vector, and to ensure efficency do like this:

Vector longList = new Vector()
longList.ensureCapacity(1000);

//then start filling
0
 
sudhakar_koundinyaCommented:
use Hashtable or Hashmap
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
sudhakar_koundinyaCommented:
You will get an Idea from above example
0
 
sudhakar_koundinyaCommented:
0
 
CEHJCommented:
Use a Set. If you don't need to worry about order, HashSet will be fine.

>>I will be doing a check(if neccessary) to see if duplicate exists.

It won't be, as long as the equals method of the contained object is implemented properly
0
 
CEHJCommented:
If order *is* important, use a LinkedHashSet
0
 
sudhakar_koundinyaCommented:
:)
0
 
CEHJCommented:
Sorry sudhakar - i didn't notice your code does what i suggested ;-)
0
 
sudhakar_koundinyaCommented:
no problems ;-)

Regards
0
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.

All Courses

From novice to tech pro — start learning today.