List of usage examples for java.util Map entrySet
Set<Map.Entry<K, V>> entrySet();
From source file:Main.java
/** * Sets the Map as DOM attributes on the given Element. * <p>/* w ww. ja va2s . co m*/ * This implementation uses <code>element.setAttribute</code>. Therefore if the element already * has attributes, the new attributes are added amongst them. If attributes with the same name * already exist, they are overwritten. */ public static void setMapAsAttributes(Element element, Map<String, String> attributes) { if (attributes == null) { return; } for (Map.Entry<String, String> entry : attributes.entrySet()) { String value = entry.getValue(); if (value == null) { element.removeAttribute(entry.getKey()); continue; } element.setAttribute(entry.getKey(), value); } }
From source file:Main.java
/** Creates a single Map with fields from the passed in Map and all nested Maps (for Map and Collection of Map entry values) */ @SuppressWarnings("unchecked") public static Map flattenNestedMap(Map theMap) { if (theMap == null) return null; Map outMap = new LinkedHashMap(); for (Object entryObj : theMap.entrySet()) { Map.Entry entry = (Map.Entry) entryObj; Object value = entry.getValue(); if (value instanceof Map) { outMap.putAll(flattenNestedMap((Map) value)); } else if (value instanceof Collection) { for (Object colValue : (Collection) value) { if (colValue instanceof Map) outMap.putAll(flattenNestedMap((Map) colValue)); }// www. j a v a2 s . c o m } else { outMap.put(entry.getKey(), entry.getValue()); } } return outMap; }
From source file:Main.java
public static String urlencode(Map<String, ?> data) { StringBuilder sb = new StringBuilder(); for (Map.Entry<String, ?> i : data.entrySet()) { try {/*from www . jav a 2 s . com*/ sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return sb.toString(); }
From source file:Main.java
/** * Returns the <code>index</code>-th value in <code>object</code>, throwing * <code>IndexOutOfBoundsException</code> if there is no such element or * <code>IllegalArgumentException</code> if <code>object</code> is not an * instance of one of the supported types. * <p/>/*w ww . j ava 2 s .c o m*/ * The supported types, and associated semantics are: * <ul> * <li> Map -- the value returned is the <code>Map.Entry</code> in position * <code>index</code> in the map's <code>entrySet</code> iterator, * if there is such an entry.</li> * <li> List -- this method is equivalent to the list's get method.</li> * <li> Array -- the <code>index</code>-th array entry is returned, * if there is such an entry; otherwise an <code>IndexOutOfBoundsException</code> * is thrown.</li> * <li> Collection -- the value returned is the <code>index</code>-th object * returned by the collection's default iterator, if there is such an element.</li> * <li> Iterator or Enumeration -- the value returned is the * <code>index</code>-th object in the Iterator/Enumeration, if there * is such an element. The Iterator/Enumeration is advanced to * <code>index</code> (or to the end, if <code>index</code> exceeds the * number of entries) as a side effect of this method.</li> * </ul> * * @param object the object to get a value from * @param index the index to get * @return the object at the specified index * @throws IndexOutOfBoundsException if the index is invalid * @throws IllegalArgumentException if the object type is invalid */ public static Object get(Object object, int index) { if (index < 0) throw new IndexOutOfBoundsException("Index cannot be negative: " + index); if (object instanceof Map) { Map map = (Map) object; Iterator iterator = map.entrySet().iterator(); return get(iterator, index); } else if (object instanceof List) { return ((List) object).get(index); } else if (object instanceof Object[]) { return ((Object[]) object)[index]; } else if (object instanceof Iterator) { Iterator it = (Iterator) object; while (it.hasNext()) { index--; if (index == -1) return it.next(); else it.next(); } throw new IndexOutOfBoundsException("Entry does not exist: " + index); } else if (object instanceof Collection) { Iterator iterator = ((Collection) object).iterator(); return get(iterator, index); } else if (object instanceof Enumeration) { Enumeration it = (Enumeration) object; while (it.hasMoreElements()) { index--; if (index == -1) return it.nextElement(); else it.nextElement(); } throw new IndexOutOfBoundsException("Entry does not exist: " + index); } else if (object == null) { throw new IllegalArgumentException("Unsupported object type: null"); } else { try { return Array.get(object, index); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } }
From source file:Main.java
public static String encode(Map<String, String> params, String encoding) { StringBuilder encodedParams = new StringBuilder(); try {//from w w w . j a v a 2 s .c o m Set uee = params.entrySet(); int size = uee.size(); int index = 0; Iterator iterator = uee.iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); encodedParams.append(URLEncoder.encode((String) entry.getKey(), encoding)); encodedParams.append('='); encodedParams.append(URLEncoder.encode((String) entry.getValue(), encoding)); ++index; if (index < size) { encodedParams.append('&'); } } return encodedParams.toString(); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Encoding not supported: " + encoding, e); } }
From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.sampling.IOHelper.java
/** * Sorts map by value (http://stackoverflow.com/a/2581754) * * @param map map/* w ww .ja va 2s. co m*/ * @param <K> key * @param <V> value * @return sorted map by value */ public static <K, V extends Comparable<? super V>> LinkedHashMap<K, V> sortByValue(Map<K, V> map, final boolean asc) { List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<K, V>>() { @Override public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { if (asc) { return (o1.getValue()).compareTo(o2.getValue()); } else { return (o2.getValue()).compareTo(o1.getValue()); } } }); LinkedHashMap<K, V> result = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
/** * Creates a map of tag name to clean header XML by running a substitution * regex on each entry of tag to dirty header XML. * * @param dirtyXmlMap a map of tag name to dirty header XML * @return a map of tag name to clean header XML *///from www .j av a 2s .c o m private static Map<String, String> createTagToCleanXmlMap(Map<String, String> dirtyXmlMap) { Map<String, String> cleanXmlMap = new HashMap<String, String>(); for (Entry<String, String> sensitiveXml : dirtyXmlMap.entrySet()) { Pattern p = Pattern.compile( String.format(SENSITIVE_REGEX, sensitiveXml.getKey(), sensitiveXml.getKey()), Pattern.CASE_INSENSITIVE | Pattern.DOTALL); Matcher m = p.matcher(sensitiveXml.getValue()); if (m.matches()) { cleanXmlMap.put(sensitiveXml.getKey(), m.replaceFirst("$1******$2")); } } return cleanXmlMap; }
From source file:Main.java
public static String mapToString(Map<? extends Object, ? extends Object> result) { StringBuilder builder = new StringBuilder(); for (Entry<? extends Object, ? extends Object> productEntry : result.entrySet()) { builder.append(productEntry.getKey() + "\t" + productEntry.getValue() + "\n"); }/*from w w w.ja v a 2 s. com*/ return builder.toString(); }
From source file:com.jaspersoft.jasperserver.ps.OAuth.JSONUtils.java
public static String getMapEntry(Map<String, String> map, String searchstring) { for (Map.Entry<String, String> entry : map.entrySet()) { if (entry.getKey().toLowerCase().contains(searchstring.toLowerCase())) { return entry.getValue(); }/*from w ww . j a v a 2 s . c om*/ } return null; }
From source file:de.christianseipl.utilities.maputils.MapPrinting.java
public static <K, V> void printOneDimensionalMap(String _header, Map<K, V> _map) { System.out.println(_header);/*from ww w. j av a2s. c o m*/ for (Entry<K, V> entry : _map.entrySet()) { System.out.println(String.format("%20s\t%20s", entry.getKey().toString(), entry.getValue().toString())); } System.out.println(); }