0

I want to know if Vector is holding <String> or <Integer>.

my function public void printVector(Vector <?> v){

I tried if(v instanceof <String>) but the compiler won't allow it. whats the issue?

  • *FYI:* You really shouldn't be using `Vector`. Recommendation (since Java 1.2 in 1998) is to use `ArrayList`. – Andreas Aug 27 '18 at 20:56
  • yeah, I know... the requirement is Vector though. @Andreas – Omri Dahan Aug 27 '18 at 20:58
  • A `Vector>` can hold many objects of *different* types, because the `?` *could* mean `Object`, so your vector can hold *both* `String` *and* `Integer` objects at the same time. Only way to know, is to check all elements in the vector. – Andreas Aug 27 '18 at 20:58
  • But how could I know if the vector that sent to the function is specific holding Strings or Ints? – Omri Dahan Aug 27 '18 at 20:59
  • You don't because of _type erasure_. What is the problem you're trying to solve? – Mick Mnemonic Aug 27 '18 at 21:00
  • You can't. [Type-erasure](https://stackoverflow.com/q/339699/5221149) prevents that. – Andreas Aug 27 '18 at 21:01
  • **the more specific problem** I'm sending a different vector to a function which decides what to do with the vector, according to the vector type. I'm simulating a company, I have a vector of Employees, Customers. So I need to know the difference so I could use it right.@MickMnemonic – Omri Dahan Aug 27 '18 at 21:05
  • Hey, a way you could use is to get an iterator for the vector, checking if it hasNext then using the getClass method on next. Note that this makes subclassing a problem (unless you have a few "superclasses" you want to test against) – Roy Shahaf Aug 27 '18 at 21:08

3 Answers3

0

A code example that may be relevant:

import java.util.Iterator;
import java.util.Vector;

public class Cool {

    public static void main(String[] args) {
         Vector<Integer> v;
         v = new Vector<>(5);
         v.add(Integer.valueOf(5));
         test(v);
    }

    private static void test(Vector<?> v) {
        Iterator<?> iterator = v.iterator();
        if (iterator.hasNext()) {
            System.out.println(iterator.next().getClass());     
        }
    }
}

This will print out class java.lang.Integer.

Your "test" method will have to check the type against a set of superclasses you're interested in (Employee/Customer/etc).

Also, Using vectors is generally considered bad practice in java.

Roy Shahaf
  • 484
  • 5
  • 10
0

Theoretically, you cannot know, because of type erasure. Basically, it means that you cannot get the generic type of the vector at runtime.

Now, in a real application, if you know that every objects are of the same type, you can get the first one and check its type with instanceof.

Note : this is not a good practice. Avoid that if you can. And as said by the others, you should consider other collections than Vector.

Note : as a rule of thumb, using instanceof is a clue of design flaw. Also, this is a costly operation.

If there is no first element, then your array is empty so you can just drop it.

Arnaud Denoyelle
  • 25,847
  • 10
  • 73
  • 128
0

As has been pointed out, Java's type erasure will make the compile-time generic type information unavailable run-time.

A common workaround for this is to pass the Class of the generic parameter as a method argument. This way you can then check whether the method was indeed passed a list of Customers or Employees:

 public void print(List<?> list, Class<?> clazz) {

    if (clazz == Employee.class) {
        // ...
    } else if (clazz == Customer.class) {
       // ...
    } else {
      // ...
    }        
}

You could then call the method as follows:

 Vector<Employee> legacy = new Vector<>();
 // do stuff       
 print(legacy, Employee.class);

Note that resorting to instanceof or class checks is usually a sign of bad object oriented design and in general, you can achieve a more elegant solution through polymorphism, i.e. overriding the print() method for different object types -- or having a single print() method that utilizes different toString() implementations of the domain objects.

Mick Mnemonic
  • 7,403
  • 2
  • 23
  • 27