public class Hash_Map_Demo {
public static void main(String[] args) {
// Creating an empty HashMap
HashMap<Integer, String> hash_map = new HashMap<Integer, String>();
// Mapping string values to int keys
hash_map.put(10, "Geeks");
hash_map.put(15, "4");
hash_map.put(20, "Geeks");
hash_map.put(25, "Welcomes");
hash_map.put(30, "You");
// Displaying the HashMap
System.out.println("Initial Mappings are: " + hash_map);
// Inserting existing key along with new value
String returned_value = (String)hash_map.put(20, "All");
// Verifying the returned value
System.out.println("Returned value is: " + returned_value);
// Displayin the new map
System.out.println("New map is: " + hash_map);
}
}
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");
System.out.println("Map value before change: "+ newmap);
// put new values at key 3
String prevvalue = (String)newmap.put(3,"is great");
// check returned previous value
System.out.println("Returned previous value: "+ prevvalue);
System.out.println("Map value after change: "+ newmap);
}
}
public class HashMapPutExample {
public static void main(String args[]) throws InterruptedException {
// declare the hashmap
HashMap<Integer, String> mapStudent = new HashMap<>();
boolean loopAgain = true;
Scanner scan = new Scanner(System.in);
// loop while user not entering no
do {
// ask for user input for id number
System.out.print("Enter ID number:");
Integer idNumber = Integer.parseInt(scan.nextLine());
// ask for user input which corresponds to student name
System.out.print("Enter Name:");
String name = scan.nextLine();
// add the key value pair from user input to the hashmap
String oldVal = mapStudent.put(idNumber, name);
if (oldVal!=null) {
System.out.println("Student Id Number:" + idNumber + " is "
+ oldVal + " and has been overwritten by " + name);
}
// ask user to check if another entry is required
System.out.print("Enter another student (y/n)?");
String answer = scan.nextLine();
// condition to satisfy in order to loop again
if (answer.equals("y") || answer.equals("Y")) {
continue;
} else {
break;
}
} while (loopAgain);
scan.close();
System.out.println("\n**********************************");
System.out.println("The following students are in database");
System.out.println(" ID Number "+ " Name");
for(int id:mapStudent.keySet()){
System.out.println(" "+id+" "+mapStudent.get(id));
}
System.out.println("\n**********************************");
}
}
public class CreateHashMapExample {
public static void main(String[] args) {
// Creating a HashMap
Map<String, Integer> numberMapping = new HashMap<>();
// Adding key-value pairs to a HashMap
numberMapping.put("One", 1);
numberMapping.put("Two", 2);
numberMapping.put("Three", 3);
// Add a new key-value pair only if the key does not exist in the HashMap, or is mapped to `null`
numberMapping.putIfAbsent("Four", 4);
System.out.println(numberMapping);
}
}
public class MyClass {
public static void main(String[] args) {
// Create a HashMap object called capitalCities
HashMap<String, String> capitalCities = new HashMap<String, String>();
// Add keys and values (Country, City)
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities);
}
}
private static Node generateRootWithGhostTutor(List<Tutor> tutors, List<Course> courses, List<Shift> shifts) {
HashMap<Rank, Set<Course>> ghostCoursePreferences = new HashMap<>();
ghostCoursePreferences.put(Rank.PREFER, new HashSet<>(courses));
HashMap<Rank, Set<Shift>> ghostShiftPreferences = new HashMap<>();
ghostShiftPreferences.put(Rank.PREFER, new HashSet<>(shifts));
HashMap<Rank, Integer> ghostShiftFrequencyPreferences = new HashMap<>();
ghostShiftFrequencyPreferences.put(Rank.PREFER, Integer.MAX_VALUE);
TutorPreferences ghostTutorPreference = new TutorPreferences(ghostCoursePreferences, ghostShiftPreferences, ghostShiftFrequencyPreferences);
Tutor ghostTutor = new Tutor(-1, "Ghost", "Tutor", ghostTutorPreference, TutorStatus.ACTIVE);
tutors.add(ghostTutor);
return generateRoot(tutors, courses, shifts);
}
public Map<String, String> getHeaders() {
Map<String, List<String>> map = con.getHeaderFields();
HashMap<String, String> res = new HashMap<String, String>(map.size());
for (String key : map.keySet()) {
List<String> list = map.get(key);
StringBuilder tmp = new StringBuilder();
int n = 0;
if (list.size() == 1) {
res.put(key, list.get(0));
continue;
}
if (list.size() == 0) {
res.put(key, "");
continue;
}
for (String one : list) {
if (n > 0)
tmp.append(";");
tmp.append(one);
n++;
}
res.put(key, tmp.toString());
}
return res;
}
public Map<String, ? extends FilterRegistration> getFilterRegistrations()
{
if (!_enabled)
throw new UnsupportedOperationException();
HashMap<String, FilterRegistration> registrations = new HashMap<String, FilterRegistration>();
ServletHandler handler=ServletContextHandler.this.getServletHandler();
FilterHolder[] holders=handler.getFilters();
if (holders!=null)
{
for (FilterHolder holder : holders)
registrations.put(holder.getName(),holder.getRegistration());
}
return registrations;
}
public @Nullable Entrant calculateWinner() {
HashMap<Entrant, Integer> wins = new HashMap<>();
for (Map.Entry<String, Entrant> match : this.winnerStore.entrySet()) {
Entrant winner = match.getValue();
if (winner != null) {
int winCount = Optional.fromNullable(wins.get(winner)).or(0) + 1;
wins.put(winner, winCount);
if (winCount > 1) {
tourney.recordMatch(match.getKey());
return winner;
}
}
}
if (this.getMatchesPlayed() > 2) tourney.recordMatch(this.winnerStore.keySet().iterator().next());
return null;
}
public void doSerialize(RegistrationData registrationData, IAccount account) throws IOException {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("publicKey", Format.convert(registrationData.getPublicKey()));
account.putProperty(new AccountProperty(PropertyType.REGISTRATION, data));
}