• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you clarify why an ArrayList that according to my interpretation, contains Future (rather than FutureTask - which is an implementation of interface Future) threads (objects) instantiated with Integers (line 59) were used in the following code snippet?


source Core Java Vol.1 (8th Ed) Chapter 14, Listing 14.11




 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:Can you clarify why an ArrayList that according to my interpretation, contains Future (rather than FutureTask - which is an implementation of interface Future) threads (objects) instantiated with Integers (line 59) were used in the following code snippet?

If your question is specifically about why an ArrayList<Future<Integer>> rather than an ArrayList<FutureTask<Integer>> is used: that's because in general it is better to program against an interface, not an implementation. In fact, that advice has been followed only halfway in the code, it would have been more consistent if the variable was declared as a List instead of an ArrayList:

 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

to elaborate the idea on "program against interface and not on implementation" you can also refer Head first design pattern book. main purpose of these kind of code is they are more maintainable and more flexible to adapt any change in future.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javin Paul wrote:
to elaborate the idea on "program against interface and not on implementation" you can also refer Head first design pattern book. main purpose of these kind of code is they are more maintainable and more flexible to adapt any change in future.


Sure, is it this book?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic