Monday, 31 August 2015

Rotate 2-D matrix by 90

Given N*M matrix, rotate it by 90 degree right

package core.geeks;
class Rotate90Degree {
      
       private static void rotateMatrix(int[][] array) {
              int oRow = array.length;
              int oCol = array[0].length;
              int[][] tArr = new int[oCol][oRow];

              int tCol = 3;
              for (int i = 0; i < oRow; i++){
                     for(int j = 0;  j < oCol; j++){
                           tArr[j][tCol] = array[i][j];
                     }
                     tCol--;
              }

              printGrid(tArr);
       }
      
       public static void main(String args[] ) throws Exception {
              int[][] array = {  {0, 1, 2, 3, 4},
                           {5, 6, 7, 8, 9},
                           {10, 11, 12, 13, 14},
                           {15, 16, 17, 18, 19}};
              rotateMatrix(array);
       }

       public static void printGrid(int[][] a) {
              for(int i = 0; i < a.length; i++) {
                     for(int j = 0; j < a[i].length; j++) {
                           System.out.printf("%5d ", a[i][j]);
                     }
                     System.out.println();
              }
       }
}

Output:
   15    10     5     0
   16    11     6     1
   17    12     7     2
   18    13     8     3
   19    14     9     4


Friday, 28 August 2015

Singleton beans scopes in Spring

Beans with different Id’s have Singleton instance


singleton: (Default) Scopes a single bean definition to a single object instance per Spring IoC container.


NO, Similar beans with different ids make the different instance (Even scope is defined as Singleton); because the singleton scope checked on the basis of bean ids.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd"

       default-autowire="byName" default-autowire-candidates="*">

       <context:annotation-config />

       <bean id="id1" class="spring.core.singleton.Singleton">
              <property name="name" value="Rajesh kumar"></property>
       </bean>

       <bean id="id2" class="spring.core.singleton.Singleton">
              <property name="name" value="Awadh kumar"></property>
       </bean>
</beans>

package spring.core.singleton;
public class Singleton {
      
       private String name;

       public String getName() {
              return name;
       }

       public void setName(String name) {
              this.name = name;
       }
}

package spring.core.singleton;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSingletonScope {
       public static void main(String[] args) {

              ApplicationContext appContext = new ClassPathXmlApplicationContext
                           ("spring/core/singleton/autowiring-spring.xml");
             
              Singleton singleton1 = appContext.getBean("id1", Singleton.class);
             
              Singleton singleton2 = appContext.getBean("id2", Singleton.class);
             
             
              System.out.println(singleton1.getName());
              System.out.println(singleton2.getName());

              if(singleton1==singleton2) {
                     System.out.println("singleton");
              } else {
                     System.out.println("not singleton");
              }
       }
}

Output:
Rajesh kumar
Awadh kumar
not singleton
Related Posts Plugin for WordPress, Blogger...