Omeshwar
@__omeshwar05__
Followers
25
Following
25K
Media
8
Statuses
19
If you found this thread helpful, please like, repost, and drop your feedback β it really helps! π Follow me for more coding tipsπ #Java #Coding #DevCommunity #Programmer #TechTwitter #LearnToCode
0
0
1
When to use Singleton: - Configuration manager - Logging framework - Single database connection pool - When NOT to use it: - When you need multiple instances - When global state causes unwanted coupling - When youβll need easy testing & mocking
0
0
2
β οΈ But be careful: Singleton is sometimes considered an anti-pattern. Why? It introduces global state, can make testing harder, and may lead to tight coupling. Use it only when it truly fits the scenario.
1
0
2
Serialization-Safe Singleton: readResolve() stops deserialization from creating new instance. Pros: Consistent instance Cons: Extra logic needed
1
0
2
Reflection-Safe Singleton: Throws exception if reflection tries to create 2nd object. Pros: More secure Cons: Still bypassable with tricks
1
0
2
Enum Singleton: JVM ensures single instance. Handles serialization + reflection. Pros: Most secure Cons: No lazy loading
1
0
2
Static Inner Class (Bill Pugh): Inner class loads only when needed β lazy + thread-safe. Pros: Cleanest + recommended Cons: None major
1
0
2
Double-Checked Locking (DCL): Checks twice β avoids locking every time. Pros: Fast + thread-safe Cons: More complex
1
0
2
Synchronized Method Singleton: Entire method is locked β thread-safe but slow. Pros: Safe Cons: Expensive locking
1
0
2
Lazy Initialization: Creates instance only on first call. Pros: Lazy, Instance is created only when used. Cons: Breaks in multi-threaded apps
1
0
2
Eager Initialization: Instance is created immediately when class loads. No lazy loading. Pros: Simple Cons: Wastes memory if unused
1
0
2
Letβs break down every Singleton pattern in Java with code, pros/cons + a short explanation of how each one works. Perfect for interviews + real-world apps. ππ #Java #DesignPatterns
1
0
2
Learning how things work under the hood makes coding way more fun! π‘ If you love exploring how core concepts like this actually work β β‘οΈ Follow me for more bite-sized technical content & dev insights! π
0
0
2
βοΈ Since Java 8, when a single bucket has too many elements, it switches from a LinkedList to a Red-Black Tree for faster lookups (O(log n) instead of O(n)) π² HashMap = elegant mix of array + hashing + tree π #Java #HashMap #Coding #TechLearning
1
0
2
To handle collisions: βοΈ Java compares keys using .equals() βοΈ If same key β overwrite value βοΈ If different key β add a new node to the linked list (or tree if many collisions)
1
0
2
Hereβs the magic πͺ When you call put(key, value) β 1οΈβ£ Java computes hash = key.hashCode() 2οΈβ£ It finds the bucket using index = hash % capacity 3οΈβ£ If that bucket is empty β store directly 4οΈβ£ If not β check for collisions (keys with same index)
1
0
2
A HashMap in Java stores key-value pairs β but under the hood, itβs powered by: πΉ Array of buckets πΉ Linked Lists (or Balanced Trees in some cases) Each keyβs position is decided by its hash code, which determines where it goes in memory.
1
0
2
Just learned how HashMap works internally in Java, and itβs fascinating! π§ π» Hereβs a simple breakdown for anyone who wants to really get it π
1
0
2
Hey @X π Iβm looking to #CONNECT with folks whoβre into: π¨βπ» Software Development β Java β‘ JavaScript π± Spring Boot βοΈ React π§© DSA π Full Stack Dev πΌ Freelancing π¨ Frontend π Backend π₯οΈ LeetCode Letβs build, learn and grow together πͺ #Coding #LearnInPublic #techtwitter
0
0
5