List of usage examples for java.util HashMap entrySet
Set entrySet
To view the source code for java.util HashMap entrySet.
Click Source Link
From source file:Main.java
/** * HashMap Operations//from w w w . ja v a 2s . co m * */ public static void printHash(HashMap<String, Integer> hashMap) { System.out.println("Print HashMap"); Set<Map.Entry<String, Integer>> s = hashMap.entrySet(); Iterator<Map.Entry<String, Integer>> it = s.iterator(); while (it.hasNext()) { Map.Entry<String, Integer> m = (Map.Entry<String, Integer>) it.next(); System.out.println(m.getKey() + "\t" + m.getValue()); } }
From source file:Main.java
public static ArrayList<String> getHashMap(HashMap<String, String> hm) { ArrayList<String> a = new ArrayList<String>(); Set<Map.Entry<String, String>> s = hm.entrySet(); Iterator<Map.Entry<String, String>> it = s.iterator(); while (it.hasNext()) { Map.Entry<String, String> m = (Map.Entry<String, String>) it.next(); a.add(m.getKey() + "\t" + m.getValue()); }/*w w w. ja v a 2 s. c om*/ return a; }
From source file:Main.java
public static ArrayList<String> getHashMap2(HashMap<String, Integer> hm) { ArrayList<String> a = new ArrayList<String>(); Set<Map.Entry<String, Integer>> s = hm.entrySet(); Iterator<Map.Entry<String, Integer>> it = s.iterator(); while (it.hasNext()) { Map.Entry<String, Integer> m = (Map.Entry<String, Integer>) it.next(); a.add(m.getKey() + "\t" + m.getValue()); }/*from w w w . j a v a 2s. c o m*/ return a; }
From source file:Main.java
public static void init(HashMap<Type, Object> typeAdapterHashMap) { GsonBuilder gsonBuilder = new GsonBuilder(); for (Map.Entry<Type, Object> entry : typeAdapterHashMap.entrySet()) { gsonBuilder.registerTypeAdapter(entry.getKey(), entry.getValue()); }// w w w .j ava 2 s . co m gson = gsonBuilder.create(); }
From source file:Main.java
public static double readHashTopValue(HashMap<String, Integer> scores, int k) { List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(scores.entrySet()); int count = 0; int value = 0; double res = 0; for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); count < k && it.hasNext();) { Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) it.next(); value = (Integer) entry.getValue(); res += (double) value * Math.log(2) / Math.log(count + 2); // res += (Integer) entry.getValue(); count++;/*from www .j a v a 2s .c o m*/ } return res; }
From source file:Main.java
public static void mergeParam(HashMap<String, Object> params, List<NameValuePair> valuePair) { if (params != null) { for (HashMap.Entry<String, Object> entry : params.entrySet()) { String key = entry.getKey(); String value = String.valueOf(entry.getValue()); valuePair.add(new BasicNameValuePair(key, value)); }/*from www .j a va 2 s . com*/ } }
From source file:Main.java
public static void vectorToXML222(String xmlFile, String xpath, String parentNodeName, Vector<HashMap> vector) { File file = new File(xmlFile); try {//from w ww . jav a2 s . c o m DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document; Element rootNode; if (file.exists()) { document = documentBuilder.parse(new File(xmlFile)); rootNode = document.getDocumentElement(); } else { document = documentBuilder.newDocument(); rootNode = document.createElement(xpath); document.appendChild(rootNode); } for (int x = 0; x < vector.size(); x++) { Element parentNode = document.createElement(parentNodeName); rootNode.appendChild(parentNode); HashMap hashmap = vector.get(x); Set set = hashmap.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); // System.out.println("key=" + // me.getKey().toString()); Element em = document.createElement(me.getKey().toString()); em.appendChild(document.createTextNode(me.getValue().toString())); parentNode.appendChild(em); // System.out.println("write " + // me.getKey().toString() + // "=" // + me.getValue().toString()); } } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); FileOutputStream fo = new FileOutputStream(xmlFile); StreamResult result = new StreamResult(fo); transformer.transform(source, result); } catch (Exception ex) { file.delete(); ex.printStackTrace(); } }
From source file:Main.java
@SuppressWarnings("unchecked") public static HashMap<?, ?> sortByValue(HashMap<?, ?> map, final int flag) { // flag = 0 decreasing order otherwise increasing List list = new LinkedList(map.entrySet()); Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { if (flag == 0) return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue()); else/*from w w w . ja v a 2 s . c om*/ return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()); } }); HashMap result = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
public static final String mapToQueryString(HashMap<String, String> queryString) { StringBuilder sb = new StringBuilder(); try {// ww w. jav a 2 s . co m for (HashMap.Entry<String, String> e : queryString.entrySet()) { if (sb.length() > 0) { sb.append('&'); } sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append('=') .append(URLEncoder.encode(e.getValue(), "UTF-8")); } } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } return sb.toString(); }
From source file:frequencyassignment.charts.Charts.java
public static void displayScatterPlot(String name, HashMap<Double, Double> values) { XYSeries xyData = new XYSeries(name); for (Map.Entry<Double, Double> entry : values.entrySet()) { xyData.add(entry.getKey(), entry.getValue()); }//from www . j a v a 2 s .co m XYSeriesCollection xySeriesCollection = new XYSeriesCollection(xyData); JFreeChart chart = ChartFactory.createScatterPlot(name, "X", "Y", (XYDataset) xySeriesCollection); ChartFrame frame = new ChartFrame(name, chart); frame.pack(); frame.setVisible(true); }