Showing posts with label CoreJava. Show all posts
Showing posts with label CoreJava. Show all posts

Couchbase Server Create-Read-Update-Delete Java Example

Couchbase Server was originally known as Membase, is an open-source, distributed multi-model NoSQL document-oriented database software package.

If you are writing large enterprise application then Caching is really important where you want to minimized access to database and access only if in case if it's really needed.

They never have to go to disk for a query so they are faster than a typical database.
Also databases often have to do some work to join data together from multiple tables, whereas a cache usually just stores data in a key/value.
This means the cache never has to actually process anything. It just does straight lookups.

Java 8 Sorting using Lambda Expression example

I have array containing names of my friends. I want to sort those name so I can get my friends's name sorted  by name alphabetically.

Persons Array
String[] persons = {"Anuj Patel", "Munjal Thakkar", "Jigar Shah", "Bhargav Patel", "Harish Raghwan","Zvika Chananel"};

Print Array using Lambda Expressions
System.out.print("*** Before Sorting ***:\n");
Arrays.asList(persons).forEach((person) -> System.out.println(person));

Here, we are creating List from Array and saying that for each person, print person.
Lambda expression is (Parameter) -> Body in simple words. It's more readable form.

Java 8 Threading Using Lambda expression Example

As we already know that In Java, we can create thread using two ways - using extends and another is using Runnable.

Normally we create Thread using below ways.
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Threading Using old way !");
}
});
t1.start();

We write codes because we are interested in doing something. we write something and expecting some output. We are more interested in outcome that our code is supposed to provide. here we expect our code to print "Threading Using old way!" once it runs.
But It seems we are writing extra codes for simple thing !

Encrypting and Decrypting Images Using BlowFish

Blowfish is a symmetric-key block cipher, designed in 1993 by Bruce Schneier and included in a large number of cipher suites and encryption products.
For more information, refer to http://en.wikipedia.org/wiki/Blowfish_(cipher)

To Encrypt and Desrypt Images , follow following steps

Encrypting and Decrypting Images Using BlowFish :
1. Create SecretKey which will be used for Encryption
SecretKey secretKey = new SecretKeySpec(password.getBytes(), "Blowfish");

2. Create Cipher Instance
Cipher cipher = Cipher.getInstance("Blowfish");

3. To Encrypt, Initialize Cipher using secretKey
cipher.init(Cipher.ENCRYPT_MODE, secretKey);

Scheduling Task using Timer in Java

Java provides class called Timer using which you can schedule task at specific intervals.

Signature of Schedule method of Timer class follows as 
schedule(TimerTask task, Date firstTime, long period)

Here, task is the task to be executed
firstTime is firstTime at which task to be executed first
period is interval time in milliseconds at which task will be executed automatically at this interval.

Creating Thread using Extend and Runnable in Java

In Java, You can create thread using 2 ways.

1. Using extend Thread
  •  MyThread t1 = new MyThread("Thread t1"); 
  • where MyThread is class which extends Java's Thread Class.
2. Using implements Runnable
  •    MyRunnable runnable = new Runnable();
  •    Thread t1 = new Thread(runnable,"Thread t1");   
  •     where MyRunnable is class which implements Runnable Interface.

Implement your own LinkedList using Java

LinkedList consist of Nodes where each Node represent two things. One is Data and other is reference to Next node.

Let's check How you can create your own LinkList Implementation :)
  1. Create Node class containing two things. Object (basically data which node will hold) and Node which is reference to Next Node
  2. Create Your Own linkedList class which will have following methods.
    1. add(Object data) - add element to tail of LinkedList
    2. add(Object data,int index) - create new Node and add that new node containing given data at specific index.

Immutable Collections Vs Unmodifiable Collections in Java

Unmodifiable Collection is kind of collection which is readonly view of Collections. You can not do modification into this kind of collections. They always present Latest Value. If any of source collection changes then unmodifiable collection contains those changes.

