Java Code Examples for java.util.HashMap#replace()
The following examples show how to use
java.util.HashMap#replace() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LeetCode169.java From Project with Apache License 2.0 | 6 votes |
public int majorityElement(int[] nums) { if (nums.length == 1) { return nums[0]; } HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { // 如果有该元素,则数目加一 if (map.containsKey(nums[i])) { int oldValue = map.get(nums[i]); int newValue = oldValue + 1; // 如果数目到一半了,返回 if (newValue > nums.length >> 1) { return nums[i]; } else { map.replace(nums[i], oldValue, newValue); } // 如果没有元素,加入该元素,出现次数设置为 1 } else { map.put(nums[i], 1); } } return 0; }
Example 2
Source File: Offer39.java From Project with Apache License 2.0 | 6 votes |
public int majorityElement(int[] nums) { if (nums.length == 1) { return nums[0]; } HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i])) { int oldValue = map.get(nums[i]); int newValue = oldValue + 1; if (newValue > nums.length >>> 1) { return nums[i]; } else { map.replace(nums[i], oldValue, newValue); } } else { map.put(nums[i], 1); } } return 0; }
Example 3
Source File: Offer03.java From Project with Apache License 2.0 | 6 votes |
public int findRepeatNumber(int[] nums) { if (nums.length == 0 || nums.length == 1) { return -1; } HashMap<Integer, Integer> resMap = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (resMap.containsKey(nums[i])) { int oldValue = resMap.get(nums[i]); int newValue = oldValue + 1; if (newValue >= 2) { return nums[i]; } else { resMap.replace(nums[i], oldValue, newValue); } } else { resMap.put(nums[i], 1); } } return -1; }
Example 4
Source File: LeetCode169.java From Project with Apache License 2.0 | 6 votes |
public int majorityElement(int[] nums) { if (nums.length == 1) { return nums[0]; } HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { // 如果有该元素,则数目加一 if (map.containsKey(nums[i])) { int oldValue = map.get(nums[i]); int newValue = oldValue + 1; // 如果数目到一半了,返回 if (newValue > nums.length >> 1) { return nums[i]; } else { map.replace(nums[i], oldValue, newValue); } // 如果没有元素,加入该元素,出现次数设置为 1 } else { map.put(nums[i], 1); } } return 0; }
Example 5
Source File: Offer39.java From Project with Apache License 2.0 | 6 votes |
public int majorityElement(int[] nums) { if (nums.length == 1) { return nums[0]; } HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i])) { int oldValue = map.get(nums[i]); int newValue = oldValue + 1; if (newValue > nums.length >>> 1) { return nums[i]; } else { map.replace(nums[i], oldValue, newValue); } } else { map.put(nums[i], 1); } } return 0; }
Example 6
Source File: Offer03.java From Project with Apache License 2.0 | 6 votes |
public int findRepeatNumber(int[] nums) { if (nums.length == 0 || nums.length == 1) { return -1; } HashMap<Integer, Integer> resMap = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (resMap.containsKey(nums[i])) { int oldValue = resMap.get(nums[i]); int newValue = oldValue + 1; if (newValue >= 2) { return nums[i]; } else { resMap.replace(nums[i], oldValue, newValue); } } else { resMap.put(nums[i], 1); } } return -1; }
Example 7
Source File: TimeSeriesForestClassifier.java From AILibs with GNU Affero General Public License v3.0 | 6 votes |
/** * Predicts the class of the given instance by taking the majority vote of all * trees. * * @param univInstance * Univariate instance to be predicted */ @Override public Integer predict(final double[] univInstance) throws PredictionException { if (!this.isTrained()) { throw new PredictionException("Model has not been built before!"); } if (univInstance == null) { throw new IllegalArgumentException("Instance to be predicted must not be null or empty!"); } HashMap<Integer, Integer> votes = new HashMap<>(); for (int i = 0; i < this.trees.length; i++) { int prediction = this.trees[i].predict(univInstance); if (!votes.containsKey(prediction)) { votes.put(prediction, 1); } else { votes.replace(prediction, votes.get(prediction) + 1); } } return TimeSeriesUtil.getMaximumKeyByValue(votes); }
Example 8
Source File: DesignToRequireExtractor.java From SnowGraph with Apache License 2.0 | 6 votes |
private void addNode(String projectName, Node node, boolean isDesign) { if(projectName.equals("")) return; HashMap<String, ArrayList<Node>> map; if(isDesign) map = designSectionMap; else map = requireSectionMap; ArrayList<Node> nodes = new ArrayList<>(); if(map.containsKey(projectName)) { nodes = map.get(projectName); nodes.add(node); map.replace(projectName, nodes); } else { nodes.add(node); map.put(projectName, nodes); } }
Example 9
Source File: DataDstPb.java From xresloader with MIT License | 6 votes |
private void filterMissingFields(LinkedList<String> missingFields, HashMap<String, String> oneofField, Descriptors.FieldDescriptor fd, boolean isMissing) throws ConvException { if (missingFields == null && oneofField == null) { return; } Descriptors.OneofDescriptor oneof = fd.getContainingOneof(); if (isMissing && oneof == null && missingFields != null) { missingFields.push(fd.getName()); } if (!isMissing && oneof != null && oneofField.containsKey(oneof.getFullName())) { String old_field = oneofField.get(oneof.getFullName()); if (old_field != null) { setLastErrorMessage( "field \"%s\" in oneof descriptor \"%s\" already exists, can not add another field \"%s\" with the same oneof descriptor", old_field, oneof.getFullName(), fd.getName()); throw new ConvException(getLastErrorMessage()); } oneofField.replace(oneof.getFullName(), fd.getName()); } }
Example 10
Source File: IO.java From Client with MIT License | 5 votes |
public void updateConfig(String key, String value) throws Exception { HashMap<String,String> config = readConfig(); config.replace(key,value); StringBuilder toBeWritten = new StringBuilder(); for(String configKey : config.keySet()) { toBeWritten.append(configKey).append(" = ").append(config.get(configKey)).append("\n"); } writeToFile(toBeWritten.toString(),configPropFileLoc); }
Example 11
Source File: RenShuTongJi.java From Project with Apache License 2.0 | 5 votes |
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int workNum = scanner.nextInt(); int searchNum = scanner.nextInt(); scanner.nextLine(); String[] workMoneyString = scanner.nextLine().split(" "); // 保存要查询的员工工资序号 int[] search = new int[searchNum]; int i = 0; while (i < searchNum) { search[i] = scanner.nextInt(); i++; } int[] workMoney = new int[workNum]; for (int j = 0; j < workMoneyString.length; j++) { workMoney[j] = Integer.parseInt(workMoneyString[j]); } HashMap<Integer, Integer> workMoneyMap = new HashMap<>(); for (int k = 0; k < workMoney.length; k++) { if (workMoneyMap.containsKey(workMoney[k])) { int oldValue = workMoneyMap.get(workMoney[k]); workMoneyMap.replace(workMoney[k], oldValue, oldValue + 1); } else { workMoneyMap.put(workMoney[k], 1); } } // 查询 for (int i1 = 0; i1 < search.length; i1++) { if (!workMoneyMap.containsKey(search[i1])){ System.out.println(0); }else { int res = workMoneyMap.get(search[i1]); System.out.println(res); } } }
Example 12
Source File: OneNetConnectionManager.java From one-net with Apache License 2.0 | 5 votes |
public void registerClientSession(String contextName,String clientName, ClientSession clientSession) { HashMap<String ,ClientSession> clientSessionMap = this.getContextNameSessionMap().getOrDefault(contextName, new HashMap<>()); if (clientSessionMap.containsKey(clientName)) { ClientSession oldSession = clientSessionMap.replace(clientName, clientSession); oldSession.close(); }else{ clientSessionMap.putIfAbsent(clientName,clientSession); } this.getContextNameSessionMap().putIfAbsent(contextName,clientSessionMap); }
Example 13
Source File: SingleResponsibility.java From java with MIT License | 5 votes |
public static void main(String[] args) throws Exception { HashMap<Integer,Worker> companyWorkers = new HashMap<Integer,Worker>(); //Adding two workers companyWorkers.put(170015, new Worker(170015, 5000, 20, "John Johnz", "Project Developer") ); companyWorkers.put(170016, new Worker(170016, 5100, 20, "Rubi Ramos", "Project Manager") ); //Initialize overtime hour payment for the two types of Job Titles SalaryCalculator.initOvertimeHourPayment(); //Calculate Salary without extra hours HashMap<Integer,Integer> companyWorkersSalaries = SalaryCalculator.calculateWorkersSalary(companyWorkers); //Print to Console System.out.println("Salaries without extra hours:"); System.out.println( companyWorkers.get(170015) + ",\n\tSalary: " + companyWorkersSalaries.get(170015) + " money unit" ); System.out.println( companyWorkers.get(170016) + ",\n\tSalary: " + companyWorkersSalaries.get(170016) + " money unit" ); //Adding Extra Hours companyWorkers.replace(170015, companyWorkers.get(170015).addExtraHour(10) ); companyWorkers.replace(170016, companyWorkers.get(170016).addExtraHour(5) ); //Calculate Salary without extra hours companyWorkersSalaries = SalaryCalculator.calculateWorkersSalary(companyWorkers); //Print to Console System.out.println("\n\nSalaries with extra hours:"); System.out.println( companyWorkers.get(170015) + ",\n\tSalary: " + companyWorkersSalaries.get(170015) + " money unit" ); System.out.println( companyWorkers.get(170016) + ",\n\tSalary: " + companyWorkersSalaries.get(170016) + " money unit" ); }
Example 14
Source File: TimeSeriesUtil.java From AILibs with GNU Affero General Public License v3.0 | 5 votes |
/** * Returns the mode of the given <code>array</code>. If there are multiple * values with the same frequency, the lower value will be taken. * * @param array The array which mode should be returned * @return Returns the mode, i. e. the most frequently occurring int value */ public static int getMode(final int[] array) { HashMap<Integer, Integer> statistics = new HashMap<>(); for (int i = 0; i < array.length; i++) { if (!statistics.containsKey(array[i])) { statistics.put(array[i], 1); } else { statistics.replace(array[i], statistics.get(array[i]) + 1); } } return getMaximumKeyByValue(statistics) != null ? getMaximumKeyByValue(statistics) : -1; }