Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.Iterator;

public class Main {
    /**
     * Searches for and returns the first string which starts with the given
     * prefix. Removes the match from the collection.
     *
     * @param collection the collection.
     * @param prefix     the string prefix.
     * @return a string, or null if no matches.
     */
    public static String popStartsWith(Collection<String> collection, String prefix) {
        Iterator<String> iterator = collection.iterator();

        while (iterator.hasNext()) {
            String element = iterator.next();

            if (element != null && element.startsWith(prefix)) {
                iterator.remove();
                return element;
            }
        }

        return null;
    }
}