File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes declaring a new Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "declaring a new Vector" Watch "declaring a new Vector" New topic
Author

declaring a new Vector

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
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

Joined: Jan 10, 2002
Posts: 61084
    
  66

Why a Vector?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rajat Jindal
Ranch Hand

Joined: Sep 19, 2011
Posts: 42


where set is


I believe this will work

But same question why a Vector ??
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
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

Joined: Sep 19, 2011
Posts: 42
In case you are not using MultiThreading in your environment , you should use ArrayList because Vector is a legacy collection class.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Sep 19, 2011
Posts: 42
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

Joined: Oct 13, 2005
Posts: 38441
    
  23
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: declaring a new Vector