Java SortedSet splitSorted(String str, String sepStr)

Here you can find the source of splitSorted(String str, String sepStr)

Description

Split the given string using the given separate, returning the components as a SortedSet.

License

Apache License

Parameter

Parameter Description
str String to be split.
sepStr Separator string that lies between values.

Return

SortedSet of separated substrings. The set may be empty but it will not be null.

Declaration

public static SortedSet<String> splitSorted(String str, String sepStr) 

Method Source Code

//package com.java2s;
/*//from   w  w  w .  jav  a2s  .  co  m
 * Copyright (C) 2014 Dell, Inc.
 * 
 * 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.apache.org/licenses/LICENSE-2.0
 * 
 * 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.ArrayList;

import java.util.HashSet;
import java.util.List;

import java.util.Set;
import java.util.SortedSet;

import java.util.TreeSet;

public class Main {
    /**
     * Split the given string using the given separate, returning the components as a
     * SortedSet. This method is similar to {@link #split(String, String)} except that
     * values are sorted by string value. If a null or empty string is passed, an empty
     * set is returned.
     *
     * @param str       String to be split.
     * @param sepStr    Separator string that lies between values.
     * @return          SortedSet of separated substrings. The set may be empty but it
     *                  will not be null.
     */
    public static SortedSet<String> splitSorted(String str, String sepStr) {
        // Split but watch out for empty substrings.
        SortedSet<String> result = new TreeSet<String>();
        if (str != null) {
            for (String value : str.split(sepStr)) {
                if (value.length() > 0) {
                    result.add(value);
                }
            }
        }
        return result;
    }

    /**
     * Split the given string using the given separate, returning the components as a
     * set. This method does the opposite as {@link #concatenate(Collection, String)}.
     * If a null or empty string is passed, an empty set is returned.
     *
     * @param str       String to be split.
     * @param sepStr    Separator string that lies between values.
     * @return          Set of separated substrings. The set may be empty but it will
     *                  not be null.
     */
    public static Set<String> split(String str, String sepStr) {
        // Split but watch out for empty substrings.
        Set<String> result = new HashSet<String>();
        if (str != null) {
            for (String value : str.split(sepStr)) {
                if (value.length() > 0) {
                    result.add(value);
                }
            }
        }
        return result;
    }

    /**
     * Split the given string by a separator char. Unlike split(String,String), doesn't use RegEx
     *
     * @param str       String to be split.
     * @param sepChr    Separator character that lies between values.
     * @return          List of separated substrings
     */
    public static List<String> split(String str, char sepChr) {
        List<String> result = new ArrayList<String>();
        int idx = 0;
        while (true) {
            int idx2 = str.indexOf(sepChr, idx);
            if (idx2 < 0) {
                result.add(str.substring(idx));
                break;
            }
            result.add(str.substring(idx, idx2));
            idx = idx2 + 1;
        }
        return result;
    }

    /**
     * Return the first index where the given character occurs in the given buffer or -1
     * if is not found. This is like String.indexOf() but it works on byte[] arrays. If
     * the given buffer is null or empty, -1 is returned.
     * 
     * @param buffer    byte[] to search.
     * @param ch        Character to find.
     * @return          Zero-relative index where character was first found or -1 if the
     *                  character does not occur or is not found.
     */
    public static int indexOf(byte[] buffer, char ch) {
        if (buffer == null) {
            return -1;
        }
        for (int index = 0; index < buffer.length; index++) {
            if (buffer[index] == ch) {
                return index;
            }
        }
        return -1;
    }
}

Related

  1. setminus(SortedSet pSet1, SortedSet pSet2)
  2. sorted(Iterable input)
  3. sortedByValues(Map map, final boolean asc)
  4. sortedConfigKeys(Iterable> conf)
  5. sortedSet()
  6. toSortedSet(Comparator comparator, Value... values)
  7. unionSortedSet(Set... sets)
  8. valueSortedMap(Map map, Comparator> comparator)