While Immutable Collections are such kind of Collection which is readonly copy of Collections. They can not be modified. if any of source collection changes then immutable collections will not reflect with changes.

Runtime Polymorphism in Java

Polymorphism means ability to take more than one form. In RunTime Polimorphism resolution to call to method is determined at runtime rather than compile time.
  • Method are overriden but data member are not so runtime polymorphism is not applied to data members

Insertion Sort Algorithm using Java

Insertion Sort is simple sort algorithm. In insertion sort, we pickup element and find correct place of element where it needs to be inserted and insert that element at correct place by moving elements to right side.We will keep doing same until we have all sorted elements available on left side.

Quick Sort Algorithm using Java

Quick Sort works on Divide and Conquer algorithm. Here, given array is divided into two parts, left and right. and both array are being sorted individually. After then we combine and will have sorted array.

How it works:
  1. Pick up any element in Array. ex. middle element of array. we called this one as Pivot.
  2. All elements which are smaller than Pivot are placed in one array and All elements which are larger than pivot are placed in another array
  3. Using recursion sort both array using quicksort
  4. Combine array as mentioned below.

Bubble Sort Algorithm using Java

In Bubble Sort, smaller elements bubbles to top of list in ascending sorting hence it's called Bubble Sort.

How it works :
  1. In Bubble Sort, You take first element and second element initially and compare both elements.
  2. If second element is smaller than first element then you swipe places. how? using temp :) as below
  3. You keep doing same meaning stepping through array and comparing element with pair of adjacent elements until no swapping required and you have sorted array

Binary Search Algorithm using Java

Binary search works faster than sequential search.It search on sorted elements.
In Binary Search, Let's say You have an array and you want to search specific item in array

How it Works :
  1. Initialize low=0 and high as max length of search array - 1.
  2. while low<=high each item you, take middle element of array and if given searchItem is less than middle element then you need to search in array of elements array which are less than middle element. Hence, high = middle - 1

Sequential Search Algorithm using Java

Sequential Search is simplest algorithm. You try to search given element in given collections sequentially and return only if you found element.

RSA Public Key Encryption and Private Key Decryption using Java

In Previous Post, I have discussed about What is Cryptography, What are Types of it and How we can encrypt and decrypt data using DES Algorithm in Java

Asymmetric Cryptography :
  • Here, We share Public Key with others so they can encrypt data using Public Key
  • We can only decrypt data using Private Key associated with Public Key. Private key is secret one and we don't share with anyone.
  • Most common used algorithm is RSA ALGORITHM. named after Rivest,Shamir and Adleman

Calibration Data Missing in Netbeans 7.3

While I was doing profiling using netbeans 7.3,I encountered with Issue "Calibration Data Missing" when we are using profiling first time and after reading netbeans documentation found solutions which is mentioned as below.

To Solve, "Calibration Data Missing" Issue,

TreeMap Example in Java

TreeMap maintains ascending order and does not allows null as Key which differs in HashMap and LinkedHashMap.

About TreeMap:
  • It extends AbstractMap class and Implements NavigableMap Interface
  • It maintain ascending order
  • It does not allow null as Key

LinkedHashMap Example in Java

LinkedHashMap is used for Key-Value concepts. when we require to store values based on Key then LinkedHashMap is for You !But It maintains insertion order while HashMap does not.

About LinkedHashMap:
  • It extends HashMap class and Implements Map Interface
  • It maintain insertion order
  • Allows null as Key and Values also

HashMap Example in Java

HashMap is used for Key-Value concepts. when  we require to store values based on Key then HashMap is for You !

About HashMap :
  • HashMap is class which extends AbstractMap and Implements Map Interface
  • HashMap does not maintain insertion order

TreeSet Example in Java

About TreeSet:
  • TreeSet is class which extends AbstractSet and Implements NavigableSet Interface
  • It does maintain ascending order
  • It does not allow null
  • Contains unique elements Only, same as HashSet and LinkedHashSet