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.