Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declaring a new Vector

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I just need a little help with declaring a new Vector. Note the following code:

TagReader tReader = csc.getFileSet();
Vector vec = new Vector(tReader.getNodes().keySet());
Collections.sort(vec);

In Eclipse the second line is underlined and a message says: Type safety: The constructor Vector(Collection) belongs to the raw type Vector. References to generic type Vector<E> should be parameterized.
The third line is underlined and a message says: Type safety: Unchecked invocation sort(Vector) of the generic method sort(List<T>) of type Collections

I want to fix this. The parameter tReader.getNodes().keySet() returns a Set<String>. How is Vector supposed to be parameterized? Please advise.

Alan
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why a Vector?
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


where set is


I believe this will work

But same question why a Vector ??
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajat Jindal wrote:

where set is


I believe this will work

But same question why a Vector ??


You think an ArrayList would be better?

TagReader tReader = csc.getFileSet();
ArrayList<String> alist = new ArrayList<String>(tReader.getNodes().keySet());
Collections.sort(alist);
Iterator<String> iter = alist.iterator();
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case you are not using MultiThreading in your environment , you should use ArrayList because Vector is a legacy collection class.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And declare it as List, so you can change it later if needed (e.g. wrap it with Collections.unmodifiableList):
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajat Jindal wrote:In case you are not using MultiThreading in your environment , you should use ArrayList because Vector is a legacy collection class.


Even in a multithreaded environment, don't use Vector. Use an ArrayList wrapped in a Collections.unmodifiableList(), or something from java.util.concurrent. The only reason to use Vector would be if you have to for interaction with legacy code.
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

Even in a multithreaded environment, don't use Vector. Use an ArrayList wrapped in a Collections.unmodifiableList(), or something from java.util.concurrent. The only reason to use Vector would be if you have to for interaction with legacy code.


Yes, Verdegan is absolutely right , you should use ArrayList instead of Vector.You can also use Collections class static method Collections.synchronizedList(List<T> list) which will "Returns a synchronized (thread-safe) list ".

Rob Spoor wrote:And declare it as List, so you can change it later if needed (e.g. wrap it with Collections.unmodifiableList):

In case you are not going to use ArrayList specific methods, it a good idea to declare it as List. So that tomorrow if you feels it is better to have LinkedList instead of ArrayList ( for instance you feel insertion and deletion is more frequent in the middle of the list ), then you just need to replace the ArrayList with LinkedList and rest of the code will remain same.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few places in the API where a Vector is required as a parameter. For example: JComboBox constructors. They really ought to bring that up to date by overloading it with a constructor taking a List<T> as a parameter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic