Solved

Which collection to Choose?

Posted on 2004-10-29
197 Views
Last Modified: 2010-08-05
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.

0
Question by:srivenky
    10 Comments
     
    LVL 13

    Expert Comment

    by:petmagdy
    U can use Vector, and to ensure efficency do like this:

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

    //then start filling
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    use Hashtable or Hashmap
    0
     
    LVL 14

    Accepted Solution

    by:
    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
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    You will get an Idea from above example
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    If order *is* important, use a LinkedHashSet
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    :)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Sorry sudhakar - i didn't notice your code does what i suggested ;-)
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    no problems ;-)

    Regards
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    no14 challenge 14 40
    tenRun challenge 28 50
    Help refactoring a java code 8 37
    array11 challenge 16 20
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    910 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now