Friday 3 November 2017

Node JS | Hello World in Node JS

A Node.js application consists of the following three important components. Components are:

1. Import required modules – the require directive to load Node.js modules.

2. Create server − A server which will listen to client's requests similar to Apache HTTP Server.

3. Read request and return response − Created server will read the HTTP request made by the client who can be a browser or a console and return the response.


<!-- 1. Import required modules -->
var http = require("http");

<!-- 2. Create server -->
http.createServer(function (request, response) {

   // Send the HTTP header
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
  
   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

Output: Open the browser with http://127.0.0.1:8081/.

Wednesday 1 November 2017

Java 9 | Factory Method for Collections: List, Set, Map

Java 9 provides new static factory methods for creating instances of collections and maps conveniently with a small number of elements. Here, we are going to discuss how to create List, Set, Map with Java 9 Factory Method for Collections.

1. List
Create an Immutable List in Java 8
In Java 8, we can create the Immutable list using the Collections utility class method. Check the below example of the same:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ImmutableListJ8 {
     public static void main(String[] args) {
          
           List<String> list = new ArrayList<>();
           list.add("Java 8");
           list.add("java 9");
           System.out.println("Mutable list "+list);
          
           List<String> imList = Collections.unmodifiableList(list);
           System.out.println("Immutable list "+imList);
          
           /** There will be java.lang.UnsupportedOperationException
            *  while modifying the Immutable list. */
           imList.add("java");
           System.out.println(imList);
     }
}

Create an Immutable List in Java 9
In Java 9, we can create an immutable list using interface static methods as described in the below example:
import java.util.List;
public class ImmutableListJ9 {
     public static void main(String[] args) {
          
           List<String> immList = List.of("Java 8","Java 9");
          
           System.out.println(immList);
          
           immList.add("java");
     }
}

There are few static methods to create an immutable List
// for empty list
static <E> List<E> of()
// for list containing one element
static <E> List<E> of(E e1)
// for list containing two element
static <E> List<E> of(E e1, E e2)
// ...
// for list containing an arbitrary number of elements
static <E> List<E> of(E... elements)

It returns an immutable list containing an arbitrary number of elements. See Immutable List Static Factory Methods for details.

Type Parameters: <E> the List's element type
Parameters: elements the elements to be contained in the list
Returns: a List containing the specified elements
Throws: NullPointerException - if an element is null or if the array is null
Since: 9

Important points
1. There will be NullPointerException while creating the immutable list with null.
import java.util.List;

public class ImmutableListJ9 {
     public static void main(String[] args) {
           List<String> immList = List.of(null,"Java 9");
           System.out.println(immList);
     }
}
Output:
Exception in thread "main" java.lang.NullPointerException
     at java.base/java.util.Objects.requireNonNull(Objects.java:221)
     at java.base/java.util.ImmutableCollections$List2.<init>(ImmutableCollections.java:185)
     at java.base/java.util.List.of(List.java:822)
     at ImmutableListJ9.main(ImmutableListJ9.java:6)

2. As the property of Immutable list, the program will throw an UnsupportedOperationException while adding the element to the Immutable list.
import java.util.List;
public class ImmutableListJ9 {
     public static void main(String[] args) {
           List<String> immList = List.of("Java 9");
           System.out.println(immList);
          
           immList.add("Java 8");
     }
}
Output:
[Java 9]
Exception in thread "main" java.lang.UnsupportedOperationException
     at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71)
     at java.base/java.util.ImmutableCollections$AbstractImmutableList.add(ImmutableCollections.java:77)
     at ImmutableListJ9.main(ImmutableListJ9.java:8)

Solution for above problem
import java.util.ArrayList;
import java.util.List;

public class ImmutableListJ9 {
     public static void main(String[] args) {
           List<String> immList = new ArrayList<>(List.of("Java 9"));
           System.out.println(immList);
          
           immList.add("Java 8");
          
           System.out.println(immList);
     }
}
Output:
[Java 9]
[Java 9, Java 8]


2. Set
Create an Immutable Set in Java 8
In Java 8, we can create the Immutable Set using the Collections utility class method. Check the below example of the same:
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class ImmutableSetJ8 {
     public static void main(String[] args) {
          
           Set<String> set = new HashSet<>();
           set.add("Java 8");
           set.add("java 9");
           System.out.println("Mutable set "+set);
          
           Set<String> imSet = Collections.unmodifiableSet(set);
           System.out.println("Immutable set "+imSet);
          
           /** There will be java.lang.UnsupportedOperationException
            *  while modifying the Immutable set. */
           imSet.add("java");
           System.out.println(imSet);
     }
}

Create an Immutable Set in Java 9
In Java 9, we can create an immutable set using interface static methods as described in the below example:
import java.util.Set;
public class ImmutableListJ9 {
     public static void main(String[] args) {
           Set<String> imSet = Set.of("Java 8","Java 9");
           System.out.println(imSet);

           imSet.add("java");
     }
}
There are few static methods to create an immutable Set
// for empty set
static <E> Set<E> of()
// for set containing one element
static <E> Set<E> of(E e1)
// for set containing two element
static <E> Set<E> of(E e1, E e2)
// ...
// for set containing an arbitrary number of elements
static <E> Set<E> of(E... elements)

It returns an immutable set containing an arbitrary number of elements. See Immutable Set Static Factory Methods for details.

Type Parameters: <E> the Set's element type
Parameters: elements the elements to be contained in the set
Returns: a Set containing the specified elements
Throws: NullPointerException - if an element is null or if the array is null
Since: 9

Important points
1. There will be NullPointerException while creating the immutable set with null.
import java.util.Set;

public class ImmutableSetJ9 {
     public static void main(String[] args) {
           Set<String> immSet = Set.of(null, "Java 9");
          
           System.out.println(immSet);
     }
}
Output:
Exception in thread "main" java.lang.NullPointerException
     at java.base/java.util.ImmutableCollections$Set2.<init>(ImmutableCollections.java:379)
     at java.base/java.util.Set.of(Set.java:483)
     at ImmutableSetJ9.main(ImmutableSetJ9.java:6)

2. As the property of an Immutable set, the program will throw an UnsupportedOperationException while adding the element to Immutable set.
import java.util.Set;
public class ImmutableSetJ9 {
     public static void main(String[] args) {
           Set<String> set = Set.of("Java 9");
           System.out.println(set);

           set.add("Java 8");
           System.out.println(set);
     }
}
Output:
[Java 9]
Exception in thread "main" java.lang.UnsupportedOperationException
     at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71)
     at java.base/java.util.ImmutableCollections$AbstractImmutableSet.add(ImmutableCollections.java:281)
     at ImmutableSetJ9.main(ImmutableSetJ9.java:9)

