tag:blogger.com,1999:blog-12351105269793744902024-02-08T03:12:17.841+05:30WELCOME TO JAVA FAQSUnknownnoreply@blogger.comBlogger365125tag:blogger.com,1999:blog-1235110526979374490.post-51929071231832685152018-06-07T21:15:00.001+05:302018-06-08T00:15:11.870+05:30Web Socket using Spring and Stomp<div dir="ltr" style="text-align: left;" trbidi="on">
WebSocket is used for bidirectional communication between the server and the client.<br />
We need this when there is a need to two directional communication. HTTP is single directional.<br />
Sometimes you might need the server to push data to the client without client request which is not possible via http. Now days websocket is supported in all the browsers.<br />
<br />
Stomp is a text oriented messaging protocol. It supports fallback options for websocket.<br />
<br />
Example Angular 6 and Spring:<br />
<br />
We will be using @stomp/ng2-stompjs. npm install --save @stomp/ng2-stompjs<br />
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">StompConfig</span>, <span style="color: #9cdcfe;">StompService</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'@stomp/ng2-stompjs'</span>;</div>
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">WebSocketConfig</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'./websocket.config'</span>;</div>
<br />
<br />
<div>
<span style="color: #c586c0;">export</span> <span style="color: #569cd6;">const</span> <span style="color: #9cdcfe;">stompConfig</span>: <span style="color: #4ec9b0;">StompConfig</span> = {</div>
<div>
<span style="color: #9cdcfe;">url:</span> <span style="color: #9cdcfe;">WebSocketConfig</span>.<span style="color: #9cdcfe;">uri</span>,</div>
<div>
<span style="color: #9cdcfe;">headers:</span> {</div>
<div>
},</div>
<div>
<span style="color: #9cdcfe;">heartbeat_in:</span> <span style="color: #b5cea8;">0</span>, </div>
<div>
<span style="color: #9cdcfe;">heartbeat_out:</span> <span style="color: #b5cea8;">20000</span>,</div>
<div>
<span style="color: #9cdcfe;">reconnect_delay:</span> <span style="color: #b5cea8;">5000</span>,</div>
<div>
<span style="color: #9cdcfe;">debug:</span> <span style="color: #569cd6;">false</span></div>
<div>
};</div>
</div>
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #c586c0;">export</span> <span style="color: #569cd6;">class</span> <span style="color: #4ec9b0;">WebSocketConfig</span>{</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #569cd6;">static</span> <span style="color: #9cdcfe;">uri</span>:<span style="color: #4ec9b0;">string</span> = <span style="color: #ce9178;">'ws://localhost<ip>:8080<port>/testws'</port></ip></span>;</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #569cd6;">static</span> <span style="color: #9cdcfe;">topic</span>:<span style="color: #4ec9b0;">string</span> = <span style="color: #ce9178;">"/topic/hello"</span>;</div>
<div>
}</div>
</div>
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">Injectable</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'@angular/core'</span>;</div>
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">Subscription</span>, <span style="color: #9cdcfe;">Observable</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'rxjs'</span>;</div>
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">Message</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'@stomp/stompjs'</span>;</div>
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">StompService</span>, <span style="color: #9cdcfe;">StompState</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'@stomp/ng2-stompjs'</span>;</div>
<div>
<span style="color: #c586c0;">import</span> {<span style="color: #9cdcfe;">map</span>} <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'rxjs/operators'</span>;</div>
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">WebSocketConfig</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'./websocket.config'</span>;</div>
<br />
<div>
@<span style="color: #dcdcaa;">Injectable</span>({</div>
<div>
<span style="color: #9cdcfe;">providedIn:</span> <span style="color: #ce9178;">'root'</span></div>
<div>
})</div>
<div>
<span style="color: #c586c0;">export</span> <span style="color: #569cd6;">class</span> <span style="color: #4ec9b0;">WebsocketService</span> {</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #9cdcfe;">message</span>: <span style="color: #4ec9b0;">Observable</span><<span style="color: #4ec9b0;">Message</span>>;</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #9cdcfe;">wsstate</span>: <span style="color: #4ec9b0;">Observable</span><<span style="color: #4ec9b0;">string</span>>;</div>
<br />
<div>
<span style="color: #569cd6;">constructor</span>(<span style="color: #569cd6;">private</span> <span style="color: #9cdcfe;">stompService</span>: <span style="color: #4ec9b0;">StompService</span>) { }</div>
<br />
<div>
<span style="color: #569cd6;">public</span> <span style="color: #dcdcaa;">connectWebSocket</span>() {</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">wsstate</span> = <span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">stompService</span>.<span style="color: #9cdcfe;">state</span>.<span style="color: #dcdcaa;">pipe</span>(<span style="color: #dcdcaa;">map</span>((<span style="color: #9cdcfe;">state</span>: <span style="color: #4ec9b0;">number</span>) <span style="color: #569cd6;">=></span> <span style="color: #9cdcfe;">StompState</span>[<span style="color: #9cdcfe;">state</span>]));</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">message</span> = <span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">stompService</span>.<span style="color: #dcdcaa;">subscribe</span>(<span style="color: #9cdcfe;">WebSocketConfig</span>.<span style="color: #9cdcfe;">topic</span>);</div>
<div>
}</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #dcdcaa;">getSocketDataObservable</span>(){</div>
<div>
<span style="color: #c586c0;">return</span> <span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">message</span>;</div>
<div>
}</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #dcdcaa;">getSocketStateObservable</span>(){</div>
<div>
<span style="color: #c586c0;">return</span> <span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">wsstate</span>;</div>
<div>
}</div>
<div>
}</div>
</div>
<br />
Add this to the app.module.ts<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #9cdcfe;">providers:</span> [<span style="color: #9cdcfe;">StompService</span>,</div>
<div>
{</div>
<div>
<span style="color: #9cdcfe;">provide:</span> <span style="color: #9cdcfe;">StompConfig</span>,</div>
<div>
<span style="color: #9cdcfe;">useValue:</span> <span style="color: #9cdcfe;">stompConfig</span></div>
<div>
}],</div>
</div>
This component will be consuming the data.<br />
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">Component</span>,<span style="color: #9cdcfe;">OnInit</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'@angular/core'</span>;</div>
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">Subscription</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'rxjs'</span>;</div>
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">Message</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'@stomp/stompjs'</span>;</div>
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">WebsocketService</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'./ws/websocket.service'</span>;</div>
<div>
<span style="color: #c586c0;">import</span> { <span style="color: #9cdcfe;">WsData</span> } <span style="color: #c586c0;">from</span> <span style="color: #ce9178;">'./ws/wsdata.model'</span>;</div>
<br />
<div>
@<span style="color: #dcdcaa;">Component</span>({</div>
<div>
<span style="color: #9cdcfe;">selector:</span> <span style="color: #ce9178;">'app-root'</span>,</div>
<div>
<span style="color: #9cdcfe;">templateUrl:</span> <span style="color: #ce9178;">'./app.component.html'</span>,</div>
<div>
<span style="color: #9cdcfe;">styleUrls:</span> [<span style="color: #ce9178;">'./app.component.css'</span>],</div>
<div>
<span style="color: #9cdcfe;">viewProviders:</span> [<span style="color: #9cdcfe;">WebsocketService</span>]</div>
<div>
})</div>
<div>
<span style="color: #c586c0;">export</span> <span style="color: #569cd6;">class</span> <span style="color: #4ec9b0;">AppComponent</span> <span style="color: #569cd6;">implements</span> <span style="color: #4ec9b0;">OnInit</span>{</div>
<div>
<span style="color: #9cdcfe;">title</span> = <span style="color: #ce9178;">'app'</span>;</div>
<div>
<span style="color: #569cd6;">private</span> <span style="color: #9cdcfe;">datasubscription</span>: <span style="color: #4ec9b0;">Subscription</span>;</div>
<div>
<span style="color: #569cd6;">private</span> <span style="color: #9cdcfe;">statesubscription</span>: <span style="color: #4ec9b0;">Subscription</span>;</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #9cdcfe;">uiData</span> : <span style="color: #4ec9b0;">WsData</span>;</div>
<div>
</div>
<div>
<span style="color: #569cd6;">constructor</span>(<span style="color: #569cd6;">private</span> <span style="color: #9cdcfe;">websocketService</span>:<span style="color: #4ec9b0;">WebsocketService</span>) { }</div>
<div>
</div>
<div>
<span style="color: #dcdcaa;">ngOnInit</span>() {</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">websocketService</span>.<span style="color: #dcdcaa;">connectWebSocket</span>();</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">datasubscription</span> = <span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">websocketService</span>.<span style="color: #dcdcaa;">getSocketDataObservable</span>().<span style="color: #dcdcaa;">subscribe</span>(<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">onData</span>);</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">statesubscription</span> = <span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">websocketService</span>.<span style="color: #dcdcaa;">getSocketStateObservable</span>().<span style="color: #dcdcaa;">subscribe</span>(<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">onStateChange</span>);</div>
<div>
}</div>
<div>
<span style="color: #569cd6;">private</span> <span style="color: #dcdcaa;">onData</span> = (<span style="color: #9cdcfe;">message</span>: <span style="color: #4ec9b0;">Message</span>) <span style="color: #569cd6;">=></span> {</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">uiData</span> = <span style="color: #4ec9b0;">JSON</span>.<span style="color: #dcdcaa;">parse</span>(<span style="color: #9cdcfe;">message</span>.<span style="color: #9cdcfe;">body</span>);</div>
<div>
}</div>
<div>
<span style="color: #569cd6;">private</span> <span style="color: #dcdcaa;">onStateChange</span> = (<span style="color: #9cdcfe;">state</span>: <span style="color: #4ec9b0;">String</span>) <span style="color: #569cd6;">=></span> {</div>
<div>
<span style="color: #4ec9b0;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">'WS connection state changed '</span>+<span style="color: #9cdcfe;">state</span>);</div>
<div>
}</div>
<div>
<span style="color: #dcdcaa;">ngOnDestroy</span>() {</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">datasubscription</span>.<span style="color: #dcdcaa;">unsubscribe</span>();</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">statesubscription</span>.<span style="color: #dcdcaa;">unsubscribe</span>();</div>
<div>
}</div>
<div>
}</div>
</div>
<br />
Spring code:<br />
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #569cd6;">import</span> org.springframework.context.annotation.Configuration;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.messaging.simp.config.MessageBrokerRegistry;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.socket.config.annotation.StompEndpointRegistry;</div>
<br />
<div>
<span style="color: #4ec9b0;">@Configuration</span></div>
<div>
<span style="color: #4ec9b0;">@EnableWebSocketMessageBroker</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #569cd6;">class</span> <span style="color: #4ec9b0;">WebSocketConfig</span> <span style="color: #569cd6;">extends</span> <span style="color: #4ec9b0;">AbstractWebSocketMessageBrokerConfigurer</span> {</div>
<div>
<span style="color: #4ec9b0;">@Override</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">configureMessageBroker</span>(<span style="color: #4ec9b0;">MessageBrokerRegistry</span> <span style="color: #9cdcfe;">config</span>) {</div>
<div>
<span style="color: #9cdcfe;">config</span>.<span style="color: #dcdcaa;">enableSimpleBroker</span>(<span style="color: #ce9178;">"/topic"</span>);</div>
<div>
<span style="color: #9cdcfe;">config</span>.<span style="color: #dcdcaa;">setApplicationDestinationPrefixes</span>(<span style="color: #ce9178;">"/test"</span>);</div>
<div>
}</div>
<div>
<span style="color: #4ec9b0;">@Override</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">registerStompEndpoints</span>(<span style="color: #4ec9b0;">StompEndpointRegistry</span> <span style="color: #9cdcfe;">registry</span>) {</div>
<div>
<span style="color: #9cdcfe;">registry</span>.<span style="color: #dcdcaa;">addEndpoint</span>(<span style="color: #ce9178;">"/testws"</span>).<span style="color: #dcdcaa;">setAllowedOrigins</span>(<span style="color: #ce9178;">"*"</span>);</div>
<div>
} </div>
<div>
}</div>
</div>
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #569cd6;">import</span> java.util.logging.Logger;</div>
<div>
<span style="color: #569cd6;">import</span> javax.annotation.PostConstruct;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.beans.factory.annotation.Autowired;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.messaging.simp.SimpMessagingTemplate;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.stereotype.Component;</div>
<div>
<span style="color: #4ec9b0;">@Component</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #569cd6;">class</span> <span style="color: #4ec9b0;">WebSocketController</span>{</div>
<div>
<span style="color: #569cd6;">private</span> <span style="color: #569cd6;">static</span> <span style="color: #569cd6;">final</span> <span style="color: #4ec9b0;">Logger</span> <span style="color: #9cdcfe;">LOGGER</span> = <span style="color: #9cdcfe;">Logger</span>.<span style="color: #dcdcaa;">getLogger</span>(<span style="color: #9cdcfe;">WebSocketController</span>.<span style="color: #9cdcfe;">class</span>.<span style="color: #dcdcaa;">getName</span>());</div>
<div>
<span style="color: #4ec9b0;">int</span> <span style="color: #9cdcfe;">time</span> = <span style="color: #b5cea8;">10</span>;</div>
<div>
<span style="color: #4ec9b0;">@Autowired</span></div>
<div>
<span style="color: #569cd6;">private</span> <span style="color: #4ec9b0;">SimpMessagingTemplate</span> <span style="color: #9cdcfe;">template</span>;</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">publishWebSocket</span>(<span style="color: #4ec9b0;">String</span> <span style="color: #9cdcfe;">data</span>){</div>
<div>
<span style="color: #9cdcfe;">LOGGER</span>.<span style="color: #dcdcaa;">info</span>(<span style="color: #ce9178;">"Publis the result : "</span> + data);</div>
<div>
<span style="color: #9cdcfe;">template</span>.<span style="color: #dcdcaa;">convertAndSend</span>(<span style="color: #ce9178;">"/topic/hello"</span>, data);</div>
<div>
}</div>
<div>
}</div>
</div>
<br />
With these two configuration our webscolet is ready. Send some data to the client in each interval.<br />
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #569cd6;">import</span> javax.annotation.PostConstruct;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.beans.factory.annotation.Autowired;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.messaging.simp.SimpMessagingTemplate;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.stereotype.Component;</div>
<br />
<div>
<span style="color: #4ec9b0;">@Component</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #569cd6;">class</span> <span style="color: #4ec9b0;">WebSocketController</span> <span style="color: #569cd6;">implements</span> <span style="color: #4ec9b0;">Runnable</span>{</div>
<div>
<span style="color: #4ec9b0;">@Autowired</span></div>
<div>
<span style="color: #569cd6;">private</span> <span style="color: #4ec9b0;">SimpMessagingTemplate</span> <span style="color: #9cdcfe;">template</span>;</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">startWebSocket</span>(){</div>
<div>
<span style="color: #c586c0;">new</span> <span style="color: #dcdcaa;">Thread</span>(<span style="color: #569cd6;">this</span>).<span style="color: #dcdcaa;">start</span>();</div>
<div>
}</div>
<div>
<span style="color: #4ec9b0;">@PostConstruct</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">postConstruct</span>() {</div>
<div>
<span style="color: #9cdcfe;">System</span>.<span style="color: #9cdcfe;">out</span>.<span style="color: #dcdcaa;">println</span>(<span style="color: #ce9178;">"Start websocket"</span>);</div>
<div>
<span style="color: #dcdcaa;">startWebSocket</span>();</div>
<div>
}</div>
<div>
<span style="color: #4ec9b0;">@Override</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">run</span>() {</div>
<div>
<span style="color: #4ec9b0;">int</span> <span style="color: #9cdcfe;">data</span> = <span style="color: #b5cea8;">10</span>;</div>
<div>
<span style="color: #c586c0;">while</span> (<span style="color: #569cd6;">true</span>){</div>
<div>
<span style="color: #c586c0;">try</span> {</div>
<div>
<span style="color: #9cdcfe;">Thread</span>.<span style="color: #dcdcaa;">sleep</span>(<span style="color: #b5cea8;">5000</span>);</div>
<div>
} <span style="color: #c586c0;">catch</span> (<span style="color: #4ec9b0;">InterruptedException</span> <span style="color: #9cdcfe;">e</span>) {</div>
<div>
<span style="color: #9cdcfe;">e</span>.<span style="color: #dcdcaa;">printStackTrace</span>();</div>
<div>
}</div>
<div>
i++;</div>
<div>
<span style="color: #9cdcfe;">template</span>.<span style="color: #dcdcaa;">convertAndSend</span>(<span style="color: #ce9178;">"/topic/hello"</span>, data);</div>
<div>
}</div>
<div>
}</div>
<div>
}</div>
</div>
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-71834006603457996892018-06-07T07:16:00.001+05:302018-06-07T20:26:17.104+05:30What is a web socket with example<div dir="ltr" style="text-align: left;" trbidi="on">
WebSocket is used for bidirectional communication between the server and the client.<br />
We need this when there is a need to two directional communication. HTTP is single directional.<br />
Sometimes you might need the server to push data to the client without client request which is not possible via http. Now days websocket is supported in all the browsers.<br />
<br />
Here is an example using Spring and Javascript. This is plain web socket withot using Stomp and SockJS.<br />
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #dcdcaa;">connectWebsocketplain</span>(){</div>
<div>
<span style="color: #9cdcfe;">ws</span>: <span style="color: #4ec9b0;">WebSocket</span>;</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">ws</span> = <span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">WebSocket</span>(<span style="color: #ce9178;">'ws://localhost:8080/name'</span>);</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">ws</span>.<span style="color: #dcdcaa;">onopen</span> = <span style="color: #569cd6;">function</span> () {</div>
<div>
<span style="color: #4ec9b0;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">'on open connection'</span>)</div>
<div>
};</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">ws</span>.<span style="color: #dcdcaa;">onmessage</span> = <span style="color: #569cd6;">function</span> (<span style="color: #9cdcfe;">event</span>: <span style="color: #4ec9b0;">any</span>) {</div>
<div>
<span style="color: #4ec9b0;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">'on message'</span> + <span style="color: #9cdcfe;">event</span>.<span style="color: #9cdcfe;">data</span>)</div>
<div>
};</div>
<div>
<span style="color: #569cd6;">this</span>.<span style="color: #9cdcfe;">ws</span>.<span style="color: #dcdcaa;">onclose</span> = <span style="color: #569cd6;">function</span> () {</div>
<div>
<span style="color: #4ec9b0;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">"Connection is closed..."</span>);</div>
<div>
};</div>
<div>
}</div>
</div>
<br />
Spring code for websocket<br />
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #569cd6;">import</span> java.io.IOException;</div>
<div>
<span style="color: #569cd6;">import</span> java.util.List;</div>
<div>
<span style="color: #569cd6;">import</span> java.util.concurrent.CopyOnWriteArrayList;</div>
<br />
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.socket.TextMessage;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.socket.WebSocketSession;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.socket.handler.TextWebSocketHandler;</div>
<br />
<div>
<span style="color: #569cd6;">public</span> <span style="color: #569cd6;">class</span> <span style="color: #4ec9b0;">SocketHandler</span> <span style="color: #569cd6;">extends</span> <span style="color: #4ec9b0;">TextWebSocketHandler</span> {</div>
<br />
<div>
<span style="color: #4ec9b0;">List</span><<span style="color: #4ec9b0;">WebSocketSession</span>> <span style="color: #9cdcfe;">sessions</span> = <span style="color: #c586c0;">new</span> <span style="color: #4ec9b0;">CopyOnWriteArrayList</span><>();</div>
<div>
<span style="color: #4ec9b0;">@Override</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">handleTextMessage</span>(<span style="color: #4ec9b0;">WebSocketSession</span> <span style="color: #9cdcfe;">session</span>, <span style="color: #4ec9b0;">TextMessage</span> <span style="color: #9cdcfe;">message</span>)</div>
<div>
<span style="color: #569cd6;">throws</span> InterruptedException, IOException {</div>
<div>
<span style="color: #c586c0;">for</span> (<span style="color: #4ec9b0;">WebSocketSession</span> webSocketSession <span style="color: #c586c0;">:</span> sessions) {</div>
<div>
<span style="color: #9cdcfe;">webSocketSession</span>.<span style="color: #dcdcaa;">sendMessage</span>(<span style="color: #c586c0;">new</span> <span style="color: #dcdcaa;">TextMessage</span>(<span style="color: #ce9178;">"Hello Client"</span>+<span style="color: #9cdcfe;">System</span>.<span style="color: #dcdcaa;">currentTimeMillis</span>()));</div>
<div>
}</div>
<div>
}</div>
<div>
<span style="color: #4ec9b0;">@Override</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">afterConnectionEstablished</span>(<span style="color: #4ec9b0;">WebSocketSession</span> <span style="color: #9cdcfe;">session</span>) <span style="color: #569cd6;">throws</span> Exception {</div>
<div>
<span style="color: #608b4e;">// the messages will be broadcasted to all users.</span></div>
<div>
<span style="color: #9cdcfe;">sessions</span>.<span style="color: #dcdcaa;">add</span>(session);</div>
<div>
}</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">sendToClient</span>() {</div>
<div>
<span style="color: #c586c0;">for</span> (<span style="color: #4ec9b0;">WebSocketSession</span> webSocketSession <span style="color: #c586c0;">:</span> sessions) {</div>
<div>
<span style="color: #c586c0;">try</span> {</div>
<div>
<span style="color: #c586c0;">if</span> (<span style="color: #9cdcfe;">webSocketSession</span>.<span style="color: #dcdcaa;">isOpen</span>()) {</div>
<div>
<span style="color: #9cdcfe;">webSocketSession</span>.<span style="color: #dcdcaa;">sendMessage</span>(<span style="color: #c586c0;">new</span> <span style="color: #dcdcaa;">TextMessage</span>(<span style="color: #ce9178;">"Hello "</span>+<span style="color: #9cdcfe;">System</span>.<span style="color: #dcdcaa;">currentTimeMillis</span>()));</div>
<div>
}</div>
<div>
} <span style="color: #c586c0;">catch</span> (<span style="color: #4ec9b0;">IOException</span> <span style="color: #9cdcfe;">e</span>) {</div>
<div>
<span style="color: #9cdcfe;">e</span>.<span style="color: #dcdcaa;">printStackTrace</span>();</div>
<div>
}</div>
<div>
}</div>
<div>
}</div>
<div>
}</div>
</div>
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #569cd6;">import</span> org.springframework.context.annotation.Bean;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.context.annotation.Configuration;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.socket.config.annotation.EnableWebSocket;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.socket.config.annotation.WebSocketConfigurer;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;</div>
<br />
<div>
<span style="color: #4ec9b0;">@Configuration</span></div>
<div>
<span style="color: #4ec9b0;">@EnableWebSocket</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #569cd6;">class</span> <span style="color: #4ec9b0;">SocketConfig</span> <span style="color: #569cd6;">implements</span> <span style="color: #4ec9b0;">WebSocketConfigurer</span> {</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">registerWebSocketHandlers</span>(<span style="color: #4ec9b0;">WebSocketHandlerRegistry</span> <span style="color: #9cdcfe;">registry</span>) {</div>
<div>
<span style="color: #9cdcfe;">registry</span>.<span style="color: #dcdcaa;">addHandler</span>(<span style="color: #dcdcaa;">socketHandler</span>(), <span style="color: #ce9178;">"/name"</span>).<span style="color: #dcdcaa;">setAllowedOrigins</span>(<span style="color: #ce9178;">"*"</span>);</div>
<div>
}</div>
<div>
<span style="color: #4ec9b0;">@Bean</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">SocketHandler</span> <span style="color: #dcdcaa;">socketHandler</span>() {</div>
<div>
<span style="color: #c586c0;">return</span> <span style="color: #c586c0;">new</span> <span style="color: #dcdcaa;">SocketHandler</span>();</div>
<div>
}</div>
<div>
}</div>
</div>
<br />
Schedular to publish task in interval<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #569cd6;">import</span> org.springframework.beans.factory.annotation.Autowired;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.scheduling.annotation.Scheduled;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.stereotype.Service;</div>
<br />
<div>
<span style="color: #4ec9b0;">@Service</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #569cd6;">class</span> <span style="color: #4ec9b0;">ScheduleTaskSoket</span> {</div>
<div>
<span style="color: #4ec9b0;">@Autowired</span></div>
<div>
<span style="color: #569cd6;">private</span> <span style="color: #4ec9b0;">SocketHandler</span> <span style="color: #9cdcfe;">handler</span>;</div>
<div>
<span style="color: #4ec9b0;">@Scheduled</span>(fixedRate = <span style="color: #b5cea8;">5000</span>)</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">trigger</span>() {</div>
<div>
<span style="color: #9cdcfe;">handler</span>.<span style="color: #dcdcaa;">sendToClient</span>();</div>
<div>
}</div>
<div>
}</div>
</div>
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-52400647506701299612018-06-07T06:36:00.001+05:302018-06-07T06:45:27.405+05:30What is server sent Events (SSE)<div dir="ltr" style="text-align: left;" trbidi="on">
SSE (Server Sent Events) are the one directional communication from server to client, supported by most recent browsers. It uses plain HTTP protocol for communication.<br />
<br />
SSE vs WebSockets<br />
<br />
<b>SSE</b><br />
- One directional communication. Service to browser<br />
- Light weight than websocket<br />
- Transported over simple HTTP instead of a custom protocol<br />
- Can be poly-filled with javascript to "backport" SSE to browsers that do not support it yet.<br />
- Built in support for automatic re-connection and event-id<br />
- Allows only 6 open connections<br />
- No binary support<br />
<br />
<b>Web socket</b><br />
- Two directional communication<br />
- Allows unlimited open connections<br />
- Native support in more browsers<br />
<br />
<br />
Simple SSE commucation code beween Spring and JavaScript<br />
<br />
We will connect the SSE with the end point /hello<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #dcdcaa;">connect</span>(){</div>
<div>
<span style="color: #4ec9b0;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">"connect"</span>);<br />
<span style="color: #569cd6;">let</span> <span style="color: #9cdcfe;">source</span> = <span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">EventSource</span>(<span style="color: #ce9178;">"http://localhost:8080/hello"</span>);<br />
<span style="color: #9cdcfe;">source</span>.<span style="color: #dcdcaa;">addEventListener</span>(<span style="color: #ce9178;">'spring'</span>,<span style="color: #569cd6;">function</span>(<span style="color: #9cdcfe;">event</span>:<span style="color: #4ec9b0;">any</span>){</div>
<div>
<span style="color: #4ec9b0;">console</span>.<span style="color: #dcdcaa;">log</span>(<span style="color: #ce9178;">'------------'</span>+<span style="color: #9cdcfe;">event</span>.<span style="color: #9cdcfe;">data</span>);<br />
});<br />
}</div>
</div>
<br />
<br />
Spring controller class which handles the SSE.<br />
Responsible for creating the connection and sends the message to all the connected client in each interval.<br />
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
<span style="color: #569cd6;">import</span> java.io.IOException;</div>
<div>
<span style="color: #569cd6;">import</span> java.util.List;</div>
<div>
<span style="color: #569cd6;">import</span> java.util.concurrent.CopyOnWriteArrayList;</div>
<br />
<div>
<span style="color: #569cd6;">import</span> javax.annotation.PostConstruct;</div>
<br />
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.bind.annotation.RequestMapping;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.bind.annotation.RestController;</div>
<div>
<span style="color: #569cd6;">import</span> org.springframework.web.servlet.mvc.method.annotation.SseEmitter;</div>
<br />
<div>
<span style="color: #4ec9b0;">@RestController</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #569cd6;">class</span> <span style="color: #4ec9b0;">SSEHandler</span> <span style="color: #569cd6;">implements</span> <span style="color: #4ec9b0;">Runnable</span>{</div>
<br />
<div>
<span style="color: #569cd6;">private</span> <span style="color: #4ec9b0;">List</span><<span style="color: #4ec9b0;">SseEmitter</span>> <span style="color: #9cdcfe;">emitters</span> = <span style="color: #c586c0;">new</span> <span style="color: #4ec9b0;">CopyOnWriteArrayList</span><>();</div>
<br />
<div>
<span style="color: #4ec9b0;">@RequestMapping</span>(<span style="color: #ce9178;">"/hello"</span>)</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">SseEmitter</span> <span style="color: #dcdcaa;">subscribe</span>() {</div>
<div>
<span style="color: #4ec9b0;">SseEmitter</span> <span style="color: #9cdcfe;">emitter</span> = <span style="color: #c586c0;">new</span> <span style="color: #dcdcaa;">SseEmitter</span>();</div>
<div>
<span style="color: #9cdcfe;">emitters</span>.<span style="color: #dcdcaa;">add</span>(emitter);</div>
<div>
<span style="color: #c586c0;">return</span> emitter;</div>
<div>
}</div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">send</span>(<span style="color: #4ec9b0;">String</span> <span style="color: #9cdcfe;">data</span>) <span style="color: #569cd6;">throws</span> IOException {</div>
<div>
<span style="color: #c586c0;">for</span> (<span style="color: #4ec9b0;">SseEmitter</span> emitter <span style="color: #c586c0;">:</span> emitters) {</div>
<div>
<span style="color: #9cdcfe;">emitter</span>.<span style="color: #dcdcaa;">send</span>(<span style="color: #9cdcfe;">SseEmitter</span>.<span style="color: #dcdcaa;">event</span>().<span style="color: #dcdcaa;">name</span>(<span style="color: #ce9178;">"spring"</span>).<span style="color: #dcdcaa;">data</span>(data));</div>
<div>
}</div>
<div>
}</div>
<div>
<span style="color: #4ec9b0;">@Override</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">run</span>() {</div>
<div>
<span style="color: #c586c0;">while</span> (<span style="color: #569cd6;">true</span>) {</div>
<div>
<span style="color: #c586c0;">try</span> {</div>
<div>
<span style="color: #9cdcfe;">Thread</span>.<span style="color: #dcdcaa;">sleep</span>(<span style="color: #b5cea8;">5000</span>);</div>
<div>
<span style="color: #dcdcaa;">send</span>(<span style="color: #ce9178;">"Hello Client "</span>+<span style="color: #9cdcfe;">System</span>.<span style="color: #dcdcaa;">currentTimeMillis</span>());</div>
<div>
} <span style="color: #c586c0;">catch</span> (<span style="color: #4ec9b0;">Exception</span> <span style="color: #9cdcfe;">e</span>) {</div>
<div>
<span style="color: #9cdcfe;">e</span>.<span style="color: #dcdcaa;">printStackTrace</span>();</div>
<div>
}</div>
<div>
}</div>
<div>
</div>
<div>
} </div>
<div>
<span style="color: #4ec9b0;">@PostConstruct</span></div>
<div>
<span style="color: #569cd6;">public</span> <span style="color: #4ec9b0;">void</span> <span style="color: #dcdcaa;">start</span>() {</div>
<div>
<span style="color: #c586c0;">new</span> <span style="color: #dcdcaa;">Thread</span>(<span style="color: #569cd6;">this</span>).<span style="color: #dcdcaa;">start</span>();</div>
<div>
}</div>
<div>
}</div>
</div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-75422418683074395932018-06-05T23:02:00.002+05:302018-06-05T23:02:17.927+05:30ABOUT THIS BLOG<div dir="ltr" style="text-align: left;" trbidi="on">
<h1>
<marquee direction="right">WANT TO LEARN SOMETHING MORE IN JAVA?</marquee></h1>
<br />
<b>THIS BLOG IS ALL ABOUT JAVA. IF YOU HAVE SOME BASIC KNOWLEDGE THEN YOU CAN GO THROUGH IT. IT WILL BE VERY HELPFUL FOR THE INTERVIEWS. READ CAREFULLY ALL THE QUESTIONS.IF YOU HAVE ANY DOUBT THEN CONTACT ME(bhabani.pattanayak@gmail.com).EVERYDAY THERE MUST BE SOME NEW POSTS IN THIS BLOG.SO CHECK THIS BLOG DAILY IF YOU WANT TO LEARN SOMETHING NEW.</b><br />
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
</div>
<br />
<h1 style="font-family: "times new roman";">
<marquee direction="left">GO THROUGH THE TOPICS</marquee></h1>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-21086847245948721822018-06-05T23:01:00.001+05:302018-06-05T23:01:40.099+05:30How to use gitbash in visual studio code (windows)<div dir="ltr" style="text-align: left;" trbidi="on">
Git bash is very useful shell you might need to integrate with the visual studio code.<br />
<br />
Go to file -> preferences -> setting<br />
Paste these in USER SETTING<br />
<br />
<div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;">
<div>
{</div>
<div>
<span style="color: #9cdcfe;">"terminal.integrated.shell.windows"</span>: <span style="color: #ce9178;">"C:</span><span style="color: #d7ba7d;">\\</span><span style="color: #ce9178;">Program Files</span><span style="color: #d7ba7d;">\\</span><span style="color: #ce9178;">Git</span><span style="color: #d7ba7d;">\\</span><span style="color: #ce9178;">bin</span><span style="color: #d7ba7d;">\\</span><span style="color: #ce9178;">bash.exe"</span>,</div>
<div>
<span style="color: #9cdcfe;">"terminal.integrated.shellArgs.windows"</span>: [</div>
<div>
<span style="color: #ce9178;">"--init-file"</span>,</div>
<div>
<span style="color: #ce9178;">"C:</span><span style="color: #d7ba7d;">\\</span><span style="color: #ce9178;">Program Files</span><span style="color: #d7ba7d;">\\</span><span style="color: #ce9178;">Git</span><span style="color: #d7ba7d;">\\</span><span style="color: #ce9178;">etc</span><span style="color: #d7ba7d;">\\</span><span style="color: #ce9178;">profile"</span></div>
<div>
]</div>
<div>
}</div>
</div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-90612210769955875822018-04-22T00:50:00.001+05:302018-04-22T00:50:36.986+05:30How to handle selenium test failed org.openqa.selenium.unhandledalertexception<div dir="ltr" style="text-align: left;" trbidi="on">
You can use<br />
<br />
DesiredCapabilities browser = DesiredCapabilities.firefox();<br />
browser.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);<br />
You can use UnexpectedAlertBehaviour.ACCEPT or UnexpectedAlertBehaviour.DISMISS<br />
Same can be done for other drivers.<br />
<br />
Or if specific alert you can do<br />
driver.switchTo().alert().accept();<br />
driver.switchTo().alert().dismiss();</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-37806687335823099182018-04-02T04:49:00.001+05:302018-04-02T04:49:38.715+05:30GIT - generating a new ssh key and add to the account.<div dir="ltr" style="text-align: left;" trbidi="on">
First time when you use git bash to clone a repository from git you will face this error,<div>
git@github.com: Permission denied (publickey).</div>
<div>
<br /></div>
<div>
This is because git repository does not recognize your computer.</div>
<div>
Now we will create ssh key so that every time you do a clone, GIT can understand who you are.</div>
<div>
Steps - </div>
<div>
Git should be installed in your computer.(https://git-scm.com/downloads)</div>
<div>
<br /></div>
<div>
Open Git bash shell</div>
<div>
Generate the local key</div>
<div>
<br /></div>
<div>
<span style="background-color: black;"><span style="color: white;">$ ssh-keygen -t rsa -b 4096 -C "bhabani.pattanayak@gmail.com"</span></span></div>
<div>
<br /></div>
<div>
It creates new ssh key using the email address</div>
<div>
<span style="background-color: black;"><span style="color: white;">$ Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]</span></span></div>
<div>
<br /></div>
<div>
<div>
<span style="background-color: black;"><span style="color: white;">$ Enter passphrase (empty for no passphrase): [Type a passphrase]</span></span></div>
<div>
<span style="background-color: black;"><span style="color: white;">$ Enter same passphrase again: [Type passphrase again]</span></span></div>
</div>
<div>
<br /></div>
<div>
Enter a secure pass phrase you can remember.</div>
<div>
<br /></div>
<div>
Start the ssh agent in the background and add your private key to the agent.</div>
<div>
<span style="background-color: black;"><span style="color: white;">$ eval $(ssh-agent -s)</span></span></div>
<div>
<span style="background-color: black;"><span style="color: white;">$ ssh-add ~/.ssh/id_rsa</span></span></div>
<div>
<br /></div>
<div>
Copy the SSH key </div>
<div>
<span style="background-color: black;"><span style="color: white;">$ clip < ~/.ssh/id_rsa.pub</span></span></div>
<div>
<br /></div>
<div>
Then add the SSH key to your git hub account.</div>
<div>
<br /></div>
<div>
In the right side Account -> Setting (https://github.com/settings/profile)</div>
<div>
Then click SSH and GPG keys. Add New SSH key.</div>
<div>
Title - You can give any name like 'Office laptop'</div>
<div>
Then paste the SSH key to the Key area which you copied earlier.</div>
<div>
Now you all set.You should be able to clone git repository.</div>
<div>
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-28866408949119992402017-07-17T21:25:00.006+05:302017-07-17T21:25:46.347+05:30How to print garbage collection details with timestamp<div dir="ltr" style="text-align: left;" trbidi="on">
Add these JVM parameters to print the garbage collection details with time stamp.<br />
The log will be printed in the System.out<br />
<br />
java -XX:+PrintGC -XX:+PrintGCDateStamps MyApplication<br />
<br />
If you need more gc details then<br />
<br />
java -XX:+PrintGCDetails -XX:+PrintGCDateStamps MyApplication</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-77785562671722333232017-02-03T04:21:00.004+05:302017-02-03T04:21:41.701+05:30How to modify environment variable in JAVA.<div dir="ltr" style="text-align: left;" trbidi="on">
We can get the environment variable using java System.getenv("env-name").<div>
But there is no setter for this.</div>
<div>
You can set it like this</div>
<div>
<br /></div>
<div>
<div>
<br /></div>
<div>
<b>import java.lang.reflect.Field;</b></div>
<div>
<b>import java.util.Collections;</b></div>
<div>
<b>import java.util.HashMap;</b></div>
<div>
<b>import java.util.Map;</b></div>
<div>
<b><br /></b></div>
<div>
<b><br /></b></div>
<div>
<b>public class Test {</b></div>
<div>
<b><br /></b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>public static void main(String[] args) throws Exception {</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>Map<string string=""> map = System.getenv();</string></b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>Map<string string=""> newmap = new HashMap<string string="">();</string></string></b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>newmap.putAll(map);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>newmap.put("abc", "----------");</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>setEnv(newmap);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println(map);</b></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"><b> </b></span></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("done");</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println(System.getenv("abc"));</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>newmap.remove("abc");</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>setEnv(newmap);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println(System.getenv("abc"));</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"><b> </b></span></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>private static void setEnv(Map<string string=""> newenv)</string></b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>{</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> try</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> {</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> Class processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> theEnvironmentField.setAccessible(true);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> Map<string string=""> env = (Map<string string="">) theEnvironmentField.get(null);</string></string></b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> env.putAll(newenv);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> theCaseInsensitiveEnvironmentField.setAccessible(true);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> Map<string string=""> cienv = (Map<string string="">) theCaseInsensitiveEnvironmentField.get(null);</string></string></b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> cienv.putAll(newenv);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> }</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> catch (NoSuchFieldException e)</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> {</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> try {</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> Class[] classes = Collections.class.getDeclaredClasses();</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> Map<string string=""> env = System.getenv();</string></b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> for(Class cl : classes) {</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> if("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> Field field = cl.getDeclaredField("m");</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> field.setAccessible(true);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> Object obj = field.get(env);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> Map<string string=""> map = (Map<string string="">) obj;</string></string></b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> map.clear();</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> map.putAll(newenv);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> }</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> }</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> } catch (Exception e2) {</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> System.out.println(e2);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> }</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> } catch (Exception e1) {</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println(e1);</b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span> } </b></div>
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></div>
<div>
<b>}</b></div>
</div>
<div>
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-24342296502734513672017-01-05T06:37:00.002+05:302017-01-05T06:38:03.272+05:30How to take screen shot in java<div dir="ltr" style="text-align: left;" trbidi="on">
This below program will take the screenshot in every 5 seconds interval<br />
<br />
<br />
import java.awt.Rectangle;<br />
import java.awt.Robot;<br />
import java.awt.Toolkit;<br />
import java.awt.image.BufferedImage;<br />
import java.io.File;<br />
import java.util.Timer;<br />
import java.util.TimerTask;<br />
<br />
import javax.imageio.ImageIO;<br />
<br />
public class Test extends TimerTask{<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private static int num = 0;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private static String FILE_PATH = "/data1/backup";<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public static void main(String[] args) throws Exception{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Timer timer = new Timer();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>timer.schedule(new Test(), 0, 5000);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private void takeScreenshot(){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>File file = new File(FILE_PATH);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>final Robot rbt = new Robot();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>BufferedImage screenshot = rbt.createScreenCapture(screenRect);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>ImageIO.write(screenshot, "jpg", new File(file + "/" + num + ".jpg"));<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>num++;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("screenshot taken"+num);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}catch(Exception e){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Exception "+e);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void run() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>takeScreenshot();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<br />
}</div>
Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-1235110526979374490.post-17927413573325707062016-04-30T02:34:00.002+05:302016-04-30T02:34:54.777+05:30Simple Cucumber example<div dir="ltr" style="text-align: left;" trbidi="on">
Cucumber is a testing framework used to test another software.<br />
The tests are written in BDD approach (behavior driven development).<br />
<br />
A Simple Example:<br />
<br />
<b style="background-color: #ffe599;">public class Book {</b><br />
<b style="background-color: #ffe599;"><br /></b>
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>private String name;</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>private String auther;</b><br />
<b style="background-color: #ffe599;"><br /></b>
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>public Book(String name, String auther) {</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>this.name = name;</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>this.auther = auther;</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>public String getName() {</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>return name;</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>public void setName(String name) {</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>this.name = name;</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>public String getAuther() {</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>return auther;</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>public void setAuther(String auther) {</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>this.auther = auther;</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b><br />
<b style="background-color: #ffe599;">}</b><br />
<br />
<span style="background-color: #ffe599;"><b>public class Library {</b></span><br />
<span style="background-color: #ffe599;"><b><br /></b></span>
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>private List<book> bookStore = new ArrayList<book>();</book></book></b></span><br />
<span style="background-color: #ffe599;"><b><br /></b></span>
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>public void addBook(Book book) {</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>bookStore.add(book);</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>public List<book> findBooks(String auther) {</book></b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>List<book> newList = new ArrayList<book>();</book></book></b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>for (Book book : bookStore) {</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>if (book.getAuther().equals(auther)) {</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>newList.add(book);</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>return newList;</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></span><br />
<span style="background-color: #ffe599;"><b>}</b></span><br />
<br />
We have a book store. We will test the search feature in our book store.<br />
<br />
Now we will write the behavior in a feature file.<br />
<br />
<b style="background-color: #ffe599;"><br /></b>
<b style="background-color: #ffe599;">Feature:Book Search</b><br />
<b style="background-color: #ffe599;">To allow customers to search books quickly</b><br />
<b style="background-color: #ffe599;">Scenario:Search book by author</b><br />
<b style="background-color: #ffe599;">Given A book with title 'book1' is written by author 'A'</b><br />
<b style="background-color: #ffe599;">And A book with title 'book2' is written by author 'B'</b><br />
<b style="background-color: #ffe599;">And A book with title 'book3' is written by author 'C' </b><br />
<b style="background-color: #ffe599;">When the customer search for author 'A'</b><br />
<b style="background-color: #ffe599;">Then '1' book should be found</b><br />
<b style="background-color: #ffe599;">And The book should have author 'A'</b><br />
<b style="background-color: #ffe599;"><br /></b>
<b style="background-color: #ffe599;"><br /></b>
<b style="background-color: #ffe599;">Given A book with title 'b1' is written by author 'A1'</b><br />
<b style="background-color: #ffe599;">And A book with title 'b2' is written by author 'A1'</b><br />
<b style="background-color: #ffe599;">And A book with title 'b3' is written by author 'A3' </b><br />
<b style="background-color: #ffe599;">When the customer search for author 'A1'</b><br />
<b style="background-color: #ffe599;">Then '2' book should be found</b><br />
<b style="background-color: #ffe599;">And The book should have author 'A1' </b><br />
<br />
Now we will write step which uses the feature file and test the feature.<br />
<b style="background-color: #ffe599;"><br /></b>
<b style="background-color: #ffe599;">public class BookSearchSteps {</b><br />
<b style="background-color: #ffe599;"><br /></b>
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Library library = new Library();</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>List<book> result = new ArrayList<book>();</book></book></b><br />
<span class="Apple-tab-span" style="background-color: #ffe599; white-space: pre;"><b> </b></span><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>@Given("A book with title '(.+)' is written by author '(.+)'")</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>public void addNewBook(String title,String auther){</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>library.addBook(new Book(title, auther));</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>@When("^the customer search for author '(.+)'")</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>public void addSearchCriteria(String auther){</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>result = library.findBooks(auther);</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>@Then("'(.+)' book should be found$")</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>public void verifyAmountOfBook(int booksFound){</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>MatcherAssert.assertThat(result.size(), IsEqual.equalTo(booksFound));</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>@Then("The book should have author '(.+)'")</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>public void verifyAuther(String auther){</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>for (Book book:result){</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>MatcherAssert.assertThat(book.getAuther(), IsEqual.equalTo(auther));</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b><br />
<b style="background-color: #ffe599;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b><br />
<b style="background-color: #ffe599;">}</b><br />
<br />
For running the test case. We can not run the step file directly.<br />
We need to have another file to run the test.<br />
<b style="background-color: #ffe599;"><br /></b>
<b style="background-color: #ffe599;">@RunWith(Cucumber.class)</b><br />
<b style="background-color: #ffe599;">@CucumberOptions(plugin={"pretty"},features="<path>\\search_book.feature")</path></b><br />
<b style="background-color: #ffe599;">public class BookSearchTest {</b><br />
<b style="background-color: #ffe599;">}</b><br />
<br />
<br />
Lets take another example.<br />
When you have to test multiple inputs, then in this approach you need to write multiple given when then.<br />
In that case we can use scenario outline. For running scenario you must use <b><cucumber .version="">1.2.4</cucumber></b><br />
<span style="background-color: #ffe599;"><br /></span>
<span style="background-color: #ffe599;"><b>public class AppleStore {</b></span><br />
<span style="background-color: #ffe599;"><b><br /></b></span>
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>private int noOfApple;</b></span><br />
<span class="Apple-tab-span" style="background-color: #ffe599; white-space: pre;"><b> </b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>public AppleStore(int noOfApple){</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>this.noOfApple = noOfApple;</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>public int eatApple(int howManyToEat){</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>return noOfApple-howManyToEat;</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></span><br />
<br />
<span style="background-color: #ffe599;"><b>}</b></span><br />
<b><br /></b>
<b>AppleStore.feature</b><br />
<span style="background-color: #ffe599;"><b>Feature:Apple Store check</b></span><br />
<span style="background-color: #ffe599;"><b>Scenario Outline: Eating</b></span><br />
<span style="background-color: #ffe599;"><b>Given There are "<total>" apples</total></b></span><br />
<span style="background-color: #ffe599;"><b>When I ate "<eat>" apples</eat></b></span><br />
<span style="background-color: #ffe599;"><b>Then There should be "<left>" left</left></b></span><br />
<span style="background-color: #ffe599;"><b><br /></b></span>
<span style="background-color: #ffe599;"><b>Examples:</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>|total<span class="Apple-tab-span" style="white-space: pre;"> </span>|eat<span class="Apple-tab-span" style="white-space: pre;"> </span>|left |</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>| 2 | 1 | 1 |</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>| 20 | 2 | 18 |</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>| 9 | 2 | 7 |</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>| 5 | 0 | 5 |</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>| 3 | 2 | 1 |</b></span><br />
<b><br /></b>
<b><br /></b>
<span style="background-color: #ffe599;"><b>import org.hamcrest.MatcherAssert;</b></span><br />
<span style="background-color: #ffe599;"><b>import org.hamcrest.core.IsEqual;</b></span><br />
<span style="background-color: #ffe599;"><b>import cucumber.api.java.en.Given;</b></span><br />
<span style="background-color: #ffe599;"><b>import cucumber.api.java.en.Then;</b></span><br />
<span style="background-color: #ffe599;"><b>import cucumber.api.java.en.When;</b></span><br />
<span style="background-color: #ffe599;"><b><br /></b></span>
<span style="background-color: #ffe599;"><b>public class AppleStoreDefinition {</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>AppleStore appleStore;</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>int remaining;</b></span><br />
<span class="Apple-tab-span" style="background-color: #ffe599; white-space: pre;"><b> </b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>@Given("^There are \"(.+)\" apples$")</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>public void init(int total) {</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>appleStore = new AppleStore(total);</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>@When("^I ate \"(.+)\" apples$")</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>public void add(int noOfApplesToEat) {</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>remaining = appleStore.eatApple(noOfApplesToEat);</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>@Then("^There should be \"(.+)\" left$")</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>public void verify(int exceptedResult) {</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>MatcherAssert.assertThat(remaining, IsEqual.equalTo(exceptedResult));</b></span><br />
<span style="background-color: #ffe599;"><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></span><br />
<span style="background-color: #ffe599;"><b>}</b></span><br />
<div>
<br /></div>
In this example no need to write multiple given when then.<br />
<br />
<br />
<br />
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-49346948076886775922015-06-18T11:50:00.001+05:302015-06-18T11:54:23.720+05:30One way hashing to store passwords<div dir="ltr" style="text-align: left;" trbidi="on">
One way hashing is for validating the integrity of content by detecting all modification thereof via obvious changes to the hash output. Hashing serves the purpose of ensuring integrity, i.e. making it so that if something is changed you can know that it’s changed. Technically, hashing takes arbitrary input and produces a fixed-length string that has the following attributes. Checksum calculation is a type of one way hashing.<br />
1. The same input will always produce the same output.<br />
2. Multiple disparate inputs should not produce the same output.<br />
3. It should not be possible to go from the output to the input.<br />
4. Any modification of a given input should result in drastic change to the hash. <br />
<br />
We use one way hashing to store passwords so that we can not read it.<br />
Once we get the password at the registration time we will create the fixed length string and store it, During the login time again we create the fixed length string using the same one way hashing technique and compare with the old one. We have different algorithms like MD5,SHA for this.<br />
<br />
<span style="background-color: #ffe599;">Example:</span><br />
<span style="background-color: #ffe599;">import java.security.MessageDigest;<br />public class Test {<br /> public static void main(String[] args) throws Exception {<br /> String str = "bhabani";<br /> MessageDigest md = MessageDigest.getInstance("MD5");<br /> md.update(str.getBytes());<br /> String newStr = new String(md.digest());<br /> System.out.println(newStr);<br /> //This below string is created for the first time<br /> System.out.println(newStr.equals(" ÒÌw ”ªÆYh«Ï(N0"));<br /> }<br />}</span><br />
<br />
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-74241716888643524692015-04-01T16:12:00.005+05:302015-04-01T16:12:44.835+05:30Abstract Factory Desion Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
</o:OfficeDocumentSettings>
</xml><![endif]--><br />
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val="Cambria Math"/>
<m:brkBin m:val="before"/>
<m:brkBinSub m:val="--"/>
<m:smallFrac m:val="off"/>
<m:dispDef/>
<m:lMargin m:val="0"/>
<m:rMargin m:val="0"/>
<m:defJc m:val="centerGroup"/>
<m:wrapIndent m:val="1440"/>
<m:intLim m:val="subSup"/>
<m:naryLim m:val="undOvr"/>
</m:mathPr></w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
DefSemiHidden="true" DefQFormat="false" DefPriority="99"
LatentStyleCount="267">
<w:LsdException Locked="false" Priority="0" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Normal"/>
<w:LsdException Locked="false" Priority="9" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="heading 1"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/>
<w:LsdException Locked="false" Priority="39" Name="toc 1"/>
<w:LsdException Locked="false" Priority="39" Name="toc 2"/>
<w:LsdException Locked="false" Priority="39" Name="toc 3"/>
<w:LsdException Locked="false" Priority="39" Name="toc 4"/>
<w:LsdException Locked="false" Priority="39" Name="toc 5"/>
<w:LsdException Locked="false" Priority="39" Name="toc 6"/>
<w:LsdException Locked="false" Priority="39" Name="toc 7"/>
<w:LsdException Locked="false" Priority="39" Name="toc 8"/>
<w:LsdException Locked="false" Priority="39" Name="toc 9"/>
<w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/>
<w:LsdException Locked="false" Priority="10" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Title"/>
<w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/>
<w:LsdException Locked="false" Priority="11" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/>
<w:LsdException Locked="false" Priority="22" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Strong"/>
<w:LsdException Locked="false" Priority="20" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/>
<w:LsdException Locked="false" Priority="59" SemiHidden="false"
UnhideWhenUsed="false" Name="Table Grid"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/>
<w:LsdException Locked="false" Priority="1" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 1"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 1"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 1"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/>
<w:LsdException Locked="false" Priority="34" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/>
<w:LsdException Locked="false" Priority="29" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Quote"/>
<w:LsdException Locked="false" Priority="30" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 1"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 1"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 2"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 2"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 2"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 2"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 2"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 3"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 3"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 3"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 3"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 3"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 4"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 4"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 4"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 4"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 4"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 5"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 5"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 5"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 5"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 5"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 6"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 6"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 6"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 6"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 6"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/>
<w:LsdException Locked="false" Priority="19" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/>
<w:LsdException Locked="false" Priority="21" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/>
<w:LsdException Locked="false" Priority="31" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/>
<w:LsdException Locked="false" Priority="32" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/>
<w:LsdException Locked="false" Priority="33" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Book Title"/>
<w:LsdException Locked="false" Priority="37" Name="Bibliography"/>
<w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/>
</w:LatentStyles>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}
</style>
<![endif]-->
<br />
<div class="MsoNormal">
This is a creational design pattern. This is actually one
more layer abstraction on top of factory method pattern. You can say it factory
of factory. </div>
<div class="MsoNormal">
Abstract factory is responsible for creating factory of related
objects. Each generated factory gives the objects as per Factory pattern.</div>
<div class="MsoNormal">
Example:</div>
<div class="MsoNormal">
We will have two types of related interfaces Shape and Color.
Will have a factory(ShapeFactory) to return the correct shape and another
factory(ColourFactory) to return the coloue object.</div>
<div class="MsoNormal">
Now on top of these two factories we will have another
Abstract Factory which is responsible to return the required Factory (ShapeFactory
or ColourFactory).</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /><span style="background-color: #ffe599;">//One Interface<br />public interface Shape {<br /> public void draw();<br />}<br /><br />public class CircelShape implements Shape {<br /> public void draw() {<br /> System.out.println("Draw Circel");<br /> }<br />}<br /><br />public class RectangleShape implements Shape {<br /> public void draw() {<br /> System.out.println("Draw Rectangle");<br /> }<br />}<br /><br />// Another related interface<br />public interface Color {<br /> public void fill();<br />}<br /><br />public class GreenColor implements Color {<br /> public void fill() {<br /> System.out.println("Fill colour Green");<br /> }<br />}<br /><br />public class RedColor implements Color {<br /> public void fill() {<br /> System.out.println("Fill colour Red");<br /> }<br />}<br /><br />public abstract class AbstractFactory {<br /> public abstract Shape getShape(String shapeType);<br /> public abstract Color getColor(String colourType);<br />}<br /><br />public class ShapeFactory extends AbstractFactory {<br /> public Shape getShape(String shapeType) {<br /> Shape shape = null;<br /> if ("circle".equals(shapeType)) {<br /> shape = new CircelShape();<br /> } else if ("rectangle".equals(shapeType)) {<br /> shape = new RectangleShape();<br /> }<br /> return shape;<br /> }<br /><br /> public public Color getColor(String colourType) {<br /> return null;<br /> }<br />}<br /><br />public class ColorFactory extends AbstractFactory {<br /> public Color getColor(String colorType) {<br /> Color color = null;<br /> if ("red".equals(colorType)) {<br /> color = new RedColor();<br /> } else if ("green".equals(colorType)) {<br /> color = new GreenColor();<br /> }<br /> return color;<br /> }<br /><br /> public Shape getShape(String shape) {<br /> return null;<br /> }<br /><br />}<br /><br />// This is the factory of factories<br />public class FactoryProducer {<br /> public static AbstractFactory getFactory(String factoryType) {<br /> AbstractFactory factory = null;<br /> if ("shape".equals(factoryType)) {<br /> factory = new ShapeFactory();<br /> } else if ("colour".equals(factoryType)) {<br /> factory = new ColorFactory();<br /> }<br /> return factory;<br /> }<br />}<br /><br />public class AbstractFactoryPattern {<br /> public static void main(String[] args) {<br /> // Need a circel shape<br /> AbstractFactory shapeFactory = FactoryProducer.getFactory("shape");<br /> Shape circelShape = shapeFactory.getShape("circle");<br /> circelShape.draw();<br /> // Now colour<br /> AbstractFactory colourFactory = FactoryProducer.getFactory("colour");<br /> Color redColor = colourFactory.getColor("red");<br /> redColor.fill();<br /><br /> }<br />} </span></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-19033038837262994042015-04-01T12:46:00.001+05:302015-04-01T12:46:49.479+05:30How to short a map based on value ?<div dir="ltr" style="text-align: left;" trbidi="on">
We can do this in two ways,<br />
By passing a comparator in the map,<br /><br /><span style="background-color: #ffd966;"><b>import java.util.Comparator;<br />import java.util.HashMap;<br />import java.util.Map;<br />import java.util.TreeMap;<br /><br />public class SortMapByValue {<br /><br /> public static void main(String[] args) {<br /> final Map<string string=""> upsortedMap = new HashMap<string string="">();<br /> upsortedMap.put("a", "x");<br /> upsortedMap.put("b", "a");<br /> upsortedMap.put("c", "z");<br /> upsortedMap.put("d", "m");<br /> upsortedMap.put("e", "c");<br /> <br /> Map<string string=""> sortedMap = new TreeMap<string string="">(new Comparator<string>() {<br /> @Override<br /> public int compare(String s1, String s2) {<br /> return upsortedMap.get(s1).compareTo(upsortedMap.get(s2));<br /> }<br /> });<br /> sortedMap.putAll(upsortedMap);<br /> System.out.println(sortedMap);<br /> }<br />}</string></string></string></string></string></b></span><br />
<span style="background-color: #ffd966;"><b><br /></b></span>
<b>By using the EntrySet,</b><br />
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-32685354610159107502015-03-31T12:43:00.000+05:302015-03-31T12:43:09.635+05:30Factory Method Design Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
</o:OfficeDocumentSettings>
</xml><![endif]--><br />
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val="Cambria Math"/>
<m:brkBin m:val="before"/>
<m:brkBinSub m:val="--"/>
<m:smallFrac m:val="off"/>
<m:dispDef/>
<m:lMargin m:val="0"/>
<m:rMargin m:val="0"/>
<m:defJc m:val="centerGroup"/>
<m:wrapIndent m:val="1440"/>
<m:intLim m:val="subSup"/>
<m:naryLim m:val="undOvr"/>
</m:mathPr></w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
DefSemiHidden="true" DefQFormat="false" DefPriority="99"
LatentStyleCount="267">
<w:LsdException Locked="false" Priority="0" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Normal"/>
<w:LsdException Locked="false" Priority="9" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="heading 1"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/>
<w:LsdException Locked="false" Priority="39" Name="toc 1"/>
<w:LsdException Locked="false" Priority="39" Name="toc 2"/>
<w:LsdException Locked="false" Priority="39" Name="toc 3"/>
<w:LsdException Locked="false" Priority="39" Name="toc 4"/>
<w:LsdException Locked="false" Priority="39" Name="toc 5"/>
<w:LsdException Locked="false" Priority="39" Name="toc 6"/>
<w:LsdException Locked="false" Priority="39" Name="toc 7"/>
<w:LsdException Locked="false" Priority="39" Name="toc 8"/>
<w:LsdException Locked="false" Priority="39" Name="toc 9"/>
<w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/>
<w:LsdException Locked="false" Priority="10" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Title"/>
<w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/>
<w:LsdException Locked="false" Priority="11" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/>
<w:LsdException Locked="false" Priority="22" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Strong"/>
<w:LsdException Locked="false" Priority="20" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/>
<w:LsdException Locked="false" Priority="59" SemiHidden="false"
UnhideWhenUsed="false" Name="Table Grid"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/>
<w:LsdException Locked="false" Priority="1" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 1"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 1"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 1"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/>
<w:LsdException Locked="false" Priority="34" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/>
<w:LsdException Locked="false" Priority="29" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Quote"/>
<w:LsdException Locked="false" Priority="30" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 1"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 1"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 2"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 2"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 2"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 2"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 2"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 3"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 3"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 3"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 3"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 3"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 4"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 4"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 4"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 4"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 4"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 5"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 5"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 5"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 5"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 5"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 6"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 6"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 6"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 6"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 6"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/>
<w:LsdException Locked="false" Priority="19" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/>
<w:LsdException Locked="false" Priority="21" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/>
<w:LsdException Locked="false" Priority="31" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/>
<w:LsdException Locked="false" Priority="32" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/>
<w:LsdException Locked="false" Priority="33" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Book Title"/>
<w:LsdException Locked="false" Priority="37" Name="Bibliography"/>
<w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/>
</w:LatentStyles>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}
</style>
<![endif]-->
<br />
<div class="MsoNormal">
This is a creational design pattern. </div>
<div class="MsoNormal">
This is used to create an object based on some logic. The
object creation logic is hidden from the client. The method which returns the
object is called factory method.</div>
<div class="MsoNormal">
This factory pattern is pretty simple. Client asks for the
object which is required and pass some argument to identity what it needs
exactly, then it gets the desired object. The client is not bothered how the
object is getting created.</div>
<div class="MsoNormal">
The advantage in this pattern is, new type of Objects can be
added in future without changing any code. Only we need to add to the factory.</div>
<div class="MsoNormal">
Example in JAVA- SaxParserFactory.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Let’s take an example. <span style="mso-spacerun: yes;"> </span>Suppose we need a factory for connection. The connection
can be multiple types e.g. serial, ip. Now the factory will create the object
based on request and return to the client.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: #7f0055; font-family: Consolas;">package</span><span style="color: black; font-family: Consolas;"> designpattern;</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: #7f0055; font-family: Consolas;">interface</span><span style="color: black; font-family: Consolas;"> Channel {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #7f0055; font-family: Consolas;">public</span><span style="color: black; font-family: Consolas;"> </span><span style="color: #7f0055; font-family: Consolas;">void</span><span style="color: black; font-family: Consolas;"> connect();</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;">}</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: #7f0055; font-family: Consolas;">class</span><span style="color: black; font-family: Consolas;"> SerialChannel </span><span style="color: #7f0055; font-family: Consolas;">implements</span><span style="color: black; font-family: Consolas;"> Channel {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #7f0055; font-family: Consolas;">public</span><span style="color: black; font-family: Consolas;"> </span><span style="color: #7f0055; font-family: Consolas;">void</span><span style="color: black; font-family: Consolas;"> connect() {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span>System.</span><i><span style="color: #0000c0; font-family: Consolas;">out</span></i><span style="color: black; font-family: Consolas;">.println(</span><span style="color: #2a00ff; font-family: Consolas;">"Connecting serial
channel"</span><span style="color: black; font-family: Consolas;">);</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;">}</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: #7f0055; font-family: Consolas;">class</span><span style="color: black; font-family: Consolas;"> IpChannel </span><span style="color: #7f0055; font-family: Consolas;">implements</span><span style="color: black; font-family: Consolas;"> Channel {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #7f0055; font-family: Consolas;">public</span><span style="color: black; font-family: Consolas;"> </span><span style="color: #7f0055; font-family: Consolas;">void</span><span style="color: black; font-family: Consolas;"> connect() {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span>System.</span><i><span style="color: #0000c0; font-family: Consolas;">out</span></i><span style="color: black; font-family: Consolas;">.println(</span><span style="color: #2a00ff; font-family: Consolas;">"Connecting ip channel"</span><span style="color: black; font-family: Consolas;">);</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;">}</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: #7f0055; font-family: Consolas;">class</span><span style="color: black; font-family: Consolas;"> ChannelFactory {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #7f0055; font-family: Consolas;">public</span><span style="color: black; font-family: Consolas;"> Channel getChannel(String </span><span style="color: #6a3e3e; font-family: Consolas;">channelType</span><span style="color: black; font-family: Consolas;">) {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span>Channel </span><span style="color: #6a3e3e; font-family: Consolas;">channel</span><span style="color: black; font-family: Consolas;"> = </span><span style="color: #7f0055; font-family: Consolas;">null</span><span style="color: black; font-family: Consolas;">;</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span></span><span style="color: #7f0055; font-family: Consolas;">if</span><span style="color: black; font-family: Consolas;"> (</span><span style="color: #2a00ff; font-family: Consolas;">"serial"</span><span style="color: black; font-family: Consolas;">.equals(</span><span style="color: #6a3e3e; font-family: Consolas;">channelType</span><span style="color: black; font-family: Consolas;">)) {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 3;"> </span></span><span style="color: #6a3e3e; font-family: Consolas;">channel</span><span style="color: black; font-family: Consolas;"> = </span><span style="color: #7f0055; font-family: Consolas;">new</span><span style="color: black; font-family: Consolas;"> SerialChannel();</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span>} </span><span style="color: #7f0055; font-family: Consolas;">else</span><span style="color: black; font-family: Consolas;"> </span><span style="color: #7f0055; font-family: Consolas;">if</span><span style="color: black; font-family: Consolas;"> (</span><span style="color: #2a00ff; font-family: Consolas;">"ip"</span><span style="color: black; font-family: Consolas;">.equals(</span><span style="color: #6a3e3e; font-family: Consolas;">channelType</span><span style="color: black; font-family: Consolas;">)) {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 3;"> </span></span><span style="color: #6a3e3e; font-family: Consolas;">channel</span><span style="color: black; font-family: Consolas;"> = </span><span style="color: #7f0055; font-family: Consolas;">new</span><span style="color: black; font-family: Consolas;"> IpChannel();</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span>}</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span></span><span style="color: #7f0055; font-family: Consolas;">return</span><span style="color: black; font-family: Consolas;"> </span><span style="color: #6a3e3e; font-family: Consolas;">channel</span><span style="color: black; font-family: Consolas;">;</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;">}</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: #7f0055; font-family: Consolas;">public</span><span style="color: black; font-family: Consolas;"> </span><span style="color: #7f0055; font-family: Consolas;">class</span><span style="color: black; font-family: Consolas;"> FactoryMethodPattern {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #7f0055; font-family: Consolas;">public</span><span style="color: black; font-family: Consolas;"> </span><span style="color: #7f0055; font-family: Consolas;">static</span><span style="color: black; font-family: Consolas;"> </span><span style="color: #7f0055; font-family: Consolas;">void</span><span style="color: black; font-family: Consolas;"> main(String[] </span><span style="color: #6a3e3e; font-family: Consolas;">args</span><span style="color: black; font-family: Consolas;">) {</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span>ChannelFactory
</span><span style="color: #6a3e3e; font-family: Consolas;">channelFactory</span><span style="color: black; font-family: Consolas;"> = </span><span style="color: #7f0055; font-family: Consolas;">new</span><span style="color: black; font-family: Consolas;"> ChannelFactory();</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span></span><span style="color: #3f7f5f; font-family: Consolas;">// Client wants serial channel</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span>Channel </span><span style="color: #6a3e3e; font-family: Consolas;">serialChannel</span><span style="color: black; font-family: Consolas;"> = </span><span style="color: #6a3e3e; font-family: Consolas;">channelFactory</span><span style="color: black; font-family: Consolas;">.getChannel(</span><span style="color: #2a00ff; font-family: Consolas;">"serial"</span><span style="color: black; font-family: Consolas;">);</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span></span><span style="color: #6a3e3e; font-family: Consolas;">serialChannel</span><span style="color: black; font-family: Consolas;">.connect();</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span></span><span style="color: #3f7f5f; font-family: Consolas;">// Client wants <u>ip</u> channel</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span>Channel </span><span style="color: #6a3e3e; font-family: Consolas;">ipChannel</span><span style="color: black; font-family: Consolas;"> = </span><span style="color: #6a3e3e; font-family: Consolas;">channelFactory</span><span style="color: black; font-family: Consolas;">.getChannel(</span><span style="color: #2a00ff; font-family: Consolas;">"ip"</span><span style="color: black; font-family: Consolas;">);</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 2;"> </span></span><span style="color: #6a3e3e; font-family: Consolas;">ipChannel</span><span style="color: black; font-family: Consolas;">.connect();</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: Consolas;"></span></b></span></div>
<span style="background-color: #ffd966;"><b>
</b></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background-color: #ffd966;"><b><span style="color: black; font-family: Consolas;">}</span></b></span><span style="font-family: Consolas;"></span></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-38114568211794212752013-03-23T14:31:00.002+05:302013-03-23T14:31:51.408+05:30Externalizable vs Serializable<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" id="internal-source-marker_0.46675019693215825" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">-
Externalizable is an interface that enables you to define custom rules
and your own mechanism for serialization. Serializable defines standard
protocol and provides out of the box serialization capabilities.</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">- Externalizable extends Serializable.</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">-
Implement writeExternal and readExternal methods of the Externalizable
interface and create your own contract / protocol for serialization.</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">-
In object de-serialization (reconsturction) the public no-argument
constructor is used to reconstruct the object. In case of Serializable,
instead of using constructor, the object is re-consturcted using data
read from ObjectInputStream.</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">-
The above point subsequently mandates that the Externalizable object
must have a public no-argument constructor. In the case of Seriablizable
it is not mandatory.</span></div>
</div>
Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-1235110526979374490.post-39704278125746560312013-03-23T14:23:00.001+05:302013-03-23T14:23:53.563+05:30Customize java serialization.<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" id="internal-source-marker_0.931996206408505" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">We can customize java serialization by providing the readObject() and writeObject() methods in the serializable class.</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">This
is not overriding or overloading methods and this is a mechanism
provided by serialization. These two included methods are declared
private but JVM can access the private methods of an object. There is no
change to the class that does the serialization and de-serialization.</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Example:</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">Serializable class:</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">public class Bean implements Serializable {</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> private static final long serialVersionUID = 8381419946118107894L;</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public String name;</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public int age;</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public Bean() {</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> System.out.println("constructor:");</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.name = "Bhabani";</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.age = 25;</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> private void writeObject(ObjectOutputStream out) throws IOException {</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> this.name = "bbc";//This value will be stored in serialization</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> out.defaultWriteObject();</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> private void readObject(ObjectInputStream in) throws IOException,</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ClassNotFoundException {</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> in.defaultReadObject();</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }}</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">The Main class:</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">public class Test {</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public static void main(String[] args)throws Exception {</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> Bean b = new Bean();</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> System.out.println("Jst before read object");</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> b.name = "BHABANI PATTANAYAK";</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> b.age = 33;</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ByteArrayOutputStream outStream = new ByteArrayOutputStream();</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ObjectOutputStream os = new ObjectOutputStream(outStream);</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> os.writeObject(b);</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> os.flush();</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> os.close();</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> ObjectInputStream in = new ObjectInputStream(inStream);</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> Bean bean = (Bean)in.readObject();</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> System.out.println("Name:"+bean.name+"\nAge:"+bean.age);</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">}</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Here
the bean object contains name as “BHABANI PATTANAYAK” But the
writeObject() method sets the name as “bbc” . So while serializing it
takes the name as “bbc”.</span></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-61584539540146021312013-03-23T11:44:00.000+05:302013-03-23T11:44:28.402+05:30What is loadfactor of hashtable ?<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" id="internal-source-marker_0.9121912511066963" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">public </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">HashMap</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">(int initialCapacity,float loadFactor)</span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Constructs an empty HashMap with the specified initial capacity and load factor. </span></div>
<div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt; text-indent: 36pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">An instance of HashMap has two parameters that affect its performance: </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">initial capacity</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> and </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">load factor</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">. The </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">capacity</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">
is the number of buckets in the hash table, and the initial capacity is
simply the capacity at the time the hash table is created. The </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">load factor</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">
is a measure of how full the hash table is allowed to get before its
capacity is automatically increased. When the number of entries in the
hash table exceeds the product of the load factor and the current
capacity, the hash table is </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">rehashed</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets. </span></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-79407825229810071062012-07-23T18:00:00.001+05:302012-07-23T18:00:40.738+05:30ABOUT THIS BLOG<h1><marquee direction="right">WANT TO LEARN SOMETHING MORE IN JAVA?</marquee></h1><br /><b>THIS BLOG IS ALL ABOUT JAVA. IF YOU HAVE SOME BASIC KNOWLEDGE THEN YOU CAN GO THROUGH IT. IT WILL BE VERY HELPFUL FOR THE INTERVIEWS. READ CAREFULLY ALL THE QUESTIONS.IF YOU HAVE ANY DOUBT THEN CONTACT ME.EVERYDAY THERE MUST BE SOME NEW POSTS IN THIS BLOG.SO CHECK THIS BLOG DAILY IF YOU WANT TO LEARN SOMETHING NEW .</b><br /><h1><marquee direction="left">GO THROUGH THE TOPICS</marquee></h1>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-19127108590963746952012-07-20T14:54:00.000+05:302012-07-20T14:54:00.386+05:30IdentityHashMap and WeakHashMap ?<div dir="ltr" style="text-align: left;" trbidi="on">
<span id="internal-source-marker_0.27181004528858055" style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">IdentityHashMap is exactly same as HashMap except the following difference.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">
In the HashMap JVM uses equals() method to identify duplicate keys
but in the case of IdentityHashMap JVM uses == operator for this.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">WeakHashMap is exactly same as HashMap except the following difference.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">
In case of HashMap an Object is not eligible for garbage collection if
it is associated with HashMap even though it doesn't have any external
references. ie HashMap dominates garbage collector.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">But
in case of WeakHashMap , if an Object is not having any external
references then it is always eligible for garbage collection even though
it is associated with weakHashMap. ie garbage collector dominates
WeakHashMap</span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-70823429600314228302012-07-20T14:47:00.001+05:302012-07-20T14:52:38.362+05:30Describe core collection interfaces.<div dir="ltr" style="text-align: left;" trbidi="on">
<span id="internal-source-marker_0.27181004528858055" style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">The following list describes the core collection interfaces:</span><br />
<ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Collection — the root of the collection hierarchy. A collection represents a group of objects known as its </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">elements</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">.
The Collection interface is the least common denominator that all
collections implement and is used to pass collections around and to
manipulate them when maximum generality is desired. Some types of
collections allow duplicate elements, and others do not. Some are
ordered and others are unordered. The Java platform doesn't provide any
direct implementations of this interface but provides implementations of
more specific subinterfaces, such as Set and List. Also see The
Collection Interface section.</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Set
— a collection that cannot contain duplicate elements. This interface
models the mathematical set abstraction and is used to represent sets,
such as the cards comprising a poker hand, the courses making up a
student's schedule, or the processes running on a machine. See also The
Set Interface section.</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">List — an ordered collection (sometimes called a </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">sequence</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">).
Lists can contain duplicate elements. The user of a List generally has
precise control over where in the list each element is inserted and can
access elements by their integer index (position). If you've used
Vector, you're familiar with the general flavor of List. Also see The
List Interface section.</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Queue
— a collection used to hold multiple elements prior to processing.
Besides basic Collection operations, a Queue provides additional
insertion, extraction, and inspection operations.</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Queues
typically, but do not necessarily, order elements in a FIFO (first-in,
first-out) manner. Among the exceptions are priority queues, which order
elements according to a supplied comparator or the elements' natural
ordering. Whatever the ordering used, the head of the queue is the
element that would be removed by a call to remove or poll. In a FIFO
queue, all new elements are inserted at the tail of the queue. Other
kinds of queues may use different placement rules. Every Queue
implementation must specify its ordering properties. Also see The Queue
Interface section.</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Map
— an object that maps keys to values. A Map cannot contain duplicate
keys; each key can map to at most one value. If you've used Hashtable,
you're already familiar with the basics of Map. Also see The Map
Interface section.</span></li>
</ul>
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">The last two core collection interfaces are merely sorted versions of Set and Map:</span><br />
<ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">SortedSet
— a Set that maintains its elements in ascending order. Several
additional operations are provided to take advantage of the ordering.
Sorted sets are used for naturally ordered sets, such as word lists and
membership rolls. Also see The SortedSet Interface section.</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">SortedMap
— a Map that maintains its mappings in ascending key order. This is the
Map analog of SortedSet. Sorted maps are used for naturally ordered
collections of key/value pairs, such as dictionaries and telephone
directories. Also see The SortedMap Interface section.</span></li>
</ul>
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">What are legacy classes and interfaces present in Collections framework ?</span><br />
<ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Enumeration ---Interface</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Dictonary ------Abstract class</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Hashtable -----Concrete class</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Properties -----Concrete class</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Vector -----Concrete class</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Stack -----Concrete class</span><span id="internal-source-marker_0.27181004528858055" style="background-color: transparent; color: black; font-family: Arial; font-size: 24px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;"> </span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span id="internal-source-marker_0.27181004528858055" style="background-color: transparent; color: black; font-family: Arial; font-size: 24px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">Basic collection types</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">The first part of the decision is choosing what </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">"basic category"</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> of organisation or functionality your data needs to have. The broad types are as follows:</span><table style="border-collapse: collapse; border: none;"><colgroup><col width="92"></col><col width="235"></col><col width="298"></col></colgroup><tbody>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Collection type</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Functionality</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Typical uses</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">List</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br /><ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Essentially a variable-size array;</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">You can usually add/remove items at any arbitrary position;</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">The order of the items is well defined (i.e. you can say what position a given item goes in in the list).</span></li>
</ul>
</td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Most cases where you just need to store or iterate through a "bunch of things" and later iterate through them.</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">Set</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br /><ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Things can be "there or not"— when you add items to a set, there's no notion of </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">how many times</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> the item was added, and usually no notion of ordering.</span></li>
</ul>
</td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br /><ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Remembering "which items you've already processed", e.g. when doing a web crawl;</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Making other </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">yes-no decisions</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> about an item, e.g. "is the item a word of English", "is the item in the database?" , "is the item in this category?" etc.</span></li>
</ul>
</td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">Map</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br /><ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Stores an </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">association</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> or mapping between "keys" and "values"</span></li>
</ul>
</td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Used
in cases where you need to say "for a given X, what is the Y"? It is
often useful for implementing in-memory caches or indexes. For example:</span><br /><ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">For a given user ID, what is their cached name/User object?</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">For a given IP address, what is the cached country code?</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">For a given string, how many instances have I seen?</span></li>
</ul>
</td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">Queue</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br /><ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Like a list, but where you </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">only ever access the </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">ends</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> of the list (typically, you add to one end and remove from the other).</span></li>
</ul>
</td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br /><ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Often used in </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">managing tasks</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">
performed by different threads in an application (e.g. one thread
receives incomming connections and puts them on a queue; other "worker"
threads take connections off the queue for processing);</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">For </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">traversing hierarchical structures</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> such as a filing system, or in general where you need to remember </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">"what data to process next"</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">, whilst also adding to that list of data;</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Related to the previous point, queues crop up in various algorithms, e.g. build the encoding tree for </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Huffman compression</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">.</span></li>
</ul>
</td></tr>
</tbody></table>
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </span><h3 dir="ltr" id="internal-source-marker_0.27181004528858055">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 19px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">Which Java List to use?</span></h3>
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">A
list is the simplest of structures. It keeps its elements in the same
order in which they are inserted and allows duplicates. There are
essentially three underlying list classes:</span><div dir="ltr">
<table style="border-collapse: collapse; border: none;"><colgroup><col width="161"></col><col width="223"></col><col width="241"></col></colgroup><tbody>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Class</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Features/implementation</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">When to use</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ArrayList</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br /><ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Allows elements to be efficiently read by index.</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Adding/removing the last element is efficient.</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Not synchronized in any way.</span></li>
</ul>
</td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">In most cases.</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">LinkedList</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br /><ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">First and last elements can be accessed efficiently;</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Other elements </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">cannot</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> be efficiently accessed by index;</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Not synchronized in any way.</span></li>
</ul>
</td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Effectively, functions as a non-synchronized </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">queue</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">.
In practice, rarely used: when you need a queue, you often need it to
be concurrent or to provide other functionality; other implementations
are often more useful.</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">CopyOnWriteArrayList</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><br /><ul style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Allows safe concurrent access;</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Reads are efficient and non-blocking;</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Modifications are not efficient (since a brand new copy of the list is taken each time).</span></li>
</ul>
</td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Where you need concurrent access and where frequency of reads far outweights frequency of modifications.</span></td></tr>
</tbody></table>
</div>
<br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">If
you need concurrent access to a list and CopyOnWriteArrayList is not
appropriate (either because the list is large or because reads don't
outnumber writes), then the best you can really do is place a
synchronized wrapper around an ordinary ArrayList:</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">List l = Collections.synchronizedList(new ArrayList());</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Note that this gives you </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">thread-safe</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> access, but it's not truly </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">concurrent</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> in the sense that each access to the list will lock the entire list during the access.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Remember if you do this that you must always </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">synchronize on the list while </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">iterating</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;"> over it</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">
(and in some cases this could be bad for concurrency). In practice, you
should think carefully whether this type of list makes much sense. If a
list is being continually altered by different threads, for example,
what value do the individual index numbers really have?</span><br /><h3 dir="ltr">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 19px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">Which Java Map to use?</span></h3>
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">The JDK provides various Map implementations depending on:</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">whether you need to maintain the </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">keys</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> in </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">sorted order</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">, in some </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">fixed order</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> or whether no particular order is required;</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">whether you require efficient </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">concurrent access</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> (i.e. where multiple threads can access the map efficiently and can perform </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">atomic updates</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> on the map).</span></li>
</ul>
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Depending on these requirements, the various Map implementations are as follows:</span><div dir="ltr">
<table style="border-collapse: collapse; border: none;"><colgroup><col width="192"></col><col width="165"></col><col width="226"></col></colgroup><tbody>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Ordering of keys</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Non-concurrent</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Concurrent</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">No particular order</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">HashMap</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><a href="http://www.javamex.com/tutorials/synchronization_concurrency_8_hashmap.shtml"><span style="background-color: transparent; color: #000099; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;">ConcurrentHashMap</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></a></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Sorted</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">TreeMap</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ConcurrentSkipListMap</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Fixed</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">LinkedHashMap</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">—</span></td></tr>
</tbody></table>
</div>
<br /><h3 dir="ltr">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 19px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">Which Java Set to use?</span></h3>
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Conceptually, a </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">set</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> serves to record </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">whether or not</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> an object belongs to a particular group. But a set is usually </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">implemented</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">
as a degenerate type of map, in which each item added to the set is
mapped to some special object meaning "present in the set". Therefore,
especially in the non-current case, the choices in deciding which set
implementation to use are largely similar to in the previous section: do
you need a predictable iteration order and/or concurrent access? The
available Set implementations are then as follows:</span><div dir="ltr">
<table style="border-collapse: collapse; border: none;"><colgroup><col width="192"></col><col width="165"></col><col width="226"></col></colgroup><tbody>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Ordering of keys</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Non-concurrent</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Concurrent</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">No particular order</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">HashSet</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">—</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Sorted</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">TreeSet</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ConcurrentSkipListSet</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: italic; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Fixed</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">LinkedHashSet</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">CopyOnWriteArraySet</span></td></tr>
</tbody></table>
</div>
<br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Perhaps
surprisingly, Java doesn't provide a set implementation based on
ConcurrentHashMap, though you could make such a subclass trivially
yourself. Another option that requires no code is to use a
ConcurrentSkipListSet, although you will be paying (in efficiency) for
sorting that you don't really need1.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">As
with CopyOnWriteArrayList (on which it is actually based), the
CopyOnWriteArraySet class is suited to cases where the set is relatively
small and reads far outweight writes. Any modification of the set is
expensive (since a brand new copy is created each time), but reads are
non-blocking.</span><ul style="margin-bottom: 0pt; margin-top: 0pt;">
</ul>
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-54783336834291289622012-07-20T14:45:00.001+05:302012-07-20T14:45:21.496+05:30What are cases that finally block wont be executed ?<div dir="ltr" style="text-align: left;" trbidi="on">
<span id="internal-source-marker_0.27181004528858055" style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">- when thread stops</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">- System.exit() called</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">- machine shut down</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">-
When a thread is set to be a daemon thread then the finally block may
not be executed. If it get sufficient time that other
threads(non-daemon) are executing then it will execute the finally
block.</span><br />
<span id="internal-source-marker_0.27181004528858055" style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">Example:</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">import java.util.concurrent.TimeUnit;</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">public class Multi implements Runnable{</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public void run(){</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> System.out.println("Starting thread");</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> try {</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> TimeUnit.SECONDS.sleep(2);</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> } catch (InterruptedException e) {</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> e.printStackTrace();</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> finally{</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> System.out.println("inside finally");</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> public static void main(String[] args)throws Exception {</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> Thread t = new Thread(new Multi());</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;"> t.setDaemon(true);</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> t.start();</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> //Main thread will sleep</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> Thread.sleep(1000);</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> }</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">}</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">OutPut:</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Starting thread</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Here if you comment the line </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;"> t.setDaemon(true);</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Then the finally block will be executed.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Here
the sleep time of the main thread is 1 sec(1000milisec). If you will
make it 5 second then you can see the finally block of the daemon thread
will be executed.</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> </span></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-19459910584087726392012-07-20T14:41:00.003+05:302012-07-20T14:41:56.828+05:30What is the difference between hashset and treeset ?<div dir="ltr" style="text-align: left;" trbidi="on">
<span id="internal-source-marker_0.6190310262125894" style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<table style="border-collapse: collapse; border: none;"><colgroup><col width="311"></col><col width="313"></col></colgroup><tbody>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt; text-align: center;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">HashSet</span></div>
</td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt; text-align: center;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;">TreeSet</span></div>
</td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">1The underlying data structure is Hashtable</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">1The underlying data structure is balanced tree</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">2Heterogeneous objects are allowed</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">2 Heterogeneous objects are not allowed by defalut</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">3Insertion order is not preserved and it is based on hashcode of the objects</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">3 Insertion order is not preserved and all the objects are inserted according to some sorting order.</span></td></tr>
<tr style="height: 0px;"><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">4null insertion is possible</span></td><td style="border: 1px solid #000000; padding: 7px 7px 7px 7px; vertical-align: top;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">4
As the first element only null insertion is possible and in all
other cases we will get NullPointerException (After null insertion other
insertion not possible)</span></td></tr>
</tbody></table>
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-80907740404494035152012-07-20T14:38:00.002+05:302012-07-20T14:38:43.597+05:30How to write Java documentation ?<div dir="ltr" style="text-align: left;" trbidi="on">
<span id="internal-source-marker_0.6190310262125894" style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Included in Sun’s Java Development Kit (JDK) is a program called javadoc that processes Java code files</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">and produces external documentation, in the form of HTML files, for your Java programs.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Tag Used for Purpose</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">@author name Interfaces,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Classes,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Interfaces</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Indicates the author(s) of a given piece of code.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">One tag per author should be used.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">@deprecated Interfaces,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Classes,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Member</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Functions</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Indicates that the API for the class… has been</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">deprecated and therefore should not be used any</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">more.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">@exception name description Member</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Functions</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Describes the exceptions that a member function</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">throws. You should use one tag per exception and</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">give the full class name for the exception.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">@param name description Member</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Functions</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Used to describe a parameter passed to a member</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">function, including its type/class and its usage.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Use one tag per parameter.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">@return description Member</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Functions</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Describes the return value, if any, of a member</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">function. You should indicate the type/class and</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">the potential use(s) of the return value.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">@since Interfaces,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Classes, Member</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Functions</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Indicates how long the item has existed, i.e. since</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">JDK 1.1</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">@see ClassName Classes,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Interfaces,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Member</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Functions, Fields</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Generates a hypertext link in the documentation to</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">the specified class. You can, and probably should,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">use a fully qualified class name.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">@see ClassName#member</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">functionName</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Classes,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Interfaces,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Member</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Functions, Fields</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Generates a hypertext link in the documentation to</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">the specified member function. You can, and</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">probably should, use a fully qualified class name.</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">@version text Classes,</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Interfaces</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Indicates the version information for a given piece</span><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">of code.</span></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-1235110526979374490.post-13055839881192954112012-07-20T14:27:00.001+05:302012-07-20T14:27:38.445+05:30What is RandomAccess Interface?<div dir="ltr" style="text-align: left;" trbidi="on">
<ul id="internal-source-marker_0.6190310262125894" style="margin-bottom: 0pt; margin-top: 0pt;">
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">If a collection class implements RandomAccess interface then we can access any of its element with the same speed.</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">RandomAccess interface is marker interface and it dosent contains any methods.</span></li>
<li style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; list-style-type: disc; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">ArrayList and vector classes implements this interface.</span></li>
</ul>
</div>Unknownnoreply@blogger.com0