In this lecture you will learn what is a map? and how to use the map in java, So first let's remove our code from our main method if you had any activity before or create a new class for the seek of this tutorial, So what is actually a map, map in java is same as English Dictionary! English Dictionary may be a collection of words and their definitions. They are often listed alphabetically in one or more specific languages, in the same way, The map in java is a collection of keys and values but the key is sort of a word, and value is sort of a definition. With map we need to specify two types, One type is for the key and another type is for value, So the map is collection of key-value pairs to demonstrate usage of the map we will use Weapons and damage. So each weapon has damage and we want to store weapons by damage at a later stage we want to get damage to the specific weapons by specifying a weapon name. This could be used to! let's say quickly find the weapon that has the highest damage in-game. Now in our main method, we will declare a Map interface like the code below
COMPLETE CODES:
Notice that Map is an interface and we will use a specific implementation which is hashMap in the <String, Integer> angle bracket we need to type two type, The first type is 'String' that is a key type for our elements and then the next type will be 'integer' which is a damage for a weapon, I will name variable as weaponsByDamage = new HashMap<>(); 'HashMap' is just implementation of the interface Now again we are using polymorphism since our weapons for damage is a reference of a map in memory and it is a 'hashMap', Now to add something into Map, There is a method "put" and to get something there is a method "Get" So let's add some 'arrows','pikes', 'swords' to our weapon like 'weaponsByDamage.put()' Now we need to specify two types or two arguments, First is key which is in our case is String, and an integer is a value. Notice we declare map as 'String', 'Integer' that is why to put() method saying String key If we declare it as 'Integer','Integer' the put() will have arguments integer, So this is where generics are really powerful. The first weapon will be ("pike",10) with damage let's say 10, type again weaponsByDamage.put("arrow",15) next weapon will be 'arrow' with damage of 15, then weaponsByDamage.put("sword",20); Next weapon will be sword with damage 20, Again type weaponsByDamage.put("pistol",30) next weapon will be ('pistol') with damage 30. And now from the map we can print value pairs using for loop we can print just keys or just values. So let's see all three solutions Type for(Map.Entry<String, Integer> entry: weaponsByDamage.entrySet()){} 'Map', This is an interface (.Entry) -Entry is interfaced inside the map interface, it is known as the inner interface And we need again to specify the entry values for the map which is a string in our case. (.entrySet()) is another collection doesn't allow duplicate, .entrySet() just return set of entries inside our map, The one Entry in our map is ("pike", 10) this is one entry, Second entry is ("arrow", 15) So we will print a key and value, First key is 'pike' and the next 'key' is 10 or value for that key is 10 we can say, The next key is "arrow" and so-on, Now to print, just print statement (System.out.println("key= "+entry.getKey()+"value="+entry.getValue())); So 'entry' is just like key-value entry or key-value pair. And now when we run our code you will see in output key-value pairs that we have on our map. In most cases they will not be in the same order because map doesn't preserve the order of adding the elements to map so first it prints 'key = sword value = 20', then 'key= arrow value = 15, then 'key= pike value = 10', then next 'key = pistol value = 30. If we want to print just let's say 'keys' then we can print keys by typing another for(String key: weaponsByDamage.keySet()) For-loop and this will return set's of keys, then type print line statement for the output (System.out.println("keys=" + key)); And for values there is also a values method that we can call like for(Integer value: weaponsByDamage().values()){} and This will return a collection of integers since our values are integers, Now we can print values like System.out.println("values=" +value); then run the code to see the actual output. Now let's see how we can get our set value inside the map. to get the value there is a method 'get' so we can just use 'System.out.println("sword has damage= "+weaponsByDamage.get("sword"));' We need to specify a key which in this case is ("sword") Now to set a new damage for "sword" we can type weaponsByDamage.put("sword",35) So we are again putting the same key with a different value, When we put the same key inside the map, the value will be overwritten so the sword will have different damage, now for the key, I will use "sword" and for damage let's say we can use '35' for example. if you will run the code you will see that the size of the map didn't change. So let's print the size as well type System.out.println("size=" +weaponsByDamage.size()); and we will print size as well after changing the "sword" damage instead of printing ("sword has damage") to weaponsByDamage.put("sword", 35); print like this instead 'System.out.println("sword has new damage=" + weaponsByDamage.get("sword"));' look for the above code for complete reference of codes.
COMPLETE CODES:
Notice that Map is an interface and we will use a specific implementation which is hashMap in the <String, Integer> angle bracket we need to type two type, The first type is 'String' that is a key type for our elements and then the next type will be 'integer' which is a damage for a weapon, I will name variable as weaponsByDamage = new HashMap<>(); 'HashMap' is just implementation of the interface Now again we are using polymorphism since our weapons for damage is a reference of a map in memory and it is a 'hashMap', Now to add something into Map, There is a method "put" and to get something there is a method "Get" So let's add some 'arrows','pikes', 'swords' to our weapon like 'weaponsByDamage.put()' Now we need to specify two types or two arguments, First is key which is in our case is String, and an integer is a value. Notice we declare map as 'String', 'Integer' that is why to put() method saying String key If we declare it as 'Integer','Integer' the put() will have arguments integer, So this is where generics are really powerful. The first weapon will be ("pike",10) with damage let's say 10, type again weaponsByDamage.put("arrow",15) next weapon will be 'arrow' with damage of 15, then weaponsByDamage.put("sword",20); Next weapon will be sword with damage 20, Again type weaponsByDamage.put("pistol",30) next weapon will be ('pistol') with damage 30. And now from the map we can print value pairs using for loop we can print just keys or just values. So let's see all three solutions Type for(Map.Entry<String, Integer> entry: weaponsByDamage.entrySet()){} 'Map', This is an interface (.Entry) -Entry is interfaced inside the map interface, it is known as the inner interface And we need again to specify the entry values for the map which is a string in our case. (.entrySet()) is another collection doesn't allow duplicate, .entrySet() just return set of entries inside our map, The one Entry in our map is ("pike", 10) this is one entry, Second entry is ("arrow", 15) So we will print a key and value, First key is 'pike' and the next 'key' is 10 or value for that key is 10 we can say, The next key is "arrow" and so-on, Now to print, just print statement (System.out.println("key= "+entry.getKey()+"value="+entry.getValue())); So 'entry' is just like key-value entry or key-value pair. And now when we run our code you will see in output key-value pairs that we have on our map. In most cases they will not be in the same order because map doesn't preserve the order of adding the elements to map so first it prints 'key = sword value = 20', then 'key= arrow value = 15, then 'key= pike value = 10', then next 'key = pistol value = 30. If we want to print just let's say 'keys' then we can print keys by typing another for(String key: weaponsByDamage.keySet()) For-loop and this will return set's of keys, then type print line statement for the output (System.out.println("keys=" + key)); And for values there is also a values method that we can call like for(Integer value: weaponsByDamage().values()){} and This will return a collection of integers since our values are integers, Now we can print values like System.out.println("values=" +value); then run the code to see the actual output. Now let's see how we can get our set value inside the map. to get the value there is a method 'get' so we can just use 'System.out.println("sword has damage= "+weaponsByDamage.get("sword"));' We need to specify a key which in this case is ("sword") Now to set a new damage for "sword" we can type weaponsByDamage.put("sword",35) So we are again putting the same key with a different value, When we put the same key inside the map, the value will be overwritten so the sword will have different damage, now for the key, I will use "sword" and for damage let's say we can use '35' for example. if you will run the code you will see that the size of the map didn't change. So let's print the size as well type System.out.println("size=" +weaponsByDamage.size()); and we will print size as well after changing the "sword" damage instead of printing ("sword has damage") to weaponsByDamage.put("sword", 35); print like this instead 'System.out.println("sword has new damage=" + weaponsByDamage.get("sword"));' look for the above code for complete reference of codes.
0 Comments