2

I want to store a data structure thats a table with one column Strings and another column of Ints.

List<Entry<String, Integer>> li = new LinkedList<Entry<String, Integer>>(); 

would do the job for a list but I would rather have the performance of and need the memory of an array.

I tried

Entry<String, Integer>[]  = new Entry<String, Integer>[10];

but that doesn't seem to work.

Is Entry the right datatype to use?

6
  • 1
    Are either the Strings or Integers unique by definition for your problem? If so, a Dictionary might be more appropriate. Commented Apr 9, 2010 at 10:53
  • 1
    @Marcelo: did you mean a Map? Commented Apr 9, 2010 at 10:54
  • 1
    Are you sure performance will be an issue? What kind of operations are you performing? Commented Apr 9, 2010 at 11:32
  • 1
    Linked lists and arrays have very different performance characteristics. However you can have ArrayList which is an linked list backed by an array. Commented Apr 9, 2010 at 11:37
  • @hhafez You mean a List backed by and array... Commented Apr 9, 2010 at 11:46

5 Answers 5

3

Write a class that represents your tuple and use an array (or a List) of that type. Don't eschew classes, they are your friends!

And what exactly do you mean by "the performance of an array"? Arrays are not necessarily faster than List implementations.

Think of inserting an element at the position 0: A LinkedList can do it in O(1). To get the same effect in an array, you'd have to do an O(n) operation (recreating the array and copying all existing values).

Sign up to request clarification or add additional context in comments.

Comments

2

If you really need an array of generic types, you can do this:

Entry<String, Integer>[] array = new Entry[10];

It gives you a compilation warning though. You can read more about it here:

http://www.devarticles.com/c/a/Java/Wildcards-Arrays-and-Generics-in-Java/2/

3 Comments

It doesn't compile because it's missing the variable name. But the compiler will still warn you about the unchecked conversion to Entry<String, Integer> even if you add the variable name.
Is there a way to do this correctly without the compiler complaining?
Well, you can create a separate class and use it instead of Entry to form the array.
1

I don't know what is not working, but:

  1. you have to give a name to your array.
  2. you can't construct arrays with generic types
  3. don't forget Entry is an interface.

So, this:

Entry<String, Integer>[] = new Entry<String, Integer>[10];

Should be this:

Entry<String, Integer>[] entries = new Entry[10];

Hope this helps!

Comments

1

Maybe you can just use ArrayList, shouldn't be much difference in performance compared to a plain array.

Comments

1

Use this,

List<Entry<String, Integer>> li = new ArrayList<Entry<String, Integer>>(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.