CodeGym /Java Course /Frontend SELF EN /New data structures: Map, Set

New data structures: Map, Set

Frontend SELF EN
Level 45 , Lesson 2
Available

3.1 Collection Map

In ECMAScript 2015 (ES6), JavaScript added two new data structures: Map and Set. These data structures provide more flexible and efficient ways to store and manage collections of data compared to traditional objects and arrays.

Map is a data structure that allows you to store key-value pairs, where keys can be any type, including objects and functions.

1. Creating and initializing a Map:

JavaScript
    
      const map = new Map();
    
  

2. Adding elements:

The set method is used to add new key-value pairs to a Map.

JavaScript
    
      map.set('key1', 'value1');
      map.set(2, 'value2');
      map.set({ name: 'objectKey' }, 'value3');
    
  

3. Retrieving elements:

The get method is used to retrieve a value by key.

JavaScript
    
      const map = new Map();

      map.set('key1', 'value1');
      map.set(2, 'value2');
      map.set({ name: 'objectKey' }, 'value3');

      console.log(map.get('key1')); // 'value1'
      console.log(map.get(2)); // 'value2'
    
  

4. Checking for a key:

The has method checks if a key exists in a Map.

JavaScript
    
      const map = new Map();

      map.set('key1', 'value1');
      map.set(2, 'value2');
      map.set({ name: 'objectKey' }, 'value3');

      console.log(map.has('key1')); // true
      console.log(map.has('key3')); // false
    
  

5. Deleting elements:

The delete method removes an element by key.

JavaScript
    
      const map = new Map();

      map.set('key1', 'value1');
      map.set(2, 'value2');
      map.set({ name: 'objectKey' }, 'value3');

      map.delete('key1');
      console.log(map.has('key1')); // false
    
  

6. Clearing the Map:

The clear method removes all elements from a Map.

JavaScript
    
      const map = new Map();

      map.set('key1', 'value1');
      map.set(2, 'value2');
      map.set({ name: 'objectKey' }, 'value3');

      map.clear();
      console.log(map.size); // 0
    
  

7. Iterating over a Map:

You can use a for...of loop to iterate over a Map.

Example:

JavaScript
    
      const map = new Map();
      map.set('key1', 'value1');
      map.set(2, 'value2');

      for (const [key, value] of map) {
        console.log(`${key}: ${value}`);
      }

      // 'key1: value1'
      // '2: value2'
    
  

3.2 Collection Set

Set is a data structure that allows you to store unique values of any type, including objects and primitives.

1. Creating and initializing a Set:

JavaScript
    
      const set = new Set();
    
  

2. Adding elements:

The add method is used to add new values to a Set.

JavaScript
    
      set.add(1);
      set.add('value2');
      set.add({ name: 'objectValue' });
    
  

3. Checking for a value:

The has method checks if a value exists in a Set.

JavaScript
    
      const set = new Set();

      set.add(1);
      set.add('value2');
      set.add({ name: 'objectValue' });

      console.log(set.has(1)); // true
      console.log(set.has('value3')); // false
    
  

4. Deleting elements:

The delete method removes a value from a Set.

JavaScript
    
      const set = new Set();

      set.add(1);
      set.add('value2');
      set.add({ name: 'objectValue' });

      set.delete(1);
      console.log(set.has(1)); // false
    
  

5. Clearing the Set:

The clear method removes all elements from a Set.

JavaScript
    
      const set = new Set();

      set.add(1);
      set.add('value2');
      set.add({ name: 'objectValue' });

      set.clear();
      console.log(set.size); // 0
    
  

6. Iterating over a Set:

You can use a for...of loop to iterate over a Set.

JavaScript
    
      const set = new Set();
      set.add(1);
      set.add('value2');

      for (const value of set) {
        console.log(value);
      }

      // 1
      // 'value2'
    
  

3.3 Comparing Map and Set

Advantages of Map:

  1. Arbitrary keys: In Map, keys can be of any type, including objects and functions, unlike objects where keys can only be strings or symbols.
  2. Ordered elements: Elements in a Map maintain the order of insertion, making it easier to iterate over them in that order.
  3. Collection size: The size method allows you to quickly get the number of elements in a Map.

Advantages of Set:

  1. Unique values: A Set automatically ensures that all its elements are unique.
  2. Ordered elements: Elements in a Set maintain the order of insertion, making it easier to iterate over them in that order.
  3. Optimized operations: Set provides optimized methods for adding, deleting, and checking for the presence of elements.

Practical Examples

Example of using Map to store user information:

JavaScript
    
      const users = new Map();
      users.set(1, { name: 'Alice', age: 25 });
      users.set(2, { name: 'Bob', age: 30 });

      for (const [id, user] of users) {
        console.log(`User ID: ${id}, Name: ${user.name}, Age: ${user.age}`);
      }

      // User ID: 1, Name: Alice, Age: 25
      // User ID: 2, Name: Bob, Age: 30
    
  

Example of using Set to filter unique values:

JavaScript
    
      const numbers = [1, 2, 3, 1, 2, 4, 5, 3];
      const uniqueNumbers = new Set(numbers);

      for (const number of uniqueNumbers) {
        console.log(number);
      }

      // 1
      // 2
      // 3
      // 4
      // 5
    
  
1
Task
Frontend SELF EN, level 45, lesson 2
Locked
Unique Emails
Unique Emails
1
Task
Frontend SELF EN, level 45, lesson 2
Locked
Users and Roles
Users and Roles
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION