• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

declaring a new Vector

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why a Vector?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic