Java Code Examples for java.util.HashMap.getOrDefault()

The following are Jave code examples for showing how to use getOrDefault() of the java.util.HashMap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: AthenaX   File: JobWatcherUtilTest.java   View Source Code Vote up 6 votes
@Test
public void testComputeState() {
  HashMap<UUID, JobDefinition> jobMap = new HashMap<>();
  HashMap<UUID, InstanceInfo> instanceMap = new HashMap<>();
  mockState1(jobMap, instanceMap);

  JobWatcherUtil.StateView v = JobWatcherUtil.computeState(jobMap, instanceMap);

  HashMap<UUID, List<InstanceInfo>> jobInstances = new HashMap<>();
  HashMap<UUID, UUID> instanceToJob = new HashMap<>();

  for (Map.Entry<UUID, InstanceInfo> e : instanceMap.entrySet()) {
    UUID jobId = e.getValue().metadata().jobDefinition();
    instanceToJob.put(e.getKey(), jobId);
    List<InstanceInfo> li = jobInstances.getOrDefault(jobId, new ArrayList<>());
    li.add(e.getValue());
    jobInstances.put(jobId, li);
  }

  assertEquals(jobMap, v.jobs());
  assertEquals(instanceMap, v.instances());
  assertEquals(instanceToJob, v.instanceToJob());
  assertEquals(jobInstances, v.jobInstances());
}
 
Example 2
Project: DeskChan   File: MeaningExtractor.java   View Source Code Vote up 6 votes
public String analyze(String phrase){
    ArrayList<String> words= TextOperations.simplifyWords(TextOperations.extractWordsLower(phrase));
    float max=0;
    int index=-1;
    int count=0;
    HashMap<Integer,Float> phrase_meanings=new HashMap<>();
    HashMap<Integer,Integer> mp;
    for(String word : words){
        mp=meanings.getOrDefault(word,null);
        if(mp==null || mp.size()==0) continue;
        count++;
        for(HashMap.Entry<Integer,Float> entry : normalizeMeaningTable(mp).entrySet()){
            float f=phrase_meanings.getOrDefault(entry.getKey(),0f)+entry.getValue();
            phrase_meanings.put(entry.getKey(),f);
            if(f>max) {
                max = f;
                index = entry.getKey();
            }
        }
    }
    if(index<0 || max/count<0.5) return null;
    return meaningNames.get(index);
}
 
Example 3
Project: DeskChan   File: MeaningExtractor.java   View Source Code Vote up 6 votes
public void teach(String phrase,String meaning){
    ArrayList<String> words=TextOperations.simplifyWords(TextOperations.extractWordsLower(phrase));
    meaning=meaning.toUpperCase();
    int meanIndex=meaningNames.indexOf(meaning);
    if(meanIndex<0){
        meanIndex=meaningNames.size();
        meaningNames.add(meaning);
    }
    HashMap<Integer,Integer> mp;
    for(String word : words){
        mp=meanings.getOrDefault(word,null);
        if(mp==null){
            mp=new HashMap<Integer,Integer>();
            meanings.put(word,mp);
        }
        int a=mp.getOrDefault(meanIndex,0)+1;
        mp.put(meanIndex,a);
    }
}
 
Example 4
Project: ZentrelaRPG   File: EtcItem.java   View Source Code Vote up 5 votes
private static boolean justAte(PlayerDataRPG pd, String id) {
    if (!eatTimers.containsKey(pd.getUUID()))
        eatTimers.put(pd.getUUID(), new HashMap<String, Long>());
    HashMap<String, Long> map = eatTimers.get(pd.getUUID());
    long diff = System.currentTimeMillis() - map.getOrDefault(id, 0l);
    if (diff < 10000) {
        pd.sendMessage(ChatColor.GRAY + "> " + ChatColor.RED + "You just ate a stackable food! Try again in " + String.format("%.1f", 10 - (diff / 1000.0)) + "s.");
        return true;
    }
    map.put(id, System.currentTimeMillis());
    return false;
}
 
Example 5
Project: HL7Receiver   File: Segment.java   View Source Code Vote up 5 votes
private static Class<? extends Segment> getSegmentClass(String segmentName, HashMap<String, Class<? extends Segment>> zSegmentDefinitions) {
    Class<? extends Segment> segmentClass = SegmentName.getSegmentClass(segmentName);

    if (segmentClass == null)
        if (zSegmentDefinitions != null)
            segmentClass = zSegmentDefinitions.getOrDefault(segmentName, null);

    if (segmentClass == null)
        segmentClass = Segment.class;

    return segmentClass;
}
 
Example 6
Project: weak-semi-crf-naacl2016   File: SMSNPUtil.java   View Source Code Vote up 5 votes
private void getMode(){
	HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
	modeCount = 0;
	mode = -1;
	for(int num: nums){
		int count = counts.getOrDefault(num, 0);
		counts.put(num, count+1);
		if(count+1 > modeCount){
			modeCount = count+1;
			mode = num;
		}
	}
	
}
 
Example 7
Project: WurstSDK   File: Kernel.java   View Source Code Vote up 4 votes
@Override
public boolean hasPermission(JavaPlugin plugin, Permission permission) {
    WurstSDK sdk = (WurstSDK) SDK.getClient();
    HashMap<String, Boolean> perms = sdk.getConfig().getPermissions(plugin, token);
    return perms != null && perms.getOrDefault(permission.getName(), false);
}