Java Collection Element Get getCommonSuffix(Collection c)

Here you can find the source of getCommonSuffix(Collection c)

Description

get Common Suffix

License

Apache License

Declaration

public static String getCommonSuffix(Collection<String> c) 

Method Source Code

//package com.java2s;
/*//  w ww . jav  a 2 s .  c  om
 * Copyright 2004-2010 Information & Software Engineering Group (188/1)
 *                     Institute of Software Technology and Interactive Systems
 *                     Vienna University of Technology, Austria
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.ifs.tuwien.ac.at/dm/somtoolbox/license.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /** Returns the common starting portion of the two Strings, or an empty String if there is no common part. */
    public static String getCommonSuffix(String s1, String s2) {
        int i = 0;
        if (s1 == null || s2 == null || s1.length() == 0
                || s2.length() == 0) {
            return "";
        }
        while (i < s1.length()
                && i < s2.length()
                && s1.charAt(s1.length() - 1 - i) == s2.charAt(s2.length()
                        - 1 - i)) {
            i++;
        }
        return s1.substring(s1.length() - i);
    }

    public static String getCommonSuffix(Collection<String> c) {
        if (c.size() == 0) {
            return "";
        }

        Iterator<String> iterator = c.iterator();
        String commonSuffix = iterator.next();
        while (iterator.hasNext()) {
            commonSuffix = getCommonSuffix(commonSuffix, iterator.next());
        }
        return commonSuffix;
    }

    public static String getCommonSuffix(String[] a) {
        if (a.length == 0) {
            return "";
        }

        String commonSuffix = a[0];
        for (int i = 1; i < a.length; i++) {
            commonSuffix = getCommonSuffix(commonSuffix, a[i]);
        }
        return commonSuffix;
    }
}

Related

  1. getCommaSeparatedString(Collection collection)
  2. getCommaSeparatedString(Collection values)
  3. getCommaSeparatedStringFromCollection(Collection collections)
  4. getCommon(Collection c1, Collection c2)
  5. getCommonElements( Collection from, Collection to)
  6. getComponentName(Collection existingNames, String name)
  7. getDocIdString(Collection docIds)
  8. getElement(final int index, final Collection coll)
  9. getElementClass(Collection collection)