Returns string keys in a hashtable as delimited string - Java java.util

Java examples for java.util:Hashtable Operation

Description

Returns string keys in a hashtable as delimited string

Demo Code


//package com.java2s;

import java.util.Enumeration;
import java.util.Hashtable;

public class Main {
    /**//from w w w  .j  a  v a2 s.  c  o  m
     * Returns string keys in a hashtable as delimited string
     *
     * @param ht
     *            , Hashtable
     * @param delimiter
     *            , String
     * @param exclude
     *            , exclude channel if present as substring
     * @return , string array with hash keys string
     */
    public static synchronized String hashTableKeysToDelimitedString(
            Hashtable ht, String delimiter, String exclude) {

        StringBuffer sb = new StringBuffer();
        boolean first = true;
        Enumeration e = ht.keys();

        while (e.hasMoreElements()) {

            String s = (String) e.nextElement();

            if (exclude != null) {
                if (s.indexOf(exclude) != -1) {
                    continue;
                }
            }
            if (first) {
                sb.append(s);
                first = false;
            } else {
                sb.append(delimiter).append(s);
            }
        }
        return sb.toString();

    }

    /**
     * Returns string keys in a hashtable as delimited string
     *
     * @param ht
     *            , Hashtable
     * @param delimiter
     *            , String
     * @return , string array with hash keys string
     */
    public static String hashTableKeysToDelimitedString(Hashtable ht,
            String delimiter) {

        return hashTableKeysToDelimitedString(ht, delimiter, null);

    }
}

Related Tutorials