Example usage for java.util Hashtable entrySet

List of usage examples for java.util Hashtable entrySet

Introduction

In this page you can find the example usage for java.util Hashtable entrySet.

Prototype

Set entrySet

To view the source code for java.util Hashtable entrySet.

Click Source Link

Usage

From source file:MainClass.java

public static void main(String[] s) {
    Hashtable table = new Hashtable();
    table.put("key1", "value1");
    table.put("key2", "value2");
    table.put("key3", "value3");

    Set set = table.entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        System.out.println(entry.getKey() + " : " + entry.getValue());
    }/*from  w ww.ja va2s .  c o m*/
}

From source file:Main.java

public static void main(String args[]) {
    Hashtable<Integer, String> htable = new Hashtable<Integer, String>();

    // put values into the table
    htable.put(1, "A");
    htable.put(2, "B");
    htable.put(3, "C");
    htable.put(4, "from java2s.com");

    // create a set view
    Set<Entry<Integer, String>> nset = htable.entrySet();

    // display set result
    System.out.println("Set result:" + nset);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    Hashtable hash = new Hashtable(89);
    hash.put("one", "two");
    hash.put("two", "three");
    hash.put("three", "four");
    hash.put("four", "five");
    System.out.println(hash);/*www .  ja  v  a2 s.  com*/
    System.out.println(hash.size());
    Enumeration e = hash.keys();
    while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        System.out.println(key + " : " + hash.get(key));
    }
    Set set = hash.entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        System.out.println(entry.getKey() + " : " + entry.getValue());
    }
}

From source file:com.srotya.tau.nucleus.Utils.java

public static Map<String, String> hashTableTohashMap(Hashtable<Object, Object> table) {
    Map<String, String> map = new HashMap<>();
    for (Entry<Object, Object> entry : table.entrySet()) {
        map.put(entry.getKey().toString(), entry.getValue().toString());
    }//w  w  w . j a  va 2 s. com
    return map;
}

From source file:net.giovannicapuano.visualnovel.Memory.java

public static boolean putKeyValues(Context context, Hashtable<String, String> keyvalues) {
    try {//from  w  w w .j  a  v a 2  s  .c  o m
        JSONObject json = new JSONObject();

        for (Entry<String, String> entry : keyvalues.entrySet())
            json.put(entry.getKey(), entry.getValue());

        FileOutputStream fos = context.openFileOutput(KEYVALUES, Context.MODE_PRIVATE);
        fos.write(json.toString().getBytes());
        fos.flush();
        fos.close();

        return true;
    } catch (Exception e) {
        Utils.error(e);
        Utils.error(context, context.getString(R.string.error_saving_game));
        return false;
    }
}

From source file:org.apache.torque.util.VillageUtils.java

/**
 * Converts a hashtable to a byte array for storage/serialization.
 *
 * @param  hash  The Hashtable to convert.
 *
 * @return  A byte[] with the converted Hashtable.
 *
 * @throws  Exception  If an error occurs.
 *//*from   w w  w. j  ava2 s  .c  o  m*/
public static byte[] hashtableToByteArray(final Hashtable hash) throws Exception {
    Hashtable saveData = new Hashtable(hash.size());
    byte[] byteArray = null;

    Iterator keys = hash.entrySet().iterator();
    while (keys.hasNext()) {
        Map.Entry entry = (Map.Entry) keys.next();
        if (entry.getValue() instanceof Serializable) {
            saveData.put(entry.getKey(), entry.getValue());
        }
    }

    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    ObjectOutputStream out = null;
    try {
        // These objects are closed in the finally.
        baos = new ByteArrayOutputStream();
        bos = new BufferedOutputStream(baos);
        out = new ObjectOutputStream(bos);

        out.writeObject(saveData);

        out.flush();
        bos.flush();
        baos.flush();
        byteArray = baos.toByteArray();
    } finally {
        close(out);
        close(bos);
        close(baos);
    }
    return byteArray;
}

From source file:org.hdiv.util.RequestUtilsHDIV.java

/**
 * Returns true only if url points to a resource that does not require
 * a HDIV state, as an image or pdf file. 
 * Before calling this method verify that it is an internal url
 * /*from  w  w  w .  ja  va 2  s .  c o  m*/
 * @param url
 * @return
 */
public static boolean isResourceUrl(HDIVConfig hdivConfig, String url) {

    // Remove parameters
    if (url.indexOf("?") > 0) {
        url = url.substring(0, url.indexOf("?"));
    }

    // Remove anchor
    if (url.indexOf("#") > 0) {
        url = url.substring(0, url.indexOf("#"));
    }

    if (url.endsWith("/")) {
        return false;
    }

    Hashtable protectedExtensions = hdivConfig.getProtectedURLPatterns();

    Iterator it = protectedExtensions.entrySet().iterator();
    while (it.hasNext()) {
        Entry valor = (Entry) it.next();
        Pattern allowedExtension = (Pattern) valor.getValue();
        if (allowedExtension.matcher(url).matches()) {
            return false;
        }
    }

    // If url ends with .../module verify that has no dot (.) on it.
    // If contains a dot it may be a type of file extension .../mod.gif
    String suffix = "";
    if (url.indexOf("/") > 0) {
        suffix = url.substring(url.lastIndexOf("/") + 1);
    } else {
        suffix = url;
    }
    if (suffix.indexOf(".") < 0) {
        return false;
    }

    return true;

}