Solution for above problem
import java.util.HashSet;
import java.util.Set;

public class ImmutableSetJ9 {
     public static void main(String[] args) {
           Set<String> set = new HashSet<>(Set.of("Java 9"));
           System.out.println(set);

           set.add("Java 8");
           System.out.println(set);
     }
}
Output:
[Java 9]
[Java 8, Java 9]


3. Map
1. Map.of()
To create a Map, we use those static methods:
import java.util.Map;
public class ImmutableMapJ9 {
     public static void main(String[] args) {
           Map<String, Integer> map = Map.of("Java 8", 8, "Java 9", 9);
           System.out.println(map);
     }
}
Output: {Java 9=9, Java 8=8}

Factory methods to create the Map
// for empty Map
static <K, V> Map<K, V> of()
//for Map containing a single mapping
static <K, V> Map<K, V> of(K k1, V v1)
//for Map containing two mappings
static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2)
//...
//for Map containing up to ten mappings
static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
                            K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
2. Map.ofEntries()
import java.util.Map;

public class ImmutableMapJ9 {
     public static void main(String[] args) {
           Map<Integer, String> newImmutableMap = Map.ofEntries(Map.entry(1, "one"), Map.entry(2, "two"),
                     Map.entry(3, "three"));
           System.out.println(newImmutableMap);
     }
}
Output: {2=two, 3=three, 1=one}
Important points
1. There will be NullPointerException while creating the immutable map with null values.
import java.util.Map;

public class ImmutableMapJ9 {
     public static void main(String[] args) {
           Map<String, String> map = Map.of("Java 8", "8" , "Java 9", null);
           System.out.println(map);
     }
}
Output:
Exception in thread "main" java.lang.NullPointerException
     at java.base/java.util.Objects.requireNonNull(Objects.java:221)
     at java.base/java.util.ImmutableCollections$MapN.<init>(ImmutableCollections.java:678)
     at java.base/java.util.Map.of(Map.java:1326)
     at ImmutableMapJ9.main(ImmutableMapJ9.java:5)

2. As the property of an Immutable set, the program will throw an UnsupportedOperationException while adding the element to the Immutable map.
import java.util.Map;

public class ImmutableMapJ9 {
     public static void main(String[] args) {
           Map<String, String> map = Map.of("Java 8", "8" , "Java 9", "9");
           System.out.println(map);
          
           map.put("Java", "2");
     }
}
Output:
{Java 9=9, Java 8=8}
Exception in thread "main" java.lang.UnsupportedOperationException
     at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71)
     at java.base/java.util.ImmutableCollections$AbstractImmutableMap.put(ImmutableCollections.java:558)
     at ImmutableMapJ9.main(ImmutableMapJ9.java:8)

Solution for above problem
import java.util.HashMap;
import java.util.Map;

public class ImmutableMapJ9 {
     public static void main(String[] args) {
           Map<String, String> map = new HashMap<>(Map.of("Java 8", "8" , "Java 9", "9"));
           System.out.println("Map: "+map);
          
           map.put("Java", "2");
           System.out.println("Modified Map: "+map);
     }
}
Output:
Map: {Java 8=8, Java 9=9}
Modified Map: {Java=2, Java 8=8, Java 9=9}

Related Posts Plugin for WordPress, Blogger...