[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Which collection to Choose?

Posted on 2004-10-29
10
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

656 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