Java Collections

December 16, 2012

A collection is a object that groups multiple elements into a single unit.

List of core collection Java interfaces.

  • Collection – This interface is the least common denominator that all collections implement and is used to pass collections around and manipulate them where maximum generality is desired.
  • Set – A collection that cannot contain duplicate elements
  • List – A ordered collection that can contain duplicate elements. Elements can be accessed by their integer index.
  • Queue – A collection which orders elements in first-in, first-out manner.
  • Map – An object which maps keys to values. There cannot be duplicate keys.
  • SortedSet – A set that maintains its elements in ascending order.
  • SortedMap – A map that maintains its mappings in ascending order.

Ways to traverse a collection.

  • for-each
for (Object o : collection)
    System.out.println(o);
  • Iterators – It enables to traverse a collection and to remove elements selectively. Iterator.remove is the only safe way to modify a collection during iteration
static void filter(Collection<?> c) {
    for (Iterator<?> it = c.iterator(); it.hasNext(); )
        if (!cond(it.next()))
            it.remove();
}

toArray method – translates collection to array.

Object[] a = c.toArray();
String[] a = c.toArray(new String[0]);

Set Implementations.

  • HashSet – Stores elements in hash table. Good performance, but makes no guarantee about order.
  • TreeSet – Stores elements in red-black tree, orders elements by values, slower than HashSet. SortedSet implementation.
  • LinkedHashSet – Implements a hash table with a linked list running through it, orders elements based on the order in which they were inserted.

List Implementations.

  • ArrayList – provides constant time positional access and is fast
  • LinkedList – offers better performance under certain circumstances. If you add elements to beginning of list or iterate over the list to delete elements.

Map Implementations.

  • HashMap – Provides maximum speed, but not ordered.
  • TreeMap – Ordered by the keys. SortedMap implementation.
  • LinkedHashMap – Provides intermediate performance of the above two implementation. Ordered by insertion.

References

http://docs.oracle.com/javase/tutorial/collections/interfaces/index.html

Advertisements

REST Principles

December 13, 2012

REST stands for Representational State Transfer

5 key principles of REST are –

  • Give everything a ID

Examples
http://example.com/orders/2007/11
http://example.com/products?color=green

  • Link things together

Links help to refer to identifiable things

  • Use standard methods

GET- To retrieve a representation. Its idempotent, meaning that has no additional effect if it is called more than once with the same input parameters
PUT – update this resource with this data, or create it at this URI if it’s not there already. Its idempotent.
DELETE – delete a resource. Its idempotent.
POST – create a new resource. Its not idempotent.

  • Resources with multiple representations

Provide multiple representations of resources for different needs.

  • Communicate statelessly

A server should not have to retain some sort of communication state for any of the clients it communicates with beyond a single request. The most obvious reason for this is scalability — the number of clients interacting would seriously impact the server’s footprint if it had to keep client state.

https://www.ibm.com/developerworks/webservices/library/ws-restful/
http://tomayko.com/writings/rest-to-my-wife
http://www.infoq.com/articles/rest-introduction


Javascript Object oriented programming

August 22, 2012
  • JavaScript is a prototype-based language which contains no class statement. Instead JavaScript uses functions as classes. Defining a class is as easy as defining a function.
  • An object constructor/object constructor function is a function that’s used to define a new object. In this function, we declare the initial properties/methods of the new object, and usually assign them a pre-defined value.
  • To create an instance of an object, we use the keyword “new”, followed by an object constructor. We can either use JavaScript’s built-in constructors to create a native object, or we can build our own constructors to create a user-defined object.
  • Every object method has a variable – this – which refers to the current instance of that object from which the method is called.

Example

     function Computer(name) {
          this.name=name;
          this.getName=function() {
             return this.name;
          }
     }

     var computer1 = new Computer("Desktop-1");
     alert(computer1.getName());//alerts Desktop-1
     var computer2 = new Computer("Desktop-2");
     alert(computer2.getName());//alerts Desktop-2

References

http://www.sitepoint.com/oriented-programming-1/

https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript


Javascript Closures

August 17, 2012

A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created.

https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Closures


Javascript Module Pattern

August 8, 2012

Module Pattern – It stays out of the global namespace, provides publicly addressable API methods, and supports protected or “private” data and methods along the way.

http://www.yuiblog.com/blog/2007/06/12/module-pattern/


javascript void(0) for HTML link

May 27, 2012

To create a HTML link which does nothing when user clicks it,  use  like below


<A HREF="javascript:void(0)">Click here to do nothing</A>

When the user clicks the link, void(0) evaluates to undefined, which has no effect in JavaScript.

References –

https://developer.mozilla.org/en/JavaScript/Guide/Expressions_and_Operators#void

Here is a stackoverflow.com link, which answers why  void(0)  is better than using #  for this requirement

http://stackoverflow.com/questions/134845/href-for-javascript-links-or-javascriptvoid0


Referencing variables in setTimeout function – Javascript Closures

May 26, 2012

Requirement

– Reference For loop variable i  in setTimeout function

Wrong way

for(var i = 1 ; i <= 10; i++) {
console.log('i in for :: ' + i);
setTimeout(function() {
console.log(' i in timeout :: ' + i);
}, 2000);
}

Ouput

i in for :: 1
i in for :: 2
i in for :: 3
i in for :: 4
i in for :: 5
i in for :: 6
i in for :: 7
i in for :: 8
i in for :: 9
i in for :: 10
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11

Right way using Closures

for(var i = 1 ; i <= 10; i++) {
console.log('i in for :: ' + i);
setTimeout(function(j) {
return function() {
console.log(' j in timeout :: ' + j);
}
}(i), 2000);
}

Ouput

i in for :: 1
i in for :: 2
i in for :: 3
i in for :: 4
i in for :: 5
i in for :: 6
i in for :: 7
i in for :: 8
i in for :: 9
i in for :: 10
j in timeout :: 1
j in timeout :: 2
j in timeout :: 3
j in timeout :: 4
j in timeout :: 5
j in timeout :: 6
j in timeout :: 7
j in timeout :: 8
j in timeout :: 9
j in timeout :: 10

References  –
http://jibbering.com/faq/notes/closures/

https://developer.mozilla.org/en/JavaScript/Guide/Closures