public class NewClass
{
public static void main(String args[])
{
// 1 Creation of HashMap
HashMap<String, String> Geeks = new HashMap<>();
// 2 Adding values to HashMap as ("keys", "values")
Geeks.put("Language", "Java");
Geeks.put("Platform", "Geeks For geeks");
Geeks.put("Code", "HashMap");
Geeks.put("Learn", "More");
// 3 containsKey() method is to check the presence
// of a particluar key
// Since 'Code' key present here, the condition is true
if (Geeks.containsKey("Code"))
System.out.println("Testing .containsKey : " +
Geeks.get("Code"));
// 4 keySet() method returns all the keys in HashMap
Set<String> Geekskeys = Geeks.keySet();
System.out.println("Initial keys : " + Geekskeys);
// 5 values() method return all the values in HashMap
Collection<String> Geeksvalues = Geeks.values();
System.out.println("Initial values : " + Geeksvalues);
// Adding new set of key-value
Geeks.put("Search", "JavaArticle");
// Again using .keySet() and .values() methods
System.out.println("New Keys : " + Geekskeys);
System.out.println("New Values: " + Geeksvalues);
}
}
public class HashMapDemo {
public static void main(String args[]) {
// create hash map
HashMap newmap = new HashMap();
// populate hash map
newmap.put(1, "tutorials");
newmap.put(2, "point");
newmap.put(3, "is best");
// check existence of key 2
System.out.println("Check if key 2 exists: " + newmap.containsKey(2));
}
}
public class TestMap {
public static void main(String[] args) {
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("orange", 2);
fruits.put("banana", 3);
fruits.put("watermelon", null);
System.out.println("1. Is key 'apple' exists?");
if (fruits.containsKey("apple")) {
//key exists
System.out.println("yes! - " + fruits.get("apple"));
} else {
//key does not exists
System.out.println("no!");
}
System.out.println("\n2. Is key 'watermelon' exists?");
if (fruits.containsKey("watermelon")) {
System.out.println("yes! - " + fruits.get("watermelon"));
} else {
System.out.println("no!");
}
}
}
public class KeySearchDemo {
public static void main(String args[]) {
// our sample map
Map < Integer, String > idToName = new HashMap < Integer, String > () {
{
put(101, "Johnny");
put(102, "Root");
put(103, "Shane");
}
};
System.out.println(idToName);
// checking if a key exists in Map
Scanner scnr = new Scanner(System.in);
System.out.println("Please enter a key to check in Map?");
int key = scnr.nextInt();
// checking if key exists in HashMap
if (idToName.containsKey(key)) {
System.out.println("Congrats, given key exits in Map");
} else {
System.out.println("Sorry, given key doesn't exists in Map");
}
scnr.close();
}
}
public class MyHashMapKeySearch {
public static void main(String a[]){
HashMap<String, String> hm = new HashMap<String, String>();
//add key-value pair to hashmap
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third","THIRD INSERTED");
System.out.println(hm);
if(hm.containsKey("first")){
System.out.println("The hashmap contains key first");
} else {
System.out.println("The hashmap does not contains key first");
}
if(hm.containsKey("fifth")){
System.out.println("The hashmap contains key fifth");
} else {
System.out.println("The hashmap does not contains key fifth");
}
}
}
public static EcrfFieldValueAuditTrailLogEagerModel getCachedAuditTrailLogModel(ProbandListEntryOutVO listEntry,
HashMap<Long, EcrfFieldValueAuditTrailLogEagerModel> auditTrailLogModelCache) {
EcrfFieldValueAuditTrailLogEagerModel model;
if (listEntry != null && auditTrailLogModelCache != null) {
long listEntryId = listEntry.getId();
if (auditTrailLogModelCache.containsKey(listEntryId)) {
model = auditTrailLogModelCache.get(listEntryId);
} else {
model = new EcrfFieldValueAuditTrailLogEagerModel();
model.setListEntryId(listEntryId);
auditTrailLogModelCache.put(listEntryId, model);
}
} else {
model = new EcrfFieldValueAuditTrailLogEagerModel();
}
return model;
}
public HashMap<Long,Integer> extractUniqueLocationIdCounts(
List<ExtendedLocationTrace> traces) {
HashMap<Long,Integer> idMap = new HashMap<Long, Integer>();
for(ExtendedLocationTrace aTrace: traces){
// all location ids start from 1. if there is a lower number, it is likely that DBSCAN assigned noise to that visit
if(aTrace.getLocationId()>0){
int count = 0;
if (idMap.containsKey(aTrace.getLocationId()) == true){
count = idMap.get(aTrace.getLocationId());
}
count++;
idMap.put(aTrace.getLocationId(), count);
}
}
return idMap ;
}
public static CollidingVisitScheduleItemEagerModel getCachedCollidingVisitScheduleItemModel(ProbandStatusEntryOutVO statusEntry, boolean allProbandGroups,
HashMap<Long, CollidingVisitScheduleItemEagerModel> collidingVisitScheduleItemModelCache) {
CollidingVisitScheduleItemEagerModel model;
if (statusEntry != null && collidingVisitScheduleItemModelCache != null) {
long probandStatusEntryId = statusEntry.getId();
if (collidingVisitScheduleItemModelCache.containsKey(probandStatusEntryId)) {
model = collidingVisitScheduleItemModelCache.get(probandStatusEntryId);
} else {
model = new CollidingVisitScheduleItemEagerModel(probandStatusEntryId, allProbandGroups);
collidingVisitScheduleItemModelCache.put(probandStatusEntryId, model);
}
} else {
model = new CollidingVisitScheduleItemEagerModel(allProbandGroups);
}
return model;
}
private static void occurenceAdderSink(HashMap<String, HashSet<String>> sinks, HashMap<String, HashSet<String>> sources, HashMap<String, Integer> sinksOccurred, HashMap<String, Integer> sinksOccurredNoSource) {
for (String sink : sinks.keySet()) {
//System.out.println(sink);
if (sinksOccurred.containsKey(sink)) {
sinksOccurred.put(sink, sinksOccurred.get(sink)+1);
} else {
sinksOccurred.put(sink, 1);
sinksOccurredNoSource.put(sink,0);
}
if (sources.isEmpty()) {
sinksOccurredNoSource.put(sink, sinksOccurredNoSource.get(sink)+1);
}
}
}
public SrlgGraphSearch(Map<E, Object> grouping) {
if (grouping == null) {
useSuurballe = true;
return;
}
numGroups = 0;
HashMap<Object, Integer> tmpMap = new HashMap<>();
riskGrouping = new HashMap<>();
for (E key: grouping.keySet()) {
Object value = grouping.get(key);
if (!tmpMap.containsKey(value)) {
tmpMap.put(value, numGroups);
numGroups++;
}
riskGrouping.put(key, tmpMap.get(value));
}
}