1. Sets: Unordered Collections of Unique Values
Sets are collections of values where each value must be unique. They provide methods for adding, deleting, and checking the existence of elements.
// Creating a Set
const mySet = new Set([1, 2, 3, 1, 2]); // Duplicates are automatically removed
// Adding elements to a Set
mySet.add(4).add(5);
// Deleting elements from a Set
mySet.delete(2);
// Checking the existence of an element
console.log(mySet.has(3)); // Output: true
// Iterating over a Set
mySet.forEach(value => console.log(value));
// Output:
// 1
// 3
// 4
// 5
Maps are collections of key-value pairs, allowing efficient data retrieval based on keys.
// Creating a Map
const myMap = new Map();
// Adding key-value pairs to a Map
myMap.set("name", "Alice");
myMap.set("age", 30);
// Retrieving values from a Map
console.log(myMap.get("name")); // Output: Alice
// Checking the existence of a key
console.log(myMap.has("gender")); // Output: false
// Deleting a key-value pair from a Map
myMap.delete("age");
// Iterating over a Map
myMap.forEach((value, key) => console.log(`${key}: ${value}`));
// Output:
// name: Alice
Sets offer distinct advantages over arrays for certain use cases:
// Using a Set to remove duplicates from an array
const arrayWithDuplicates = [1, 2, 3, 1, 2];
const uniqueValues = [...new Set(arrayWithDuplicates)];
console.log(uniqueValues); // Output: [1, 2, 3]
The performance characteristics of sets and arrays depend on the specific operations you’re performing. Here are some key points to consider:
for
loop can be faster than iterating over a set using the forEach
method.indexOf
can be faster than searching in a set if the position/index of the element is known.Certainly! WeakSet
is a specialized collection in JavaScript that allows you to store weakly held object references. The primary characteristic of a WeakSet
is that it allows the garbage collector to reclaim the objects it references when those objects are no longer used elsewhere in your program. Here are some key points about WeakSet
:
Weak References:
Set
, a WeakSet
can only contain objects, and these objects are held by weak references.Automatic Garbage Collection:
WeakSet
, it doesn’t prevent the object from being garbage-collected if there are no other references to it outside of the WeakSet
.No Iteration:
Set
, WeakSet
does not have methods for iteration such as forEach
, keys
, values
, or entries
. This is because the contents of a WeakSet
are not enumerable.Methods:
WeakSet
has three main methods: add(value)
, has(value)
, and delete(value)
.Use Cases:
WeakSet
is often used in scenarios where you want to associate additional data or metadata with specific objects but don’t want to prevent those objects from being garbage-collected when they are no longer used elsewhere.Here’s a basic example:
let object1 = { key: "value" };
let object2 = { key: "value" };
const weakSet = new WeakSet([object1, object2]);
console.log(weakSet.has(object1)); // Output: true
// If there are no other references to object1 and object2, they can be garbage-collected
object1 = null;
object2 = null;
In this example, if there are no other references to object1
and object2
, they may be garbage-collected, and the WeakSet
will automatically remove them from its collection.
Keep in mind that the use of WeakSet
is more specialized, and it’s particularly helpful in scenarios where automatic garbage collection of associated objects is desired.
WeakMap
is another specialized data structure in JavaScript, similar to WeakSet
, that allows you to create a collection of key-value pairs where the keys are weakly referenced. Here are some key points about WeakMap
:
Key Weak References:
WeakSet
, WeakMap
allows the keys to be weakly referenced. This means that if there are no other strong references to a key, it can be garbage-collected.Object-Only Keys:
WeakMap
, keys can only be objects. This is different from regular Map
where keys can be any data type, including primitives.Automatic Garbage Collection:
WeakMap
is no longer referenced anywhere else in your program, it becomes eligible for garbage collection, and the corresponding key-value pair is automatically removed from the WeakMap
.No Iteration:
WeakSet
, WeakMap
does not have methods for direct iteration. This is because the contents of a WeakMap
are not enumerable.Methods:
WeakMap
provides methods like set(key, value)
, get(key)
, has(key)
, and delete(key)
.Use Cases:
WeakMap
is commonly used when you want to associate additional data with objects but without preventing those objects from being garbage-collected.Here’s a simple example:
let key1 = { id: 1 };
let key2 = { id: 2 };
const weakMap = new WeakMap();
// Setting key-value pairs
weakMap.set(key1, "Value associated with key1");
weakMap.set(key2, "Value associated with key2");
console.log(weakMap.get(key1)); // Output: Value associated with key1
// If there are no other references to key1, it can be garbage-collected, and the entry is automatically removed
key1 = null;
console.log(weakMap.has(key1)); // Output: false
In this example, if there are no other references to key1
, it becomes eligible for garbage collection, and the corresponding entry in the WeakMap
is automatically removed.
Use WeakMap
when you need to associate private or additional data with objects and you don’t want to interfere with the garbage collection of those objects.
Sets and Maps offer powerful alternatives to arrays and objects in JavaScript, providing distinct functionalities for different scenarios. Whether you need to ensure uniqueness, efficient key-based data retrieval, or garbage collection-friendly structures, Sets and Maps have got you covered.
Encapsulation and abstraction are two pillars of object-oriented programming (OOP) that play a vital role…
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects to take on…
Inheritance is a cornerstone of object-oriented programming (OOP) and one of its most powerful features.…
In the world of C# and object-oriented programming (OOP), classes and objects form the backbone…
In modern C# programming, working with data collections is a common task. Understanding how to…
Exception handling is a critical part of writing robust and maintainable C# applications. It allows…