It's not a secret anymore!
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: 662

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: 14957
    
  26

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 8 API documentation
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 294


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.


SQL Query Interview Question
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 662

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();
 
jQuery in Action, 3rd edition