?
Solved

Which collection to Choose?

Posted on 2004-10-29
10
Medium Priority
?
250 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
Comment
Question by:srivenky
  • 6
  • 3
10 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12447212
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
ID: 12447259
use Hashtable or Hashmap
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 150 total points
ID: 12447305
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12447311
You will get an Idea from above example
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12447739
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
ID: 12447757
If order *is* important, use a LinkedHashSet
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12453236
:)
0
 
LVL 86

Expert Comment

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

Expert Comment

by:sudhakar_koundinya
ID: 12453343
no problems ;-)

Regards
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month6 days, 16 hours left to enroll

593 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