Java String Remove parseAndRemoveDuplicates(String s, String del)

Here you can find the source of parseAndRemoveDuplicates(String s, String del)

Description

parse And Remove Duplicates

License

Apache License

Declaration

public static String parseAndRemoveDuplicates(String s, String del) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    public static String parseAndRemoveDuplicates(String s, String del) {
        if (s == null)
            return s;

        Set<String> set = parseAndRemoveDuplicatesAsSet(s, del);

        return getSeparatedListOfString(set, "", "", false, del);
    }/*from   w w  w .  j  av  a 2 s .  co  m*/

    public static Set<String> parseAndRemoveDuplicatesAsSet(String s,
            String del) {
        if (s == null)
            return Collections.emptySet();
        List<String> list = parseString(s, del);
        Set<String> set = new HashSet<String>();
        for (String item : list) {
            set.add(item);
        }
        return set;
    }

    public static String getSeparatedListOfString(Collection<String> list,
            String open, String close, boolean singleQuoteElement,
            String separator) {
        if (list == null)
            return null;
        StringBuffer buffer = new StringBuffer(open);
        for (Iterator<String> it = list.iterator(); it.hasNext();) {
            String item = it.next();
            if (singleQuoteElement) {
                buffer.append("'" + item + "'");
            } else {
                buffer.append(item);
            }
            if (it.hasNext()) {
                buffer.append(separator);
            }
        }
        buffer.append(close);
        return buffer.toString();
    }

    public static List<String> parseString(String s) {
        return parseString(s, ",");
    }

    public static List<String> parseString(String s, String del) {
        return parseString(s, del, false);
    }

    public static List<String> parseString(String s, String del,
            boolean ignoreNull) {
        return parseString(s, del, ignoreNull, false);
    }

    public static List<String> parseString(String s, String del,
            boolean ignoreNull, boolean ignoreDup) {
        if (s == null)
            return null;
        List<String> ret = new ArrayList<String>();
        if (del == null) {
            ret.add(s);
            return ret;
        }
        StringTokenizer token = new StringTokenizer(s, del);
        while (token.hasMoreTokens()) {
            String tokenSingle = token.nextToken();
            boolean toAdd = true;
            if (ignoreNull) {
                tokenSingle = tokenSingle.trim();
                if ("".equals(tokenSingle)) {
                    toAdd = false;
                }
            }
            if (toAdd) {
                if (ignoreDup) {
                    toAdd = !ret.contains(tokenSingle);
                }
                if (toAdd) {
                    ret.add(tokenSingle);
                }
            }
        }
        return ret;
    }
}

Related

  1. getAndRemove(String key)
  2. removeAll(String origStr, String removeStr)
  3. removeAll(String targetStr, String removeStr)
  4. removeAndConcatenateAlternate(String s1, String s2)
  5. removeBlankLines(String s)