java - HashMap values to Collection -


i have following code:

    hashmap<integer, string> h = new hashmap<integer, string>();     h.put(1, "a");     h.put(2, "b");     h.put(3, "c");     h.put(4, "d");      system.out.println(h); //{1=a, 2=b, 3=c, 4=d}      collection<string> vals = h.values();     system.out.println(vals); //[a, b, c, d]      iterator<string> itr = vals.iterator();      while (itr.hasnext()) //a b c d      {         system.out.print(itr.next() + " ");     } 

my questions:

  1. h.values() returns collection view of values in h. since vals interface, how can assign values interface (they cannot instantiated)? class implementing interface? object of class?

  2. similar question itr. know vals.iterator() returns first element of collection. how can assign interface instance?

the underlying principle governs answers questions called liskov's substitution principle applies in case assign value of instance of given interface (collection, iterator) reference type class implements interface (e.g. abstractcollection, anonymous class etc).

if see hashmap#values() method code, you'll see in java 8 source code:

public collection<v> values() {     collection<v> vs;     return (vs = values) == null ? (values = new values()) : vs; }  final class values extends abstractcollection<v> {     public final int size()                 { return size; }     public final void clear()               { hashmap.this.clear(); }     ... } 

thus, being returned either

  1. an instance of values class extends abstractcollection implements collection, or
  2. an instance of concrete subclass of abstractcollection (see: values = new abstractcollection<v>() @ line 386 in abstractmap.java in java 8 source code.

again, according lsp, valid. understand how wired up, you'll need grok jdk code base.

more answers

h.values() returns collection view of values in h. since vals interface, how can assign values interface (they cannot instantiated)?

to precise, vals not interface, instance thereof. it's true can not instantiate interface listener using new operator listener l = new listener(), according lsp, can instantiate concrete implementation of listener interface , assign variable type listener, example, listener listener = new seriallistener();

where class implementing interface?

in case, values class or abstractcollection class shown above.

where object of class?

in several cases, instance of anonymous inner class instantiated @ time of definition.

we know vals.iterator() returns first element of collection.

not quite right. returns instance of class implements iterator interface. if , when call next() method on returned object, first element of collection (assuming not empty).

how can assign interface instance?

the idea same. if variable on left hand side of assignment statement (left of = sign) refers interface, right hand side can refer reference object implements interface, directly or indirectly (via inheritance hierarchy).


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo