Get generic Iterator from generic ArrayList : ArrayList « Collections Data Structure « Java






Get generic Iterator from generic ArrayList

     
/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class OysterMonths {
  Collection<String> safeMonths;

  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) {
    return s.contains("r");
  }

  public static void main(String[] args) {
    OysterMonths om = new OysterMonths();
    DateFormatSymbols dfs = new DateFormatSymbols();
    String[] monthArray = dfs.getMonths();
    Collection<String> months = Arrays.asList(monthArray);
    om.safeMonths = om.filter(months);
    System.out.println("The following months are safe for oysters:");
    System.out.println(om.safeMonths);
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Get the size of an arraylist after and before add and remove methods
2.Use the Iterator returned from ArrayList to loop through an array list
3.Convert an ArrayList into an array.
4.Pre-generics example that uses a collection.
5.shows the modern, generic form of collection classes
6.Use set method to change the value in an array list
7.Store user-defined objects in arraylist
8.pass the actual object you want removed.
9.Convert a List (ArrayList) to an Array with zero length array
10.Convert a List (ArrayList) to an Array with full length array
11.Remove duplicate items from an ArrayList
12.Looping through a Collection object: while loop, iterator, and for each
13.Append all elements of other Collection to Java ArrayList
14.Copy all elements of Java ArrayList to an Object Array
15.Get Size of Java ArrayList and loop through elements
16.Add an element to specified index of Java ArrayList
17.Get Sub List of Java ArrayList
18.Insert all elements of other Collection to Specified Index of Java ArrayList
19.Iterate through elements Java ArrayList using Iterator
20.Iterate through elements Java ArrayList using ListIterator
21.Remove all elements from Java ArrayList
22.Remove an element from specified index of Java ArrayList
23.Search an element of Java ArrayList
24.Get element in an ArrayList by index
25.Replace an element at specified index of Java ArrayList
26.Sort elements of Java ArrayList
27.Copy Elements of ArrayList to Java Vector
28.Copy Elements of One Java ArrayList to Another Java ArrayList
29.Find maximum element of Java ArrayList
30.Find Minimum element of Java ArrayList
31.Get Enumeration over Java ArrayList
32.Get Synchronized List from Java ArrayList
33.Perform Binary Search on Java ArrayList
34.Replace All Elements Of Java ArrayList
35.Replace all occurrences of specified element of Java ArrayList
36.Reverse order of all elements of Java ArrayList
37.Shuffle elements of Java ArrayList
38.Swap elements of Java ArrayList
39.Iterate through a Collection using Java Iterator
40.Sort Java ArrayList in descending order using comparator
41.Remove an element from Collection using Java Iterator
42.Replace an element from ArrayList using Java ListIterator
43.How to Convert an ArrayList into an array
44.Sort items of an ArrayList
45.Rotate elements of a collection
46.Search collection element
47.If an ArrayList contains a given item
48.Convert Collection to ArrayList?
49.A boolean is being stored and then retrieved from an ArrayList
50.Traverse through ArrayList in reverse direction using Java ListIterator
51.Traverse through ArrayList in forward direction using Java ListIterator
52.Remove an element from ArrayList using Java ListIterator
53.Get Previous and next index using Java ListIterator
54.A Map implemented with ArrayLists
55.A variant of java.util.ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array
56.Copy On Write ArrayList
57.This program demonstrates the ArrayList class
58.ArrayList with sorted contents.