|     | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
 
- Question: If you need a
Setimplementation that provides value-ordered iteration, which class should you use?Answer:
HashSet
LinkedHashSet
TreeSetTreeSetguarantees that the sorted set is in ascending element order, sorted according to the natural order of the elements or by theComparatorprovided.- Question: Which of the following classes implements a FIFO
Queue?Answer:
HashSet
LinkedList
PriorityQueue
CopyOnWriteArraySetLinkedListimplements a FIFOQueue.- Question: Which class do you use to access wrapper implementations? Answer: You use the
Collectionclass, which provides static methods that operate on or return collections.- Question: Which class and static method can you use to convert an
arrayto aList? Answer: TheArrays.asListmethod returns a fixed-sizeListview of its array argument.
- Exercise: In the Interfaces exercises, you created a method called
filterthat (1) traversed through the elements in aCollectionusing anIterator, (2) checked whether eachStringmet a certain condition, and (3) if theStringmet the condition, added it to a newCollectionto 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; } }mainmethod from theOysterMonthsprogram.
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.