Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
- Question: If you need a
Set
implementation that provides value-ordered iteration, which class should you use?Answer:
HashSet
LinkedHashSet
TreeSet
TreeSet
guarantees that the sorted set is in ascending element order, sorted according to the natural order of the elements or by theComparator
provided.- Question: Which of the following classes implements a FIFO
Queue
?Answer:
HashSet
LinkedList
PriorityQueue
CopyOnWriteArraySet
LinkedList
implements a FIFOQueue
.- Question: Which class do you use to access wrapper implementations? Answer: You use the
Collection
class, which provides static methods that operate on or return collections.- Question: Which class and static method can you use to convert an
array
to aList
? Answer: TheArrays.asList
method returns a fixed-sizeList
view of its array argument.
- Exercise: In the Interfaces exercises, you created a method called
filter
that (1) traversed through the elements in aCollection
using anIterator
, (2) checked whether eachString
met a certain condition, and (3) if theString
met the condition, added it to a newCollection
to return.We will now use this method in a complete program that tells us which months it is safe to eat oysters (months with names that contain "r"). Following is partial code for that program, with comments where you need to add statements.
Answer: Following is the fullimport java.util.Collection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.text.DateFormatSymbols; public class OysterMonths { Collection <String> safeMonths; public static void main(String[] args) { OysterMonths om = new OysterMonths(); DateFormatSymbols dfs = new DateFormatSymbols(); String[] monthArray = dfs.getMonths(); /* (1)Create a Collection that stores the month names. (2)Use the filter method to store in the safeMonths Collection the names of the safe months. (3)Print out the names of the safe months. */ } public Collection <String> filter (Collection<String> c) { Collection <String> filteredCollection = new ArrayList<String>(); for (Iterator<String> i = c.iterator(); i.hasNext(); ) { String s = i.next(); if (condition(s)) { filteredCollection.add(s); } } return filteredCollection; } public boolean condition(String s) { if (s.contains("r")) { return true; } return false; } }main
method from theOysterMonths
program.Following is the output of this program.public static void main(String[] args) { OysterMonths om = new OysterMonths(); DateFormatSymbols dfs = new DateFormatSymbols(); String[] monthArray = dfs.getMonths(); Collectionmonths = Arrays.asList(monthArray); om.safeMonths = om.filter(months); System.out.println("The following months are safe for oysters:"); System.out.println(om.safeMonths); }The following months are safe for oysters: [January, February, March, April, September, October, November, December]
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.