From source file:org.wso2.carbon.pc.analytics.core.generic.utils.AnalyticsUtils.java

/**
 * Get sorted list (sort by long type keys)
 *
 * @param table is a hash table to keep the result as key-value pairs
 * @param key1  is the name for the first value of the JSON object
 * @param key2  is the name for the second value for the JSON object
 * @return a sorted list as a JSON array string
 * @throws JSONException//from   ww w .ja  v  a 2  s .  com
 */
public static String getLongKeySortedList(Hashtable<Long, Integer> table, String key1, String key2)
        throws JSONException {
    ArrayList<Map.Entry<Long, Integer>> l = new ArrayList(table.entrySet());
    Collections.sort(l, new Comparator<Map.Entry<Long, Integer>>() {
        public int compare(Map.Entry<Long, Integer> o1, Map.Entry<Long, Integer> o2) {
            return o1.getKey().compareTo(o2.getKey());
        }
    });
    JSONArray array = new JSONArray();
    for (int i = 0; i < l.size(); i++) {
        JSONObject o = new JSONObject();
        //            o.put(key1, dateFormatter(l.get(i).getKey()));
        o.put(key1, l.get(i).getKey());
        o.put(key2, l.get(i).getValue());
        array.put(o);
    }
    return array.toString();
}

From source file:org.wso2.ei.bpmn.analytics.core.utils.BPMNAnalyticsCoreUtils.java

/**
 * Get sorted list (sort by long type keys)
 *
 * @param table is a hash table to keep the result as key-value pairs
 * @param key1  is the name for the first value of the JSON object
 * @param key2  is the name for the second value for the JSON object
 * @return a sorted list as a JSON array string
 * @throws JSONException//from   w  w  w  .  ja  v  a  2s .  co  m
 */
public static String getLongKeySortedList(Hashtable<Long, Integer> table, String key1, String key2)
        throws JSONException {
    ArrayList<Map.Entry<Long, Integer>> l = new ArrayList(table.entrySet());
    Collections.sort(l, new Comparator<Map.Entry<Long, Integer>>() {
        public int compare(Map.Entry<Long, Integer> o1, Map.Entry<Long, Integer> o2) {
            return o1.getKey().compareTo(o2.getKey());
        }
    });
    Collections.reverse(l);
    JSONArray array = new JSONArray();
    for (int i = 0; i < l.size(); i++) {
        JSONObject o = new JSONObject();
        o.put(key1, l.get(i).getKey());
        o.put(key2, l.get(i).getValue());
        array.put(o);
    }
    return array.toString();
}

From source file:org.wso2.carbon.pc.analytics.core.generic.utils.AnalyticsUtils.java

/**
 * Get sorted list (sort by int type values)
 *
 * @param table is a hash table to keep the result as key-value pairs
 * @param key1  is the name for the first value of the JSON object
 * @param key2  is the name for the second value for the JSON object
 * @param order is to get the top or bottom results
 * @param count is to limit the number of results
 * @return a sorted list as a JSON array string
 * @throws JSONException/*from w  w w .jav a  2s  . co m*/
 */
public static String getIntegerValueSortedList(Hashtable<String, Integer> table, String key1, String key2,
        String order, int count) throws JSONException {
    ArrayList<Map.Entry<String, Integer>> l = new ArrayList(table.entrySet());
    Collections.sort(l, new Comparator<Map.Entry<String, Integer>>() {
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            return o1.getValue().compareTo(o2.getValue());
        }
    });
    JSONArray array = new JSONArray();
    for (int i = 0; i < l.size(); i++) {
        JSONObject o = new JSONObject();
        o.put(key1, l.get(i).getKey());
        o.put(key2, l.get(i).getValue());
        array.put(o);
    }

    //if count exceeds the array length, then assign the array length to the count variable
    if (count > array.length()) {
        count = array.length();
    }

    JSONArray arrayPortion = new JSONArray();
    if (order.equalsIgnoreCase(AnalyticsConstants.TOP)) {
        for (int i = array.length() - count; i < array.length(); i++) {
            arrayPortion.put(array.get(i));
        }
    } else if (order.equalsIgnoreCase(AnalyticsConstants.BOTTOM)) {
        for (int i = 0; i < count; i++) {
            arrayPortion.put(array.get(i));
        }
    }
    return arrayPortion.toString();
}