Loop a list by generic Iterator
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);
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Collection List:
- Create a list from an array
- Add element to a list at specified index
- Append one list to another list
- Insert a list at Specified Index
- Compare two List objects
- Convert Collection to List
- Convert a Queue to a List
- Convert List to array
- Convert List to a Set
- Copy List to Vector
- Copy one List to Another List
- Fill all elements in a list
- Fill n Copies of Specified Object
- Find maximum element in a List
- Find Minimum element in a List
- Get element by index
- Get Enumeration over List
- Get Synchronized List from a List
- Get Sub List
- Loop a list by Iterator
- Loop a list by generic Iterator
- Loop a list with using ListIterator
- Loop through a ist in reverse direction using ListIterator
- Remove an element by value and by index from List
- Remove all elements from a List
- Remove duplicate items from a List
- Remove item from a List while looping
- Remove range of elements from a List
- Remove user object from a list
- Replace an Element of List
- Replace all occurrences of specified element in a List
- Replace an element in a List using ListIterator
- Reverse a List
- Reverse order of all elements in a List
- Rotate elements of a list
- Search a List for an item with contains method
- Search elements of a List
- Binary Search a Sorted List
- Binary Search for a non-existent element in a List
- Set to change(replace) the value in a list
- Shuffle elements in a List
- Shuffle(repeatable) a List
- Sort a list
- Sort a List in descending order using comparator
- Swap elements of List