Saturday, 25 August 2012

HOW TO Insert a code snippets in Blogger

Hello Readers,
Just while writing my first blog, I came across the problem that how can I insert a code snippet into my Blog. I then came across something called Syntax Highlighter. SyntaxHighlighter is a fully functional self-contained code syntax highlighter developed in JavaScript. 

In this post, I have tried to provide a simple way to insert a simple code snippet into your Blog.

Firstly, all you have to do is to create a simple blog containing all the text part without the code in it and save it as draft.

Then edit the blog in HTML mode an insert the following code just above the <HEAD> tab:

<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/> 
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'></script> 
<script language='javascript'> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
Now, there are two methods to insert your actual code into your blog:

1) Using a script Tag:
Just insert your code within the following into your HTML.
Using a script Tag can display an error.
This error does not allow you to draft your blog. You can fix this by simply inserting a 'space' before the <![CDATA[ like this < ![CDATA[ (dont forget to add a space at end and change ]]> to ]] >) then it wont count it as a broken tag and you can save it as draft. Just don't forget to revert it before you publish the blog.

2) Using the pre Tag:
Using a PRE Tag is simple and it does not involve any issue as in the script tag.
 
 // HTML Encoded code

I have always used the term 'HTML Encoded code' in the above examples to denote your code, wherein we must insert a HTML encoded form of the code when our code consists of tags which are similar to HTML tags.
For example, when using the following code snippet into your blog, you need to first encode the code into HTML to keep it from breaking:
Set<Object> set = new HashSet<Object>();

The syntax highlighting wont be proper if you don't encode the code containing the tags similar to HTML tags.
You can encode your code online using a simple HTML Encoder.

Also, there is a very easy way if you dont want to play around with the HTML code of your blog. You can use http://hilite.me/ to format source code. It supports lots of formats and outputs rather clean html. But if you have lots of code snippets then you have to do a lot of copy paste.

Please let me know if this post helped you in any way or if there are any other simple ways to achieve the same.
I'd love learning new things.

Please leave a comment if this post helped you or if there are any suggestions.

~SunMit.


Iterating over Collections in JAVA


The Collections API's  in JAVA were introduced to provide a simple way to handle the collection of objects.
Collections works a bit like arrays where unlike arrays, collections prove very important when you expect their size to change dynamically.
The Iterable interface (java.lang.Iterable) is one of the root interfaces of the Java collection classes.
The Collection interface extends Iterable, so all subtypes of Collection also implement the Iterable interface.

Considering the three most frequently used Collection types ie. Array List, Hash Set and Hash Map, this post will help you iterate over these collections.

Iterating using iterators :

1) Iterate over Array List:
List<Object> list = new ArrayList<Object>();
while(listIterator.hasNext()){
 Object object = listIterator.next();
 // do something with object
}

2) Iterate over Hash Set:

HashSet is backed by a HashMap. It makes no guarantees about the sequence of the elements when you iterate them.

LinkedHashSet differs from HashSet by guaranteeing that the order of the elements during iteration is the same as the order they were inserted into the LinkedHashSet.
Reinserting an element that is already in the LinkedHashSet does not change this order.
Set<Object> set = new HashSet<Object>();
Iterator setIterator = set.iterator();
while(setIterator.hasNext()){
 Object object = setIterator.next();
 // Do something with object
}

3) Iterate over Hash Map:

HashMap maps a key and a value. It does not guarantee any order of the elements stored internally in the map.
TreeMap also maps a key and a value and it guarantees the order in which keys or values are iterated - which is the sort order of the keys or values.

Iterating over a Hash Map is tricky as Hash map contains a key-value pair.
A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class.

Map<Object> map = new HashMap<Object>();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
 Entry thisEntry = (Entry) entries.next();
 Object key = thisEntry.getKey();
 Object value = thisEntry.getValue();
 // do something with key and value
}

Iterating using for-each loop and its advantages :

JAVA later introduced 'Enhanced for each loop iteration control', which extends the JAVA5 for-each loop to allow access to the loop index,  whether this is the first or last iteration, and to remove the current item.

MAJOR ADVANTAGE :

The for-each loop is almost certainly the most new popular feature from Java 5. It works because it increases the abstraction level - instead of having to express the low-level details of how to loop around a list or array (with an index or iterator), the developer simply states that they want to loop and the language takes care of the rest.
However, all the benefit is lost as soon as the developer needs to access the index or to remove an item. The original Java 5 for each work took a relatively conservative stance on a number of issues aiming to tackle the 80% case.
However, loops are such a common form in coding that the remaining 20% that was not tackled represents a significant body of code.

The process of converting the loop back from the for each to be index or iterator based is painful. This is because the old loop style if significantly lower-level, is more verbose and less clear. It is also painful as most IDEs don't support this kind of 'de-refactoring'.

MAJOR BENEFIT :

A common coding idiom is expressed at a higher abstraction than at present. This aids readability and clarity.


# Iterating over Array list and Hash Set using for-each loop is similar and simple.
for(Object object: list){
 // Do something with object
}
for(Object object: set){
 // Do something with object
}

# Iterating over Hash Map:
The simplest method to iterate over a Hash Map is by using a for-each loop.
for (Map.Entry<Object> entry : map.entrySet()){
 Object key = thisEntry.getKey();
 Object value = thisEntry.getValue();
 // do something with key and value
}

I have used stuff from JAVA Tutorials provided by Oracle and some answers I got from
Stack Overflow. You can find these things online out there, but I have tried using those 
things in a simple way and that you can find at one place.
Please let me know if I am wrong or if there are any other easy and efficient methods that can be used to iterate over these collections.
I would love learning something more.


Please leave a comment if this post helped you or if there are any suggestions.


~SunMit

Friday, 24 August 2012

My First Blog

Hello Readers,
I had wished to write my own blog for a long time. Always wondered what the content of my blog would be. Whether it should be technical or just some random stuff that I would like sharing.
So here I am, trying to put everything that I have learned and will be learning throughout my journey.

Being a Software Engineer, I believe most of the things I post will be technical, the new things that I will be learning. The tricks that might be useful to all the new professionals who get into this industry.

That's all for now. Will update my first blog later when I myself get a clear idea about my strengths and key areas so that I can help the readers in developing themselves too.

Always keen to learn new things.

~SunMit