January 12 2015

Constructor Overloading In Java

Constructor Overloading In Java

Constructors is a special method in Java which is used to initialize the instance variables of the class.A constructor has the same name of the class and does not have any return type(not even void) .A constructor may or may not have parameters.

Whenever a new object is created JVM first allocates memory for the object and then invokes the matching constructor.If class does not have any constructor JVM provides default constructor with no arguments.

Constructors in Java can be overloaded. Two ore more constructors with difference in the parameters is called constructor overloading. Each constructor is used to perform different task.

Constructor Overloading In Java Example

Continue reading

January 11 2015

Java SortedMap Example

Java SortedMap Examplejava sortedmap example

A SortedMap is a Map that maintains its entries in ascending order, sorted according to the key’s natural ordering(if they implement java.lang.Comparable), or according to a Comparator provided at the time of the SortedMap creation.

SortedMap Creation Example

SortedMap mapName= new TreeMap();

Comparator comparator = new MyComparator();

SortedMap mapName = new TreeMap(comparator);

SortedMap newSortedMap = new TreeMap(TreeMap or HashMap);

Example – SortedMapExample.java

Continue reading

January 10 2015

Java Sortedset Example

Java Sortedset Examplejava sortedset example

SortedSet interface is part of java.util package.All the elements are stored in sorted order.The elements are sorted using their natural ordering (if they implement java.lang.Comparable) or by a Comparator provided at time of Set creation.Treeset provides implementation for SortedSet.

Set creation examples

SortedSet setName= new TreeSet();

or

Comparator comparator = new MyComparator();

Continue reading

January 6 2015

Overriding equals method in java

equals method is inherited from the Object class and it is overridden in each of the wrapper classes.This methods compares the references of two objects if they are equal,it returns true otherwise false.

To check whether two objects of user defined class equal or not,the class should override equals method.

Example : If equals method is not present

Continue reading

January 4 2015

cannot be cast to java.lang.comparable

cannot be cast to java.lang.comparable

Cannot be cast to java.lang.Comparable is a common exception in Java.This post explains why this exception occurs and how to handle this exception.This exception occurs when a sort is done on array of objects and the class objects refers to does not implement java.lang.Comparable inteface.

java.lang.Comparable

Comparable is interface which contains one method compareTo where the sorting mechanism need to be placed.The return type of this method is int.

Continue reading

January 3 2015

Java bitwise operators

Bitwise Operators

There are seven bitwise operators in Java that acts on integer datatypes(byte,short,long and int).Bitwise operators acts on bits of the integers.All the operation are perfomed on the binary representation of the number.Binary number system uses only two digits 0 and 1.

Integer.toBinaryString(10) will give the binary number of 10 that is 1010.

Bitwise Operators

Bitwise complement Operator(~)

Bitwise and operator(&) Continue reading

January 1 2015

Arraylist remove duplicates

Arraylist remove duplicates

There are two ways to remove duplicate elements from arraylist.

Using Set


package com.answersz.collection.examples;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class ArraylistRemoveDuplicates {

public static void main(String[] args) {

System.out.println(" Arraylist remove duplicates - Set Example");


List<String> countryList = new ArrayList<String>();
countryList.add("Australia");
countryList.add("India");
countryList.add("Denmark");
countryList.add("Italy");
countryList.add("India");
countryList.add("France");
countryList.add("United States");
countryList.add("Italy");

HashSet countrySet = new HashSet(countryList);

List updatedCountryList = new ArrayList(countrySet);

System.out.println("ArrayList with duplicates" + countryList);
System.out.println("ArrayList after removing duplicates"+ updatedCountryList);
}

}

Continue reading

December 27 2014

Spring Autowire by Name

Autowiring by Name

Setter method is used for  Autowiring by Name  and name of a bean should be same as the name of other bean property.

Example:

In the below example we have two classes Employee and Address. Employee class has Address object reference. In the below example, Address is autowired in Employee using ‘Autowiring by Name’ by autowire=”byName”. The object name of Address in Employee class is ‘address’ therefore the bean id for Address class should be ‘address’. Continue reading

Category: Spring | LEAVE A COMMENT
December 27 2014

Autowiring In Spring

Autowiring In Spring

Spring provides autowiring feature for connecting one bean with other bean automatically.We can inject beans using ref in property or constructor-arg by yourself or we can autowiring. To enable autowire for a bean autowire=”autowiretype” attribute should be added inside bean.

There are five types of autowiring in spring

  • no (default)
  • byName
  • byType
  • constructor
  • autodetect

Continue reading

Category: Spring | LEAVE A COMMENT