Skip to content

Commit 265ed9a

Browse files
Ranga Rao KaranamRanga Rao Karanam
authored andcommitted
More information in Readme
1 parent 7df9d31 commit 265ed9a

File tree

3 files changed

+94
-20
lines changed

3 files changed

+94
-20
lines changed

readme.md

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@
8383
```
8484

8585
##String vs StringBuffer vs StringBuilder
86-
- String is Immutable
87-
- StringBuilder is not synchronized
88-
- Performance
86+
- Immutability : String
87+
- Thread Safety : String(immutable), StringBuffer
88+
- Performance : StringBuilder (especially when a number of modifications are made.)
8989
- [Example 1](src/main/java/com/in28minutes/java/string/StringBufferBuilderExamples.java)
9090

9191
#OOPS Basics
@@ -94,18 +94,30 @@
9494
- Every Class Extends Object - See [Example](src/main/java/com/in28minutes/java/oops/inheritance/EveryClassExtendsObject.java)
9595
- [Example 1](src/main/java/com/in28minutes/java/oops/inheritance/InheritanceExamples.java)
9696
- Reuse Through Inheritance - [TestReuse.java](src/main/java/com/in28minutes/java/oops/inheritance/reuse/TestReuse.java) [Hero.java](src/main/java/com/in28minutes/java/oops/inheritance/reuse/Hero.java) [Actor.java](src/main/java/com/in28minutes/java/oops/inheritance/reuse/Actor.java) [Comedian.java](src/main/java/com/in28minutes/java/oops/inheritance/reuse/Comedian.java)
97+
- Example in Java Api : HashMap & TreeMap extend AbstractMap.
9798

9899
##Method Overloading
99-
- [Example](src/main/java/com/in28minutes/java/oops/inheritance/overloading/OverloadingRules.java)
100+
- Java Example
101+
- Constructors
102+
- public HashMap(int initialCapacity, float loadFactor)
103+
- public HashMap() {
104+
- public HashMap(int initialCapacity)
105+
- Methods
106+
- public boolean addAll(Collection<? extends E> c)
107+
- public boolean addAll(int index, Collection<? extends E> c)
108+
- [Rules](src/main/java/com/in28minutes/java/oops/inheritance/overloading/OverloadingRules.java)
100109

101110
##Method Overriding
111+
- Java Example
112+
- HashMap public int size() overrides AbstractMap public int size()
102113
- [Example](src/main/java/com/in28minutes/java/oops/inheritance/overriding/OverridingRules.java)
103114

104115
##Interface
105116
- An interface is a contract: the guy writing the interface says, "hey, I accept things looking that way"
106117
- Interface represents common actions between Multiple Classes.
107118
- Basic Example : [Flyable](src/main/java/com/in28minutes/java/oops/interfaces/Flyable.java)
108119
[Aeropane](src/main/java/com/in28minutes/java/oops/interfaces/Aeroplane.java) [Bird](src/main/java/com/in28minutes/java/oops/interfaces/Bird.java)
120+
- Example in Java api : Map interface, Collection interface.
109121
- Rules and Examples : [Rules](src/main/java/com/in28minutes/java/oops/interfaces/IntefaceRules.java) [More Examples](src/main/java/com/in28minutes/java/oops/interfaces/InterfaceExamples.java)
110122

111123
##Java and Multiple Inheritance
@@ -117,13 +129,14 @@
117129
- Abstract Class uses Inheritance and hence is an implementation of IS-A relationship between classes.
118130
- If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class.
119131
- Example - Animal, Cat, Dog
120-
- An example of an abstract class in the JDK is AbstractMap, which is part of the Collections Framework. Its subclasses (which include HashMap, TreeMap, and ConcurrentHashMap) share many methods (including get, put, isEmpty, containsKey, and containsValue) that AbstractMap defines.
132+
- An example of an abstract class in the JDK is AbstractMap, which is part of the Collections Framework. Its subclasses (which include HashMap, TreeMap, and ConcurrentHashMap) share many methods (including get, put, isEmpty, containsKey, and containsValue) that AbstractMap defines.
133+
- example abstract method : public abstract Set<Entry<K,V>> entrySet();
121134
- [Another Example - Spring AbstractController] (https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/AbstractController.java)
122135
- [More Details](docs/abstract-class.md)
123136

124137
##Abstract Class vs Interface
125138
- Real Difference - Apple vs Orange
126-
- Syntactical Differences For a Dumb Interviewer
139+
- Syntactical Differences
127140
- Methods and members of an abstract class can have any visibility. All methods of an interface must be public.
128141
- A concrete child class of an Abstract Class must define all the abstract methods. An Abstract child class can have abstract methods. An interface extending another interface need not provide default implementation for methods inherited from the parent interface.
129142
- A child class can only extend a single class. An interface can extend multiple interfaces. A class can implement multiple interfaces.
@@ -134,6 +147,10 @@
134147

135148
##Abstraction
136149
- Base of all programming
150+
- Example:
151+
- Consider simple statement : a = b + c;
152+
- Add two values stored in two memory locations and store it into a third memory location.
153+
- Actual operation involves registers, instruction sets, program counters etc. High level language we use abstracts those complex details.
137154

138155
##Encapsulation
139156
- [Example](src/main/java/com/in28minutes/java/oops/encapsulation/EncapsulationExample.java)
@@ -146,30 +163,38 @@
146163

147164
##Final Modifier
148165
- [Final Class](src/main/java/com/in28minutes/java/classmodifiers/nonaccess/finalclass/FinalClass.java)
166+
- Final Class examples : String, Integer, Double and other wrapper classes
149167
- [Final Member Variable](src/main/java/com/in28minutes/java/membermodifiers/nonaccess/FinalMemberModifiersExample.java)
168+
- Final Variable example : java.lang.Math.PI
169+
- Final Method example : java.lang.Math.PI
150170

151171
##Member Access Modifiers
152172
- [Examples](src/main/java/com/in28minutes/java/membermodifiers/access)
153173

154174
##Class Access Modifiers
155175
- [Examples](src/main/java/com/in28minutes/java/classmodifiers/)
156-
- TODO - A few classes missing here?????????????????
176+
- public - Accessible from everywhere.
177+
- private and protected cannot be used on a class.
157178

158179
##Static Modifier
159180
- [Example](src/main/java/com/in28minutes/java/membermodifiers/nonaccess/StaticModifierExamples.java)
181+
- Examples in Java Api : Methods in Math class.
160182

161183
#Constructors
162184

163185
##Constructors
164186
- [Example](src/main/java/com/in28minutes/java/object/constructors/ConstructorExamples.java)
187+
- Examples in Java Api : HashMap has 2 constructors
165188

166189
##Static Initializers
167190
- [Example](src/main/java/com/in28minutes/java/initializers/InitializerExamples.java)
168191

169192
#Collections
170193

171194
##Why do we need Collections?
172-
- Arrays are static in Size.
195+
- Arrays are not resizable.
196+
- Java Collections Framework allows you to use the right data structure, because one size does not fit all.
197+
- Java Collections Framework provides the correct abstractions, List, Map, Set
173198

174199
##Hierarchy of Collection Interface?
175200
- [Hierarchy](src/main/java/com/in28minutes/java/collections/CollectionHierarchy.java)
@@ -202,24 +227,25 @@
202227
- [All Collections Examples](src/main/java/com/in28minutes/java/collections/examples/CollectionExamples.java)
203228

204229
##Concurrency in Collections
205-
- TODO - More Code Examples
206230
- Synchronized collections are implemented using synchronized methods and synchronized blocks. Only one thread can executing any of the synchronized code at a given point in time. This places severe restrictions on the concurrency of threads – thereby affecting performance of the application. All the pre Java 5 synchronized collections (HashTable & Vector, for example) use this approach. Post Java 5, collections using new approaches to synchronization are available in Java. These are called concurrent collections. More details below.
231+
- When many threads are expected to access a given collection, a ConcurrentHashMap is normally preferable to a synchronized HashMap, and a ConcurrentSkipListMap is normally preferable to a synchronized TreeMap. A CopyOnWriteArrayList is preferable to a synchronized ArrayList when the expected number of reads and traversals greatly outnumber the number of updates to a list.
207232
- Copy on Write
208233
- All values in collection are stored in an internal immutable (not-changeable) array. A new array is created if there is any modification to the collection.
209234
- Read operations are not synchronized. Only write operations are synchronized.
210235
- Copy on Write approach is used in scenarios where reads greatly out number write’s on a collection. CopyOnWriteArrayList & CopyOnWriteArraySet are implementations of this approach. Copy on Write collections are typically used in Subject – Observer scenarios, where the observers very rarely change. Most frequent operations would be iterating around the observers and notifying them.
211-
- Compare and Swap
212-
- Compare and Swap is one of the new approaches (Java 5) introduced in java to handle synchronization. In traditional approach, a method which modifies a member variable used by multiple threads is completely synchronized – to prevent other threads accessing stale value.
213-
- In compare and swap approach, instead of synchronizing entire method, the value of the member variable before calculation is cached. After the calculation, the cached value is compared with the current value of member variable. If the value is not modified, the calculated result is stored into the member variable. If another thread has modified the value, then the calculation can be performed again. Or skipped – as the need might be.
214-
- ConcurrentLinkedQueue uses this approach.
236+
- Example : CopyOnWriteArrayList : public boolean add(E e)
215237
- Locks
238+
- CopyOnWriteArrayList : final ReentrantLock lock = this.lock;
216239
- When 10 methods are declared as synchronized, only one of them is executed by any of the threads at any point in time. This has severe performance impact.
217240
- Another new approach introduced in Java 5 is to use lock and unlock methods. Lock and unlock methods are used to divide methods into different blocks and help enhance concurrency. The 10 methods can be divided into different blocks, which can be synchronized based on different variables.
218241

219-
##Fail Safe vs Fail Fast
220-
- TODO - Code Examples
221-
- Fail Fast Iterators throw a ConcurrentModificationException if there is a modification to the underlying collection is modified. This was the default behavior of the synchronized collections of pre Java 5 age.
222-
- Fail Safe Iterators do not throw exceptions even when there are changes in the collection. This is the default behavior of the concurrent collections, introduced since Java 5.
242+
##Concurrent Modification & Fail Safe vs Fail Fast
243+
- Concurrent Modification : Changes made to collection when one or more threads are iterating over it.
244+
- Fail Fast : Throws Concurrent Modification Exception if there are Concurrent Modifications
245+
- Fail Safe : Fail Safe Iterator makes copy of the internal data structure (object array) and iterates over the copied data structure.
246+
- Fail Safe is efficient when traversal operations vastly outnumber mutations
247+
- [FailFast](src/main/java/com/in28minutes/java/collections/FailFast.java)
248+
- [FailSafe](src/main/java/com/in28minutes/java/collections/FailSafe.java)
223249

224250
##Atomic Operations
225251
- Atomic Access Java Tutorial states “In programming, an atomic action is one that effectively happens all at once. An atomic action cannot stop in the middle: it either happens completely, or it doesn't happen at all. No side effects of an atomic action are visible until the action is complete”.
@@ -250,9 +276,6 @@
250276
## New Exception Handling Features
251277
- TODO - Code Examples
252278

253-
## Puzzles on Exception Handling
254-
- TODO
255-
256279
## Hierarchy of Exception Related Classes
257280
- [Example 1](src/main/java/com/in28minutes/java/exceptionhandling/ExceptionHandlingExample1.java)
258281
- [Example 2](src/main/java/com/in28minutes/java/exceptionhandling/ExceptionHandlingExample2.java)
@@ -378,6 +401,10 @@ obj2.hashCode(). Two unequal objects MIGHT have the same hashCode.
378401
##Lambda Expressions
379402
- TODO
380403

404+
## 20 Puzzles to Test Yourselves
405+
- TODO
406+
407+
381408
#Others
382409
- [Basics of Objects and Classes](docs/basics-class-object.md)
383410
- [Arrays](docs/arrays.md)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.in28minutes.java.collections;
2+
3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
import java.util.Map;
6+
7+
public class FailFast {
8+
9+
public static void main(String[] args) {
10+
Map<String, String> map = new HashMap<String, String>();
11+
map.put("key1", "value1");
12+
map.put("key2", "value2");
13+
map.put("key3", "value3");
14+
15+
Iterator<String> iterator = map.keySet().iterator();
16+
17+
while (iterator.hasNext()) {
18+
System.out.println(map.get(iterator.next()));
19+
map.put("key4", "value4");
20+
}
21+
22+
}
23+
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.in28minutes.java.collections;
2+
3+
import java.util.Iterator;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
6+
public class FailSafe {
7+
8+
public static void main(String[] args) {
9+
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>();
10+
map.put("key1", "value1");
11+
map.put("key2", "value2");
12+
map.put("key3", "value3");
13+
14+
Iterator<String> iterator = map.keySet().iterator();
15+
16+
while (iterator.hasNext()) {
17+
System.out.println(map.get(iterator.next()));
18+
map.put("key4", "value4");
19+
}
20+
21+
}
22+
23+
}

0 commit comments

Comments
 (0)