๐Doctor Strange's Guide to Collections in Java๐งโโ๏ธโจ
Code Sample Available Below ๐
Greetings, fellow coders! ๐
Today, we're diving into the mystical realm of Java Collections Framework, guided by none other than the Sorcerer Supreme himself, Doctor Strange!
๐ Let's explore List, Set, and Map through the enchanting world of spells, artifacts, and portals.
๐จโ๐ป Ready to cast some coding magic? ๐
๐ฎ Library of Spells
List Imagine having access to the Sanctum Sanctorum's library filled with ancient and powerful spells!
๐ In Java, we can use a List to store these spells. Lists maintain the order of insertion and allow duplicates, making them perfect for our spell collection.
๐ Lists are great for keeping things in order, just like how Doctor Strange meticulously organizes his Spell-Book! ๐ช
๐บArtifacts of the Sanctum Sanctorum
Set Next, let's look at the unique and powerful artifacts housed in the Sanctum Sanctorum.
๐บ To ensure no duplicates, we use a Set. Sets are perfect for collections where each item must be unique.
๐ Sets ensure that each artifact is one-of-a-kind, just like the mystical items Doctor Strange relies on! ๐ฎ
๐ Portal Coordinates๐ซ
Map Lastly, let's navigate through the multiverse using portals!
๐ A Map is perfect for storing portal coordinates, mapping dimension names to their specific locations.
๐บ๏ธ Maps are ideal for handling key-value pairs, ensuring Doctor Strange can always find his way through the multiverse! ๐
โจ Bonus:
Sorting the Collections! Just like organizing spells and artifacts, sorting can be magical too!
๐งน Using Collections.sort(), we can keep our lists neat and ready for any adventure.
๐ฎ Sorting helps keep our collections organized, ready to face any mystical threat that comes our way! ๐งโโ๏ธ๐
Embrace the magic of Java Collections and may your code be as powerful as the Sorcerer Supreme! ๐ป๐ฎ
#Java #Coding #CollectionsFramework #DoctorStrange #Programming #Hashnode #Medium #Tech #Automation #SoftwareEngineering #ArtificialIntelligence
๐๐Full Code Here๐๐
import java.util.*;
public class DoctorStrangeCollections {
public static void main(String[] args) {
// List - Sanctum Sanctorum's Library of Spells
List spells = new ArrayList<>();
spells.add("Winds of Watoomb");
spells.add("Crimson Bands of Cyttorak");
spells.add("Vapors of Valtorr");
spells.add("Bolts of Bedevilment");
System.out.println("Library of Spells:");
for (String spell : spells) {
System.out.println(spell);
}
// Set - Artifacts of the Sanctum Sanctorum (No duplicates allowed)
Set artifacts = new HashSet<>();
artifacts.add("Eye of Agamotto");
artifacts.add("Cloak of Levitation");
artifacts.add("Book of Vishanti");
artifacts.add("Wand of Watoomb");
artifacts.add("Eye of Agamotto"); // Duplicate entry, won't be added
System.out.println("\nArtifacts in the Sanctum Sanctorum:");
for (String artifact : artifacts) {
System.out.println(artifact);
}
// Map - Portal coordinates to different dimensions
Map<String, String> portals = new HashMap<>();
portals.put("Dark Dimension", "13ยฐN, 77ยฐE");
portals.put("Mirror Dimension", "23ยฐS, 45ยฐW");
portals.put("Astral Dimension", "0ยฐ, 0ยฐ");
portals.put("Dark Dimension", "14ยฐN, 76ยฐE"); // Overwriting previous entry
System.out.println("\nPortal Coordinates:");
for (Map.Entry<String, String> portal : portals.entrySet()) {
System.out.println(portal.getKey() + ": " + portal.getValue());
}
// Extra - Using Collections methods
Collections.sort(spells);
System.out.println("\nSorted Library of Spells:");
for (String spell : spells) {
System.out.println(spell);
}
List sortedArtifacts = new ArrayList<>(artifacts);
Collections.sort(sortedArtifacts);
System.out.println("\nSorted Artifacts in the Sanctum Sanctorum:");
for (String artifact : sortedArtifacts) { System.out.println(artifact);
}
}
}
Happy coding, fellow sorcerers! ๐งโโ๏ธโจ