Зміст курсу
Java Data Structures
Java Data Structures
Map with Other Data Structures
In general, there's not much more to tell about maps. The information that it is a data structure that stores data based on the key-value principle is sufficient. But what if we want to pull off a little adventure and pass a data structure like an ArrayList
as a value in the map?
Passing Data Structures as Values in a Map
Perhaps we'll have too little collision in the hashmap, and we might want to store data in an even more peculiar way.
For example:
main
package com.example; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static void main(String[] args) { List<String> marketingDepartment = new ArrayList<>(); marketingDepartment.add("Michael"); marketingDepartment.add("Alice"); marketingDepartment.add("Jimmy"); List<String> developerDepartment = new ArrayList<>(); developerDepartment.add("Bob"); developerDepartment.add("John"); developerDepartment.add("Ryan"); Map<String, List<String>> company = new HashMap<>(); company.put("Marketing", marketingDepartment); company.put("Development", developerDepartment); System.out.println("Company: " + company); } }
Here's an example of creating a data structure that stores information about a particular company. This approach is sometimes used when working with databases, but it's advisable not to abuse it, as retrieving data from such data structures can be challenging. For instance, suppose we want to retrieve the marketer at index 1:
main
package com.example; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static void main(String[] args) { List<String> marketingDepartment = new ArrayList<>(); marketingDepartment.add("Michael"); marketingDepartment.add("Alice"); marketingDepartment.add("Jimmy"); List<String> developerDepartment = new ArrayList<>(); developerDepartment.add("Bob"); developerDepartment.add("John"); developerDepartment.add("Ryan"); Map<String, List<String>> company = new HashMap<>(); company.put("Marketing", marketingDepartment); company.put("Development", developerDepartment); System.out.println("Company: " + company); String marketerAtIndexOne = company.get("Marketing").get(1); System.out.println("Marketer at index 1 is: " + marketerAtIndexOne); } }
At first glance, it doesn't seem complicated. You just need to use the method one more time. However, in programming, it is crucial to avoid hardcoding.
Hardcoding is bad, and it should be avoided in every way. Let me show you an example with hardcoding, and then we'll fix it together:
main
package com.example; public class Main { public static void main(String[] args) { // Hardcoded value double price = 49.99; // Using hardcoded value double discountedPrice = price * 0.8; // 20% discount System.out.println("Discounted Price: " + discountedPrice); } }
As you can see, there is hardcoding in the code above. When specifying the discount, we use a plain number. We need to store this discount in a variable so that we can reuse this value later. Let's improve the code above:
main
public class Main { public static void main(String[] args) { // Using variables instead of hardcoding double price = 49.99; double discountPercentage = 0.2; // 20% discount // Using variables double discountedPrice = price * (1 - discountPercentage); System.out.println("Discounted Price: " + discountedPrice); } }
This way, we obtain a variable with the discount value, and in a large program in the future, we would only need to change the value of this single variable.
If we had hardcoded it, we would have to change the value at every instance, significantly increasing the time it takes to enhance or edit the code.
Summary
In summary, it can be said that in data structures, various types of data, including other data structures, can be used. This adds convenience to the use of these data structures and flexibility to your application. However, one should not forget about algorithmic complexity, as it is a crucial parameter when writing an application. When using data structures within other data structures, it can be quite easy to make mistakes and significantly complicate the execution of a specific operation.
Keep an eye on that, and your code will be excellent!
Дякуємо за ваш відгук!