Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, 28 August 2014

Java + MongoDB Hello World Examle

A simple Java + Maven  + MongoDB  hello world example.

In this Post we will see step by step how to connect MongoDB, create database, collection and document, save, update, remove, get and display document.



Wednesday, 6 August 2014

How to generate MD5 Hash in Java


There are multiple ways to generate MD5 hash in Java Program. Not only Java API provides convenient method to generate MD5 hash, you can also use popular open source frameworks, like Spring and Apache commons Codec to generate MD5 digest in Java.

 MD5 is popular Message Digest Algorithm, which is most commonly used to check data integrity e.g. comparing MD5 check-sum to see, if any file is altered or not. Though, MD5 was not considered a good cryptographic algorithm for security purpose due to several vulnerability found on it, it's still good enough or cheking integrity of file. 

How do I encode URL parameters

When using the GET method, parameter names and their values get submitted on the URL string after a question mark. Different parameter name/value pairs are separated by ampersands.

However, what happens if the parameter value in the URL string has a space or some other special character? Submitting an HTML form will result in special characters being automatically converted to special codes for us. However, sometimes it is necessary to encode these characters without the aid of an HTML form. Java  has a class called URLEncoder that can encode these characters so that they are properly handled. The encode method takes two parameters, the string to be encoded and the encoding scheme. Unless you're smoking crack, you probably want the encoding scheme to be "UTF-8".

How to read a properties file with a Resource Bundle

A ResourceBundle offers a very easy way to access key/value pairs in a properties file in a Java.
ResourceBundle class is used to internationalize the messages. In other words, we can say that it provides a mechanism to globalize the messages.

The hardcoded message is not considered good in terms of programming, because it differs from one country to another. So we use the ResourceBundle class to globalize the massages. The ResourceBundle class loads these informations from the properties file that contains the messages.

Tuesday, 5 August 2014

Find Out Your Java Heap Memory Size

In this article, we will show you how to use the -XX:+PrintFlagsFinal to find out your heap size detail. In Java, the default and maximum heap size are allocated based on this – ergonomics algorithm.


Heap sizes
Initial heap size of 1/64 of physical memory up to 1Gbyte
Maximum heap size of 1/4 of physical memory up to 1Gbyte

Monday, 4 August 2014

Java Virtual Machine Internals

This article explains the internal architecture of the Java Virtual Machine (JVM). 






Sunday, 27 July 2014

Examples on String Class in Java

In this post we will see some examples on String Class in java

How to reverse a String


public class StringReverseExample{
   public static void main(String[] args){
      String string="abcdef";
      String reverse = new StringBuffer(string).
      reverse().toString();
      System.out.println("\nString before reverse:
      "+string);
      System.out.println("String after reverse:
      "+reverse);
   }
}

Stuff about String class

In this Post we will discuss about some important concepts of String Class in Java.

Memory leak issue


 Have you tried creating substrings from a string object. Do you know the internals of substring in java. How they create memory leaks?

Sorting a Map In Java

In this Post we will see how to sort Map by its keys and by values.


1. Sort a Map by Keys

Uses TreeMap, and keys are sorted automatically.

SortMapOnKeyExample.java

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class SortMapOnKeyExample {

Identify Duplicated Items In Java List and count

The Below example show you how to count the total number of duplicated entries in a List, using Collections.frequency and Map

Saturday, 26 July 2014

Collections Looping Example

Concept of the Iterator
An iterator is an object that enables us to traverse a collection. There is an iterator (java.util.Iterator) in all the top level interfaces of the Java Collections Framework that inherits java.util.Collection interface. These interfaces are java.util.List,java.util.Queue, java.util.Deque, and java.util.Set. Furthermore, there is the java.util.Map interface that does not inheritjava.util.Collection.

Sorting List Collections Examples

In Java, it’s pretty easy to sort elements in a list collection using the Collections.sort() static utility method. This method has two forms as follows:
    • void sort(List<T> list): sorts the list into ascending order according to its elements’ natural ordering.
    • void sort(List<T> list, Comparator<? super T> c): sorts the list into the order specified by the given comparator.
Behind the scenes, the sort operation uses a merge sort algorithm which is slightly optimized to be fast and stable:

Lambda - Collections Comparator Example

The following examples illustrate how to use Lambda expressions - the main feature coming with Java SE 8 - to improve the boilerplate code of comparator written for sorting list collections.

Examples for enum type

The enum keyword is used to declare a new enumeration type. This keyword has been introduced since Java 5. The declaration syntax for an enum type is as follows

How to make a java class immutable

An immutable class is one whose state can not be changed once created. There are certain guidelines to create an class immutable. In this post, we will revisit these guidelines.

Here we will Discuss below sections
  • Benefits of making a class immutable
  • Guidelines to make a class immutable

Friday, 18 July 2014

How To Bypass Certificate Checking In A Java Jersey WebService Client


How To Bypass Certificate Checking In A Java Jersey WebService Client


To Turn Off Certificate Validation in Java HTTPS Connections 
This can be done by replacing the default SSL trust manager and the default SSL hostname verifier.

Here’s a source code for Java Web Services which is used to by pass all the certificate and hostname checking. 
A very useful code in testing environment ONLY.

How to get Client IP Adress in Java

Simply getting the IP of the remote client in Java is an easy task. Assuming we have a request instance, we can simply invoke the “getRemoteAddr” method

servletRequest.getRemoteAddr() 


But, if user is behind a proxy server or access your web server through a Load balancer (for example, in cloud hosting), the above code will get the IP address of the proxy server or load balancer server, not the original IP address of a client.

To solve it, you should get the IP address of the request’s HTTP header

Sunday, 13 July 2014

Sorting a list by multiple attributes example

There are several ways in order to sort a list collection by multiple attributes (keys) of its elements type. For example, sorting a list of employees by their job title, then by age, and then by salary.

In this article, we are going to discuss using a CompareToBuilder class of the Apache Commons Lang library.