Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Map with Other Data Structures | Map
Java Data Structures
course content

Зміст курсу

Java Data Structures

Java Data Structures

1. Basic Data Structures
2. Additional Data Structures
3. Map
4. enum & Stream API

bookMap 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:

java

main

copy
1234567891011121314151617181920212223242526
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:

java

main

copy
1234567891011121314151617181920212223242526272829
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:

java

main

copy
123456789101112
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:

java

main

copy
1234567891011
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!

1. What is the time complexity of an algorithm that iterates through an array of size `n` and performs a constant-time operation on each element?
2. What is the main drawback of hardcoding values directly in the code?
3. Given two algorithms with time complexities `O(n log n)` and `O(n^2)`, which one is generally more efficient for large input sizes?
What is the time complexity of an algorithm that iterates through an array of size `n` and performs a constant-time operation on each element?

What is the time complexity of an algorithm that iterates through an array of size n and performs a constant-time operation on each element?

Виберіть правильну відповідь

What is the main drawback of hardcoding values directly in the code?

What is the main drawback of hardcoding values directly in the code?

Виберіть правильну відповідь

Given two algorithms with time complexities `O(n log n)` and `O(n^2)`, which one is generally more efficient for large input sizes?

Given two algorithms with time complexities O(n log n) and O(n^2), which one is generally more efficient for large input sizes?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4
We're sorry to hear that something went wrong. What happened?
some-alt