List of usage examples for java.util TreeMap putAll
public void putAll(Map<? extends K, ? extends V> map)
From source file:org.polymap.kaps.ui.form.RichtwertzoneProvider.java
/** * // w ww . j a v a 2 s . c om * @param gemeindeComposite * @param date * @return */ public static SortedMap<String, Object> findFor(GemeindeComposite gemeinde, Date date) { // long start = System.currentTimeMillis(); // log.info( "findFor: " + gemeinde.schl().get() + ", " + date.getDate() ); if (gemeinde == null) { throw new IllegalArgumentException("gemeinde must not be null"); } if (date == null) { throw new IllegalArgumentException("date must not be null"); } Map<String, RichtwertzoneZeitraumComposite> zonen = new HashMap<String, RichtwertzoneZeitraumComposite>(); Iterable<RichtwertzoneComposite> iterable = RichtwertzoneComposite.Mixin.findZoneIn(gemeinde); for (RichtwertzoneComposite zone : iterable) { String prefix = zone.schl().get(); // find zeitraum RichtwertzoneZeitraumComposite zeitraum = RichtwertzoneZeitraumComposite.Mixin.findZeitraumFor(zone, date); if (zeitraum != null && zeitraum.gueltigAb().get() != null) { zonen.put(prefix + " - " + zone.name().get() + " (" + KapsRepository.SHORT_DATE.format(zeitraum.gueltigAb().get()) + ")", zeitraum); } } TreeMap<String, Object> sorted = new TreeMap<String, Object>(new RWZComparator(zonen)); sorted.putAll(zonen); // log.info( "findFor: " + gemeinde.schl().get() + " needed " + (System.currentTimeMillis() - start) + "ms" ); return sorted.descendingMap(); }
From source file:org.polymap.kaps.ui.form.RichtwertzoneProvider.java
private static Cache<GemeindeComposite, SortedMap<String, Object>> getCache() { if (gemeindeCache == null) { gemeindeCache = CacheBuilder.newBuilder().weakKeys().maximumSize(10000) .expireAfterWrite(1, TimeUnit.MINUTES) .build(new CacheLoader<GemeindeComposite, SortedMap<String, Object>>() { public SortedMap<String, Object> load(GemeindeComposite gemeinde) { Map<String, RichtwertzoneComposite> zonen = new HashMap<String, RichtwertzoneComposite>(); Iterable<RichtwertzoneComposite> iterable = RichtwertzoneComposite.Mixin .findZoneIn(gemeinde); for (RichtwertzoneComposite zone : iterable) { String prefix = zone.schl().get(); zonen.put(prefix + " - " + zone.name().get(), zone); }//w w w . j a v a2 s . c om TreeMap<String, Object> sorted = new TreeMap<String, Object>(new RWComparator(zonen)); sorted.putAll(zonen); SortedMap<String, Object> ret = sorted.descendingMap(); return ret; } }); } return gemeindeCache; }
From source file:com.powers.wsexplorer.gui.GUIUtil.java
public static Listener createTextSortListener(final Table table, final Map<String, String> map, final boolean sortOnValues) { Listener sortListener = new Listener() { public void handleEvent(Event e) { final int direction = table.getSortDirection(); Collator collator = Collator.getInstance(Locale.getDefault()); TableColumn column = (TableColumn) e.widget; Set<String> keys = null; List<String> l = new LinkedList<String>(); Iterator<String> itr = null; if (sortOnValues) { TreeMap<String, String> tm = new TreeMap<String, String>( new MapValueComparator(map, direction == SWT.DOWN)); tm.putAll(map); if (direction == SWT.DOWN) { table.setSortDirection(SWT.UP); } else { table.setSortDirection(SWT.DOWN); }/*from w w w.j a va2 s.c o m*/ itr = tm.keySet().iterator(); } else { keys = map.keySet(); l.addAll(keys); Collections.sort(l, collator); if (direction == SWT.DOWN) { Collections.reverse(l); table.setSortDirection(SWT.UP); } else { table.setSortDirection(SWT.DOWN); } itr = l.iterator(); } // remove all table data table.removeAll(); String key = null; String value = null; while (itr.hasNext()) { key = itr.next(); if (StringUtils.isNotBlank(key)) { TableItem ti = new TableItem(table, SWT.BORDER); ti.setText(0, key); value = map.get(key); if (StringUtils.isNotBlank(value)) { ti.setText(1, value); } } } table.setSortColumn(column); } }; return sortListener; }
From source file:com.insightml.utils.Collections.java
public static <T, N extends Comparable<N>> Map<T, N> sortAsc(final Map<T, N> map) { final TreeMap<T, N> sorted = new TreeMap<>((o1, o2) -> { if (o1 == o2) { return 0; }/* w w w.j av a2 s. co m*/ final N v1 = map.get(o1); final N v2 = map.get(o2); final int comp = v1.compareTo(v2); Check.state(comp != 0); return comp; }); sorted.putAll(map); return sorted; }
From source file:com.alibaba.jstorm.ui.UIUtils.java
public static List<TableData> getWorkerMetricsTable(Map<String, MetricInfo> metrics, Integer window, Map<String, String> paramMap) { List<TableData> ret = new ArrayList<TableData>(); TableData table = new TableData(); ret.add(table);//from w w w .j av a2 s. c o m List<String> headers = table.getHeaders(); List<Map<String, ColumnData>> lines = table.getLines(); table.setName("Worker " + UIDef.METRICS); List<String> keys = getSortedKeys(UIUtils.getKeys(metrics.values())); headers.add(UIDef.PORT); headers.add(MetricDef.NETTY); headers.addAll(keys); TreeMap<String, MetricInfo> tmpMap = new TreeMap<String, MetricInfo>(); tmpMap.putAll(metrics); Map<String, MetricInfo> showMap = new TreeMap<String, MetricInfo>(); long pos = JStormUtils.parseLong(paramMap.get(UIDef.POS), 0); long index = 0; for (Entry<String, MetricInfo> entry : tmpMap.entrySet()) { if (index < pos) { index++; } else if (pos <= index && index < pos + UIUtils.ONE_TABLE_PAGE_SIZE) { showMap.put(entry.getKey(), entry.getValue()); index++; } else { break; } } for (Entry<String, MetricInfo> entry : showMap.entrySet()) { Map<String, ColumnData> line = new HashMap<String, ColumnData>(); lines.add(line); String slot = entry.getKey(); MetricInfo metric = entry.getValue(); ColumnData slotColumn = new ColumnData(); slotColumn.addText(slot); line.put(UIDef.PORT, slotColumn); ColumnData nettyColumn = new ColumnData(); line.put(MetricDef.NETTY, nettyColumn); if (StringUtils.isBlank(paramMap.get(UIDef.TOPOLOGY))) { nettyColumn.addText(MetricDef.NETTY); } else { LinkData linkData = new LinkData(); nettyColumn.addLinkData(linkData); linkData.setUrl(UIDef.LINK_WINDOW_TABLE); linkData.setText(MetricDef.NETTY); linkData.addParam(UIDef.CLUSTER, paramMap.get(UIDef.CLUSTER)); linkData.addParam(UIDef.PAGE_TYPE, UIDef.PAGE_TYPE_NETTY); linkData.addParam(UIDef.TOPOLOGY, paramMap.get(UIDef.TOPOLOGY)); } for (String key : keys) { String value = UIUtils.getValue(metric, key, window); ColumnData valueColumn = new ColumnData(); valueColumn.addText(value); line.put(key, valueColumn); } } return ret; }
From source file:org.etudes.util.HtmlHelper.java
/** * Clean some user entered HTML. Assures well formed HTML. Assures all anchor tags have target=_blank. Assures all tag attributes are in determined (alpha) order. * //from www. ja v a 2 s .co m * @param source * The source HTML * @param fragment * if true, return a fragment of html, else return a complete html document. * @return The cleaned up HTML. */ protected static String cleanWithHtmlCleaner(String source, boolean fragment) { if (source == null) return null; // http://htmlcleaner.sourceforge.net // http://mvnrepository.com/artifact/net.sourceforge.htmlcleaner/htmlcleaner/2.2 HtmlCleaner cleaner = new HtmlCleaner(); cleaner.getProperties().setOmitXmlDeclaration(true); cleaner.getProperties().setTranslateSpecialEntities(false); cleaner.getProperties().setOmitComments(true); TagNode node = cleaner.clean(source); node.traverse(new TagNodeVisitor() { public boolean visit(TagNode tagNode, HtmlNode htmlNode) { if (htmlNode instanceof TagNode) { TagNode tag = (TagNode) htmlNode; String tagName = tag.getName(); if ("a".equals(tagName)) { // if there is a href attribute, and it does not begin with # (a link to an anchor), add a target attribute String href = tag.getAttributeByName("href"); if ((href != null) && (!href.startsWith("#"))) { tag.setAttribute("target", "_blank"); } } // order the tag attributes Map<String, String> attributes = tag.getAttributes(); TreeMap<String, String> attr = new TreeMap<String, String>(); attr.putAll(attributes); for (String key : attr.keySet()) { tag.removeAttribute(key); } for (String key : attr.keySet()) { String value = attr.get(key); // fix up style attributes if ("style".equalsIgnoreCase(key)) { value = canonicalStyleAttribute(value); } tag.setAttribute(key, value); } } return true; } }); String rv = ""; final HtmlSerializer serializer = new CompactHtmlSerializer(cleaner.getProperties()); try { TagNode nodeToGet = node; boolean omitEnvelope = false; if (fragment) { Object[] nodes = node.evaluateXPath("//body"); if (nodes.length == 1) { nodeToGet = (TagNode) (nodes[0]); omitEnvelope = true; } } rv = serializer.getAsString(nodeToGet, "UTF-8", omitEnvelope); } catch (XPatherException e) { } catch (IOException e) { } return rv; }
From source file:nl.uva.sne.commons.SemanticUtils.java
public static Set<Term> tf_idf_Disambiguation(Set<Term> possibleTerms, Set<String> nGrams, String lemma, double confidence, boolean matchTitle) throws IOException, JWNLException, ParseException { List<List<String>> allDocs = new ArrayList<>(); Map<String, List<String>> docs = new HashMap<>(); if (possibleTerms == null || possibleTerms.size() < 10) { return null; }//from www . j av a 2 s .co m for (Term tv : possibleTerms) { Set<String> doc = SemanticUtils.getDocument(tv); allDocs.add(new ArrayList<>(doc)); docs.put(tv.getUID(), new ArrayList<>(doc)); } Set<String> contextDoc = new HashSet<>(); for (String s : nGrams) { if (s.contains("_")) { String[] parts = s.split("_"); for (String token : parts) { if (token.length() >= 1 && !token.contains(lemma)) { contextDoc.add(token); } } } else if (s.length() >= 1 && !s.contains(lemma)) { contextDoc.add(s); } } docs.put("context", new ArrayList<>(contextDoc)); Map<String, Map<String, Double>> featureVectors = new HashMap<>(); for (String k : docs.keySet()) { List<String> doc = docs.get(k); Map<String, Double> featureVector = new TreeMap<>(); for (String term : doc) { if (!featureVector.containsKey(term)) { double tfidf = tfIdf(doc, allDocs, term); featureVector.put(term, tfidf); } } featureVectors.put(k, featureVector); } Map<String, Double> contextVector = featureVectors.remove("context"); Map<String, Double> scoreMap = new HashMap<>(); for (String key : featureVectors.keySet()) { Double similarity = cosineSimilarity(contextVector, featureVectors.get(key)); for (Term t : possibleTerms) { if (t.getUID().equals(key)) { String stemTitle = stem(t.getLemma().toLowerCase()); String stemLema = stem(lemma); // List<String> subTokens = new ArrayList<>(); // if (!t.getLemma().toLowerCase().startsWith("(") && t.getLemma().toLowerCase().contains("(") && t.getLemma().toLowerCase().contains(")")) { // int index1 = t.getLemma().toLowerCase().indexOf("(") + 1; // int index2 = t.getLemma().toLowerCase().indexOf(")"); // String sub = t.getLemma().toLowerCase().substring(index1, index2); // subTokens.addAll(tokenize(sub, true)); // } // // List<String> nTokens = new ArrayList<>(); // for (String s : nGrams) { // if (s.contains("_")) { // String[] parts = s.split("_"); // for (String token : parts) { // nTokens.addAll(tokenize(token, true)); // } // } else { // nTokens.addAll(tokenize(s, true)); // } // } // if (t.getCategories() != null) { // for (String s : t.getCategories()) { // if (s != null && s.contains("_")) { // String[] parts = s.split("_"); // for (String token : parts) { // subTokens.addAll(tokenize(token, true)); // } // } else if (s != null) { // subTokens.addAll(tokenize(s, true)); // } // } // } //// System.err.println(t.getGlosses()); // Set<String> intersection = new HashSet<>(nTokens); // intersection.retainAll(subTokens); // if (intersection.isEmpty()) { // similarity -= 0.1; // } int dist = edu.stanford.nlp.util.StringUtils.editDistance(stemTitle, stemLema); similarity = similarity - (dist * 0.05); t.setConfidence(similarity); } } scoreMap.put(key, similarity); } if (scoreMap.isEmpty()) { return null; } ValueComparator bvc = new ValueComparator(scoreMap); TreeMap<String, Double> sorted_map = new TreeMap(bvc); sorted_map.putAll(scoreMap); // System.err.println(sorted_map); Iterator<String> it = sorted_map.keySet().iterator(); String winner = it.next(); Double s1 = scoreMap.get(winner); if (s1 < confidence) { return null; } Set<Term> terms = new HashSet<>(); for (Term t : possibleTerms) { if (t.getUID().equals(winner)) { terms.add(t); } } if (!terms.isEmpty()) { return terms; } else { Logger.getLogger(SemanticUtils.class.getName()).log(Level.INFO, "No winner"); return null; } }
From source file:edu.jhuapl.openessence.web.util.ControllerUtils.java
public static Map<String, ChartData> getSortedByChartDataMap(LinkedHashMap<String, ChartData> unsortedMap) { ChartDataComparator chartDataCompare = new ChartDataComparator(unsortedMap); TreeMap<String, ChartData> sortedMap = new TreeMap<String, ChartData>(chartDataCompare); sortedMap.putAll(unsortedMap); LinkedHashMap<String, ChartData> map = new LinkedHashMap<String, ChartData>(sortedMap); return map;/*from ww w . j a v a 2 s . c om*/ }
From source file:edu.jhuapl.openessence.web.util.ControllerUtils.java
/** * Sorts a map by value./* www .j a va2 s .c om*/ */ public static Map<String, Double> getSortedByValueMap(LinkedHashMap<String, Double> unsortedMap) { //TODO use this? /* * Collections.sort(dataList, new Comparator<Pair<String, Double>>() { @Override public int compare(Pair<String, Double> o1, Pair<String, Double> o2) { return (int) Math.ceil(o2.getValue() - o1.getValue()); } }); * */ ValueComparator dubCompare = new ValueComparator(unsortedMap); TreeMap<String, Double> sortedMap = new TreeMap<String, Double>(dubCompare); sortedMap.putAll(unsortedMap); LinkedHashMap<String, Double> map = new LinkedHashMap<String, Double>(sortedMap); return map; }
From source file:com.linkedin.helix.TestHelper.java
public static void printCache(Map<String, ZNode> cache) { System.out.println("START:Print cache"); TreeMap<String, ZNode> map = new TreeMap<String, ZNode>(); map.putAll(cache); for (String key : map.keySet()) { ZNode node = map.get(key);/*from w ww . ja va 2 s .c o m*/ TreeSet<String> childSet = new TreeSet<String>(); childSet.addAll(node.getChildSet()); System.out.print(key + "=" + node.getData() + ", " + childSet + ", " + (node.getStat() == null ? "null\n" : node.getStat())); } System.out.println("END:Print cache"); }