The JavaTM Tutorial
Previous Page Lesson Contents Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Collections
Lesson: Implementations

Answers to Questions and Exercises:

Questions

  1. Question: If you need a Set implementation that provides value-ordered iteration, which class should you use?
    • HashSet
    • LinkedHashSet
    • TreeSet

    Answer: TreeSet guarantees that the sorted set is in ascending element order, sorted according to the natural order of the elements or by the Comparator provided.

  2. Question: Which of the following classes implements a FIFO Queue?
    • HashSet
    • LinkedList
    • PriorityQueue
    • CopyOnWriteArraySet

    Answer: LinkedList implements a FIFO Queue.

  3. 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.

  4. Question: Which class and static method can you use to convert an array to a List?

    Answer: The Arrays.asList method returns a fixed-size List view of its array argument.

Exercises

  1. Exercise: In the Interfaces exercises, you created a method called filter that (1) traversed through the elements in a Collection using an Iterator, (2) checked whether each String met a certain condition, and (3) if the String met the condition, added it to a new Collection 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.

    import 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;
        }
    }
    

    Answer: Following is the full main method from the OysterMonths (in a .java source file) program.

    public static void main(String[] args) {
        OysterMonths om = new OysterMonths();
        DateFormatSymbols dfs = new DateFormatSymbols();
        String[] monthArray = dfs.getMonths();
        
        Collection  months = Arrays.asList(monthArray);                
    
        om.safeMonths = om.filter(months);
    
        System.out.println("The following months are safe for oysters:");
    
        System.out.println(om.safeMonths);
    }
    
    Following is the output of this program.
    The following months are safe for oysters:
    [January, February, March, April, September, October, November, December]
    

Previous Page Lesson Contents Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.