*
The moose likes Beginning Java and the fly likes ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();" Watch "ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();" New topic
Author

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

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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





Jon
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


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.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();
 
Similar Threads
Overwriting Objects in ArrayList
A Timeout scenario