Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**
     * Creates new collection that contains only element whose string
     * representation starts with prefix It is parameterized.
     * 
     * @param c
     *            raw collection to create prefixed collection from
     * @param prefix
     *            to use as filter
     * @return collection without nulls
     */
    public static <T> Collection<T> withPrefix(Collection<T> c, String prefix) {
        Collection<T> prefixed = new ArrayList<T>();
        Iterator<T> iterator = c.iterator();
        while (iterator.hasNext()) {
            T o = iterator.next();
            if (o != null && o.toString().startsWith(prefix)) {
                prefixed.add(o);
            }
        }
        return prefixed;
    }
}