Saturday, April 20, 2019

Linkedlist Java

Key facts about Linkedlist
·  LinkedList can have duplicate and null values.·  The LinkedList class implements Queue and Deque interfaces. Therefore, It can also be used as a QueueDeque or Stack.
·  Java LinkedList is not thread-safe. You must explicitly synchronize concurrent modifications to the LinkedList in a multi-threaded environment.·  Java LinkedList maintains the insertion order of the elements.

Linkedlist Example:import java.util.*;
public class LinkedListExample {
     public static void main(String args[]) {
       /* Linked List Declaration */
       LinkedList<String> testlist = new LinkedList<String>();
       /*add(String Element) is used for adding 
        * the elements to the linked list*/
       /*Display Linked List Content*/
       System.out.println("Linked List Content: " + testlist);
       /*Add First and Last Element*/
       testlist.addFirst("First Item");
       testlist.addLast("Last Item");
       System.out.println("LinkedList Content after addition: " + testlist);
       /*This is how to get and set Values*/
       Object firstvar = testlist.get(0);
       System.out.println("First element: " +firstvar);
       testlist.set(0, "Changed first item");
       Object firstvar2 = testlist.get(0);
       System.out.println("First element after update by set method: " +firstvar2);
       /*Remove first and last element*/
       System.out.println("LinkedList after deletion of first and last element: " + testlist);
       /* Add to a Position and remove from a position*/
       testlist.add(0, "Newly added item");
       System.out.println("Final Content: " + testlist); 
       /* Access specific element in the Linkedlist*/
       testlist.indexOf(“Newly added item”);

       /*Iterator for Linkedlist*/
       Iterator<String> testlistIterator = testlist.iterator();
        while (testlistIterator.hasNext()) {
            String speciesName =;

Applications of linked list in real world-
  1. Image viewer – Previous and next images are linked, hence can be accessed by next and previous button.
  2. Previous and next page in web browser – We can access previous and next url searched in web browser by pressing back and next button since, they are linked as linked list.
  3. Music Player – Songs in music player are linked to previous and next song. you can play songs either from starting or ending of the list.

Monday, April 15, 2019

AWS Lamdba: Exception in thread "main" java.lang.Error: java.lang.OutOfMemoryError: Metaspace

Execution of newly created Lambda gives below error

Exception in thread "main" java.lang.Error: java.lang.OutOfMemoryError: Metaspace

Bydefault the Memory allocated to Lambda is 128MB this needs to be increased


Navigate to your Lambda in AWS and update the Basic setting increase the Memory and test it out

Saturday, March 30, 2019

JSON Serialization and Deserialization

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation.
When transmitting data or storing them in a file, the data are required to be byte strings, but complex objects are seldom in this format. Serialization can convert these complex objects into byte strings for such use. After the byte strings are transmitted, the receiver will have to recover the original object from the byte string. This is known as deserialization.
Say, you have an object
{foo: [1, 4, 7, 10], bar: "baz"}
serializing into JSON will convert it into a string:
which can be stored or sent through wire to anywhere. The receiver can then deserialize this string to get back the original object. {foo: [1, 4, 7, 10], bar: "baz"}.

DynamoDB Read/Write Capacity

Read Request Units and Write Request Units

  • One read request unit represents one strongly consistent read request, or two eventually consistent read requests, for an item up to 4 KB in size. Transactional read requests require 2 read request units to perform one read for items up to 4 KB. If you need to read an item that is larger than 4 KB, DynamoDB needs additional read request units. The total number of read request units required depends on the item size, and whether you want an eventually consistent or strongly consistent read. For example, if your item size is 8 KB, you require 2 read request units to sustain one strongly consistent read, 1 read request unit if you choose eventually consistent reads, or 4 read request units for a transnational read request.
  • One write request unit represents one write for an item up to 1 KB in size. If you need to write an item that is larger than 1 KB, DynamoDB needs to consume additional write request units. Transactional write requests require 2 write request units to perform one write for items up to 1 KB. The total number of write request units required depends on the item size. For example, if your item size is 2 KB, you require 2 write request units to sustain one write request or 4 write request units for a transnational write request. 
    Read Consistency

    Eventually Consistent Reads
    When you read data from a DynamoDB table, the response might not reflect the results of a recently completed write operation. The response might include some stale data. If you repeat your read request after a short time, the response should return the latest data.
    Strongly Consistent Reads
    When you request a strongly consistent read, DynamoDB returns a response with the most up-to-date data, reflecting the updates from all prior write operations that were successful. A strongly consistent read might not be available if there is a network delay or outage. Consistent reads are not supported on global secondary indexes (GSI).

    DynamoDB uses eventually consistent reads, unless you specify otherwise. Read operations (such as GetItem, Query, and Scan) provide a ConsistentRead parameter. If you set this parameter to true, DynamoDB uses strongly consistent reads during the operation. 

Friday, March 15, 2019

Modulo Operator: Java

Java has one important arithmetical operator you may not be familiar with, %, also known as the modulus or remainder operator. The % operator returns the remainder of two numbers. 

So 20 modulo 5 is 0 because 20 divided by 5 is 4 with no remainder.
    21 modulo 5 is 1 22 modulo 5 is 2 23 modulo 5 is 3 24 modulo 5 is 4 25 modulo 5 is 0
In C, C++ and Java, modulo is represented as the percent sign. So
    int a = 20 % 5 ;

The most common use case for the modulo operator is to find out if a given number is odd or even.
Modulo has a variety of uses. If you want to know if a number is an even "hundred", like 300, 400, 500 or 256700, you can test for it like this:
    if ( ( a % 100 ) == 0 )
        System.out.println( a + "exactly!");

If the outcome of the modulo operation between any number and two is equal to one, it’s an odd number:
public void whenDivisorIsOddAndModulusIs2_thenResultIs1() {
    assertThat(3 % 2).isEqualTo(1);
On the other hand, if the result is zero (i.e. there is no remainder), it’s an even number:
public void whenDivisorIsEvenAndModulusIs2_thenResultIs0() {
    assertThat(4 % 2).isEqualTo(0);