-1

if my method is accepting Queues from any type, how to determine if the method arg is ArrayBlockedQueue() or LinkedList() queue?

I used .equals as the following its always returning true

Queue q1 = new LinkedList();
Queue q2 = new ArrayBlockingQueue(4);
System.out.println(l1.equals(q1));

then I tried the following I have faced Illegal argument exception

public static void add2Q (Queue q)  {
    if(q.equals( new ArrayBlockingQueue(q.size()))){
        q.offer(2);
        q.offer(5);
        q.offer(9);
        q.offer(0);
        q.offer(-1);
    } else {
        // thorws exception with add if exceeded 
        try {
            q.add(2);
            q.add(5);
            q.add(4);
            q.add(9);
            q.add(10);
        } catch (IllegalStateException e){
            System.out.println("you are using ArrayBlockQueue of size "+ q.size() );
            //e.printStackTrace();
        }   
    }   
}
2
  • Why? You should not care. That's the whole point of being passed a Queue object instead of a specific type. Commented Nov 30, 2017 at 2:12
  • @EJP this is only for the sake of examining which method to use when adding element to the Queue Commented Nov 30, 2017 at 2:26

1 Answer 1

0

If you want to determine the type of object:

if (l1 instanceof LinkedList) {
   //Do something
}

The same solution will apply for other objects as well.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.