List of usage examples for java.util Map entrySet
Set<Map.Entry<K, V>> entrySet();
From source file:io.amira.zen.http.ZenHTTP.java
private static Header[] _parseHeaders(Map<String, String> headers) { Header[] rv = new Header[headers.size()]; Iterator iter = headers.entrySet().iterator(); int i = 0;//from ww w.j a v a 2s.c o m while (iter.hasNext()) { Map.Entry pairs = (Map.Entry) iter.next(); rv[i] = new BasicHeader(pairs.getKey().toString(), pairs.getValue().toString()); i++; } return rv; }
From source file:Main.java
public static double compareText(String text, String source) { Map<String, Integer> sourceMap = parseTextToWords(source); Map<String, Integer> textMap = parseTextToWords(text); int total = 0; int notFound = 0; for (Map.Entry<String, Integer> textEntry : textMap.entrySet()) { String word = textEntry.getKey(); int count = textEntry.getValue(); Integer sourceCount = sourceMap.get(word); int sCount = sourceCount != null ? sourceCount.intValue() : 0; total += count;// w w w . ja v a2 s . c o m notFound += (count > sCount) ? count - sCount : 0; } return (double) (total - notFound) / (double) total; }
From source file:Main.java
/** * Sort a map according to its values in ascending order when asc=true and i descending order otherwise. * @param unsortedMap //from w w w. jav a2 s .c o m * @param asc * @return */ public static Map<String, Integer> sortUsingComparator(Map<String, Integer> unsortedMap, final boolean asc) { // Convert Map to List List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortedMap.entrySet()); // Sort a list using custom comparator, to compare the Map values Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { if (asc) { return (o1.getValue()).compareTo(o2.getValue()); } else { return -1 * (o1.getValue()).compareTo(o2.getValue()); } } }); // Convert the sorted map back to a Map Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) { Map.Entry<String, Integer> entry = it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; }
From source file:com.primitive.applicationmanager.BaseApplicationManager.java
/** * Map<String, String>JSONObject??????? * @param params//from w w w. j av a 2s . c o m * @return JSONObject * @throws JSONException */ private static JSONObject getJsonObjectFromMap(final Map<String, String> params) throws JSONException { final Iterator<Entry<String, String>> iter = params.entrySet().iterator(); final JSONObject holder = new JSONObject(); while (iter.hasNext()) { final Map.Entry<String, String> pairs = (Map.Entry<String, String>) iter.next(); final String key = (String) pairs.getKey(); final String value = (String) pairs.getValue(); holder.put(key, value); } return holder; }
From source file:com.drtshock.playervaults.vaultmanagement.Serialization.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static ConfigurationSerializable deserialize(Map<String, Object> map) { for (Entry<String, Object> entry : map.entrySet()) { if (entry.getValue() instanceof Map && ((Map) entry.getValue()).containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) { entry.setValue(deserialize((Map) entry.getValue())); } else if (entry.getValue() instanceof Iterable) { entry.setValue(convertIterable((Iterable) entry.getValue())); }/*from w w w.jav a2s .com*/ } return ConfigurationSerialization.deserializeObject(map); }
From source file:com.turt2live.xmail.api.Attachments.java
/** * Attempts to get an attachment from a JSON string. This will throw an IllegalArgumentException if * the json string is null or invalid.// ww w.j av a 2s . co m * * @param json the JSON string * * @return the attachment, or an UnknownAttachment if no match */ @SuppressWarnings("unchecked") public static Attachment getAttachment(String json) { if (json == null) { throw new IllegalArgumentException(); } JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { @Override public List<Object> creatArrayContainer() { return new LinkedList<Object>(); } @Override public Map<String, Object> createObjectContainer() { return new LinkedHashMap<String, Object>(); } }; Map<String, Object> map = new HashMap<String, Object>(); try { Map<?, ?> json1 = (Map<?, ?>) parser.parse(json, containerFactory); Iterator<?> iter = json1.entrySet().iterator(); // Type check while (iter.hasNext()) { Entry<?, ?> entry = (Entry<?, ?>) iter.next(); if (!(entry.getKey() instanceof String)) { throw new IllegalArgumentException("Not in <String, Object> format"); } } map = (Map<String, Object>) json1; } catch (ParseException e) { e.printStackTrace(); return null; } catch (IllegalArgumentException e) { e.printStackTrace(); return null; } XMail plugin = XMail.getInstance(); Attachment a = plugin.getMailServer().attemptAttachmentDecode(map); if (a != null) { return a; } a = plugin.getAttachmentHandlerRegistry().getAttachmentFromHandler(json); return a == null ? new UnknownAttachment(json) : a; }
From source file:Main.java
public static Map<? extends Object, ? extends Double> merge(Map<? extends Object, ? extends Double> map1, Map<? extends Object, ? extends Double> map2) { Map<Object, Double> result = new HashMap<Object, Double>(); Map<? extends Object, ? extends Double> biggerMap = map1.size() > map2.size() ? map1 : map2; Map<? extends Object, ? extends Double> smallerMap = map1.size() > map2.size() ? map2 : map1; for (Entry<? extends Object, ? extends Double> entry : smallerMap.entrySet()) { Object key = entry.getKey(); Double value = entry.getValue(); Double value2 = (Double) getValue(biggerMap, key, Double.class); value2 += value;/*from w w w .j a va2 s . c o m*/ result.put(key, value2); biggerMap.remove(key); } result.putAll(biggerMap); return sortByValueDesc(result); }
From source file:Main.java
public static String joinMap(Map<?, ?> map) { if (map.isEmpty()) { return ""; }//from ww w. j a v a 2s. c om StringBuilder stringBuilder = new StringBuilder(); boolean notFirst = false; for (Map.Entry<?, ?> entry : map.entrySet()) { if (notFirst) { stringBuilder.append(','); } stringBuilder.append(entry.getKey().toString()).append(':').append(entry.getValue().toString()); notFirst = true; } return stringBuilder.toString(); }
From source file:Main.java
public static String jointUrl(String url, Map<String, String> params) { if (params != null && params.size() > 0) { Uri uri = Uri.parse(url);/*from ww w .j ava 2 s. c o m*/ Uri.Builder b = uri.buildUpon(); for (Map.Entry<String, String> entry : params.entrySet()) { b.appendQueryParameter(entry.getKey(), entry.getValue()); } return b.build().toString(); } return url; }
From source file:Main.java
public static String mapToString(Map<?, ?> map) { if (map.isEmpty()) { return ""; }//from w w w. j av a2 s. c o m StringBuilder stringBuilder = new StringBuilder(); boolean notFirst = false; for (Map.Entry<?, ?> entry : map.entrySet()) { if (notFirst) { stringBuilder.append(','); } stringBuilder.append(entry.getKey().toString()).append(':').append(entry.getValue().toString()); notFirst = true; } return stringBuilder.toString(); }