public static TreeMap<Integer, Integer> integerDivided(int sum, int numPieces) { int base = sum / numPieces; int numInc = sum % numPieces; int numBases = numPieces - numInc; TreeMap<Integer, Integer> ret = new TreeMap<Integer, Integer>(); ret.put(base, numBases); if (numInc != 0) { ret.put(base + 1, numInc); } return ret; }
HashMap<String, Double> map = new HashMap<String, Double>(); ValueComparator bvc = new ValueComparator(map); TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc); map.put("D", 67.3); System.out.println("unsorted map: " + map); sorted_map.putAll(map); System.out.println("results: " + sorted_map); if (base.get(a) >= base.get(b)) { return -1; } else {
private String propertiesToString(Map<String, String> props, List<String> exclude) { String prop_string = ""; if (!props.isEmpty()) { Map<String, String> properties = new TreeMap<String, String>(props); List<String> realProps = new ArrayList<String>(); for (String key : properties.keySet()) { if (properties.get(key) != null && (exclude == null || !exclude.contains(key))) { realProps.add(" '" + key + "'='" + HiveStringUtils.escapeHiveCommand(properties.get(key)) + "'"); } } prop_string += StringUtils.join(realProps, ", \n"); } return prop_string; }
private Map<String, Capacity> getTierCapacityInternal() { SortedMap<String, Capacity> tierCapacity = new TreeMap<>(getTierAliasComparator()); Map<String, Long> capacityBytesOnTiers = mStoreMeta.getCapacityBytesOnTiers(); Map<String, Long> usedBytesOnTiers = mStoreMeta.getUsedBytesOnTiers(); for (Map.Entry<String, Long> entry : capacityBytesOnTiers.entrySet()) { tierCapacity.put(entry.getKey(), new Capacity().setTotal(entry.getValue()).setUsed(usedBytesOnTiers.get(entry.getKey()))); } return tierCapacity; }
private void printRow(TRowResult rowResult) { // copy values into a TreeMap to get them in sorted order TreeMap<String, TCell> sorted = new TreeMap<>(); for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) { sorted.put(utf8(column.getKey().array()), column.getValue()); } StringBuilder rowStr = new StringBuilder(); for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) { rowStr.append(entry.getKey()); rowStr.append(" => "); rowStr.append(utf8(entry.getValue().value.array())); rowStr.append("; "); } System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr); }
Map<String, Object> map = new TreeMap<String, Object>(); /* Add entries to the map in any order. */ ... /* Now, iterate over the map's contents, sorted by key. */ for (Map.Entry<String, ?> entry : map.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); }
Map<String, String> map = new HashMap<String, String>(); Map<String, String> treeMap = new TreeMap<String, String>(map); for (String str : treeMap.keySet()) { System.out.println(str); }
@Override public void prepare(Map<String, Object> conf) { toleranceCount = ObjectReader.getInt(conf.get(DaemonConfig.BLACKLIST_SCHEDULER_TOLERANCE_COUNT), DEFAULT_BLACKLIST_SCHEDULER_TOLERANCE_COUNT); resumeTime = ObjectReader.getInt(conf.get(DaemonConfig.BLACKLIST_SCHEDULER_RESUME_TIME), DEFAULT_BLACKLIST_SCHEDULER_RESUME_TIME); String reporterClassName = ObjectReader.getString(conf.get(DaemonConfig.BLACKLIST_SCHEDULER_REPORTER), LogReporter.class.getName()); reporter = (IReporter) initializeInstance(reporterClassName, "blacklist reporter"); nimbusMonitorFreqSecs = ObjectReader.getInt(conf.get(DaemonConfig.NIMBUS_MONITOR_FREQ_SECS)); blacklist = new TreeMap<>(); }
@Override protected Map<Integer, TypeDefinition> resolveInitializationTypes(ArgumentHandler argumentHandler) { SortedMap<Integer, TypeDefinition> namedTypes = new TreeMap<Integer, TypeDefinition>(); for (Map.Entry<String, TypeDefinition> entry : this.namedTypes.entrySet()) { namedTypes.put(argumentHandler.named(entry.getKey()), entry.getValue()); } return namedTypes; }
@Override public Map<String, Double> computeScore(String outerSentence) { TreeMap<String, Double> result = new TreeMap<String, Double>(Collections.reverseOrder()); T keyOuter = generateKey(outerSentence); if (keyOuter == null) return result; for (Map.Entry<T, Set<String>> entry : storage.entrySet()) { T key = entry.getKey(); Double score = keyOuter.similarity(key); for (String sentence : entry.getValue()) { result.put(sentence, score); } } return result; }
/** * 克隆一个状态<br> * Constructs an MDAGNode possessing the same accept state status and outgoing transitions as another. * @param node the MDAGNode possessing the accept state status and * outgoing transitions that the to-be-created MDAGNode is to take on */ private MDAGNode(MDAGNode node) { isAcceptNode = node.isAcceptNode; outgoingTransitionTreeMap = new TreeMap<Character, MDAGNode>(node.outgoingTransitionTreeMap); //Loop through the nodes in this node's outgoing _transition set, incrementing the number of //incoming transitions of each by 1 (to account for this newly created node's outgoing transitions) for(Entry<Character, MDAGNode> transitionKeyValuePair : outgoingTransitionTreeMap.entrySet()) transitionKeyValuePair.getValue().incomingTransitionCount++; ///// }
Map<Float,String> mySortedMap = new TreeMap<Float,MyObject>(); // Put some values in it mySortedMap.put(1.0f,"One"); mySortedMap.put(0.0f,"Zero"); mySortedMap.put(3.0f,"Three"); // Iterate through it and it'll be in order! for(Map.Entry<Float,String> entry : mySortedMap.entrySet()) { System.out.println(entry.getValue()); } // outputs Zero One Three
/** * 分割Map,其中旧map直接被改变 * @param src * @param rate * @return */ public static Map<String, String[]> splitMap(Map<String, String[]> src, double rate) { assert 0 <= rate && rate <= 1; Map<String, String[]> output = new TreeMap<String, String[]>(); for (Map.Entry<String, String[]> entry : src.entrySet()) { String[][] array = spiltArray(entry.getValue(), rate); output.put(entry.getKey(), array[0]); entry.setValue(array[1]); } return output; } }