Java List Sort formatAsSortUniqCSVString(List s)

Here you can find the source of formatAsSortUniqCSVString(List s)

Description

list of strings to one string comma separated.
e.g.

License

Apache License

Parameter

Parameter Description
selection a parameter

Declaration

public static String formatAsSortUniqCSVString(List<String> s) 

Method Source Code

//package com.java2s;
/**//from  w  ww.ja  va2  s.c  o  m
 * OLAT - Online Learning and Training<br>
 * http://www.olat.org
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); <br>
 * you may not use this file except in compliance with the License.<br>
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing,<br>
 * software distributed under the License is distributed on an "AS IS" BASIS, <br>
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
 * See the License for the specific language governing permissions and <br>
 * limitations under the License.
 * <p>
 * Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
 * University of Zurich, Switzerland.
 * <p>
 */

import java.util.ArrayList;
import java.util.Collections;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import java.util.Map;
import java.util.Set;

public class Main {
    /**
     * list of strings to one string comma separated.<br>
     * e.g. ["z","a","b","c","s","a"] -> "a, b, c, s, z" No duplicates, alphabetically sorted
     * 
     * @param selection
     * @return
     */
    public static String formatAsSortUniqCSVString(List<String> s) {

        Map<String, String> u = new HashMap<String, String>();
        for (Iterator<String> si = s.iterator(); si.hasNext();) {
            u.put(si.next().trim(), null);
        }

        List<String> rv = new ArrayList<String>();
        rv.addAll(u.keySet());
        rv.remove("");
        Collections.sort(rv);

        return formatAsCSVString(rv);
    }

    /**
     * list of strings to one string comma separated.<br>
     * e.g. ["z","a","b","c","s","a"] -> "a, b, c, s, z" No duplicates, alphabetically sorted
     * 
     * @param selection
     * @return
     */
    public static String formatAsSortUniqCSVString(Set<String> s) {

        Map<String, String> u = new HashMap<String, String>();
        for (Iterator<String> si = s.iterator(); si.hasNext();) {
            u.put(si.next().trim(), null);
        }

        List<String> rv = new ArrayList<String>();
        rv.addAll(u.keySet());
        rv.remove("");
        Collections.sort(rv);

        return formatAsCSVString(rv);
    }

    /**
     * set of strings to one string comma separated.<br>
     * e.g. ["a","b","c","s"] -> "a,b,c,s"
     * 
     * @param selection
     * @return
     */
    public static String formatAsCSVString(Set<String> entries) {
        boolean isFirst = true;
        String csvStr = null;
        for (Iterator<String> iter = entries.iterator(); iter.hasNext();) {
            String group = iter.next();
            if (isFirst) {
                csvStr = group;
                isFirst = false;
            } else {
                csvStr += ", " + group;
            }
        }
        return csvStr;
    }

    /**
     * list of strings to one string comma separated.<br>
     * e.g. ["a","b","c","s"] -> "a,b,c,s"
     * 
     * @param selection
     * @return
     */
    public static String formatAsCSVString(List<String> entries) {
        boolean isFirst = true;
        String csvStr = null;
        for (Iterator<String> iter = entries.iterator(); iter.hasNext();) {
            String group = iter.next();
            if (isFirst) {
                csvStr = group;
                isFirst = false;
            } else {
                csvStr += ", " + group;
            }
        }
        return csvStr;
    }
}

Related

  1. calculateBox(List values, boolean copyAndSort)
  2. calculateQ1(List values, boolean copyAndSort)
  3. descendingSortByCreationTime(List tasks)
  4. equalUnsorted(List list1, List list2)
  5. extractRankedProducts( List unsortedProducts)
  6. getIndexForSortedInsert(final List listSorted, final String item)
  7. getNumberListSorted(String pagesStr)
  8. getOverlapCount(List list1, List list2, boolean sorted)
  9. getSortedFilesList(String filesList)