Java, Struts, Spring, Hibernate, Unix,IT, Software, Jobs, Interview, Aptitude, Learning, Algorithms
Sunday, October 31, 2010
equals and hashcode
Object equals will just compare references and hashcode is native which always return difference integer even if objects are same but different instances. So of you override equals and not hashcode for any object being used as a key in hashmap, then I will not get value corresponding to key because it wont be able to find hashcode for new instance of key. As it will look into the wrong bucket (new bucket) if I override hashcode and not equals, even there I wont be able to get the value because I can find the bucket but objects equal check references which will be different for stored object and my key object (new instance of old key object).
DB Connection class
DriverManager.getConnection (url, user, pwd).getClass().getName().equals("java.sql.Connection") = false
This is false because left hand side will return a class of the underlying database connection class i.e. oracle.jdbc.driver.OracleConnection in case of oracle. So this will be a instance of java.sql.Connection but can not be equal.
This is false because left hand side will return a class of the underlying database connection class i.e. oracle.jdbc.driver.OracleConnection in case of oracle. So this will be a instance of java.sql.Connection but can not be equal.
Cut rectangular cake half
Given a rectangular cake with rectangular piece removed (any size, any orientation), how would you cut remainder cake in two equal haves with one straight cut of knife.
Serializable Singleton
In a class write below code to prevent singleton class from losing its behavior if you make it serializable.
protected Object readResolve(){
return singletonObj;
}
Deserialization creates another object instance, so to avoid that situation this needs to be done.
protected Object readResolve(){
return singletonObj;
}
Deserialization creates another object instance, so to avoid that situation this needs to be done.
Java Threading
public static void myMethod(){
synchronized(this){
//do stuff
}
}
This will not get a class level lock, many threads can enter this static method of class and with individual object lock can execute code in it.
public static synchronized void myMethod() {
//do stuff
}
-----is equivalent to-----
public void myMethod(){
synchronized(MyClass.class){
//do stuff
}
}
In both the case, threads will obtain a class level lock butt there will just be a difference of timing for how long lock is being obtained.
1. Threads that synchronize on the same object will block each other. Threads that synchronize on different objects will not.
2. A static synchronized method and a non-static synchronized method will not block each other, ever. The static method locks on a class instance while non-static method locks on this instance these actions do not interfere with each other at all.
3. Threads calling static synchronized methods in the same class will always block each other - they all lock the same class instance.
4. Threads calling non-static synchronized methods in the same class will only block each other if they are invoked using same instance.
5. A thread can acquire more that one lock. A thread can enter a synchronized method, acquiring a lock and then immediately invoke a synchronized method on a different object, acquiring that locak as well. A thread acquires a lock and then attempts to call a synchronized method on the same object, no problem because thread already have lock of this object.
6. If a class has both synchronized and non-synchronized methods, multiple threads can still access the class's non-synchronized methods.
Thread Join-
If you have a thread B that can't do its work until another thread A has completed its work, then you want B to join thread A.
This means that thread B will not become runnable until thread A has finished.
Thread t = new Thread();
t.start();
t.join();
This code take currently running thread and joins it to the end of thread t. Current thread become runnable after thread t is no longer alive.
synchronized(this){
//do stuff
}
}
This will not get a class level lock, many threads can enter this static method of class and with individual object lock can execute code in it.
public static synchronized void myMethod() {
//do stuff
}
-----is equivalent to-----
public void myMethod(){
synchronized(MyClass.class){
//do stuff
}
}
In both the case, threads will obtain a class level lock butt there will just be a difference of timing for how long lock is being obtained.
1. Threads that synchronize on the same object will block each other. Threads that synchronize on different objects will not.
2. A static synchronized method and a non-static synchronized method will not block each other, ever. The static method locks on a class instance while non-static method locks on this instance these actions do not interfere with each other at all.
3. Threads calling static synchronized methods in the same class will always block each other - they all lock the same class instance.
4. Threads calling non-static synchronized methods in the same class will only block each other if they are invoked using same instance.
5. A thread can acquire more that one lock. A thread can enter a synchronized method, acquiring a lock and then immediately invoke a synchronized method on a different object, acquiring that locak as well. A thread acquires a lock and then attempts to call a synchronized method on the same object, no problem because thread already have lock of this object.
6. If a class has both synchronized and non-synchronized methods, multiple threads can still access the class's non-synchronized methods.
Thread Join-
If you have a thread B that can't do its work until another thread A has completed its work, then you want B to join thread A.
This means that thread B will not become runnable until thread A has finished.
Thread t = new Thread();
t.start();
t.join();
This code take currently running thread and joins it to the end of thread t. Current thread become runnable after thread t is no longer alive.
Fibonacci
public void fibonacci(){
int curr = 1;
int prv = 0;
int prvHolder=0;
for(int c = 0; c <= 30; c++){
Sysout (curr);
prvHolder=prv;
prv=curr;
curr += prvHolder;
}
}
int curr = 1;
int prv = 0;
int prvHolder=0;
for(int c = 0; c <= 30; c++){
Sysout (curr);
prvHolder=prv;
prv=curr;
curr += prvHolder;
}
}
Prime factorization
public void primeFactor (int num){
List factors = new ArrayList();
for (int i = 2; i <= n; i++){
while (n % i == 0){
factors.add(i);
n /= i;
}
}
}
List factors = new ArrayList();
for (int i = 2; i <= n; i++){
while (n % i == 0){
factors.add(i);
n /= i;
}
}
}
Greatest Common Divisor
public int gcd (int p, int q){
if (q=-=0){
return p;
}
return gcd (q, p%q);
}
if (q=-=0){
return p;
}
return gcd (q, p%q);
}
Annotation
It releives java developer from sumbersome configuration. Declarative programming style - programmer says what should be done & tools emit the code to do it. These are meta-tag information.
DisplayTag
<display:table export="true" is="data" name="sessionScope.UserForm.forbesList" requestURI="/userAction.do" poagesize="10" >
<display:column property="rank" title="Rank" sortable="true" />
....
</display:table>
export - to export table data as excel, csv or xml
sortable - makes column sortable by providing clickable title
pagesize - for pagination
<display:column property="rank" title="Rank" sortable="true" />
....
</display:table>
export - to export table data as excel, csv or xml
sortable - makes column sortable by providing clickable title
pagesize - for pagination
Hibernate mapping
Event table (LocationID - FK, EventID - PK)
Attendee Table (AttendeeID - PK)
SpeakerID (SpeakerID - PK, EventID - FK)
Event_Attendee (EventID, AttendeeID)
An event can have many attendee and speakers. An attendee can attend many events. One location can also have many events.
<hibernate-mapping>
<class name="Event" table="events">
<id name="id" column="uid" type="long">
<generator class="increment">
</id>
<property name="name" type="string" length="100"/>
<many-to-one name="location" column="locationID" class="Location"/>
<set name="speakers" cascade="all">
<key column="eventID"/>
<one-to-many class=Speakers"/>
</set>
<set name="attendee" table="event_attendee" cascade="all">
<key column="eventID"/>
<many-to-many column="AttendeeID" class=Attendee"/>
</set>
</class>
</hibernate-mapping>
Attendee Table (AttendeeID - PK)
SpeakerID (SpeakerID - PK, EventID - FK)
Event_Attendee (EventID, AttendeeID)
An event can have many attendee and speakers. An attendee can attend many events. One location can also have many events.
<hibernate-mapping>
<class name="Event" table="events">
<id name="id" column="uid" type="long">
<generator class="increment">
</id>
<property name="name" type="string" length="100"/>
<many-to-one name="location" column="locationID" class="Location"/>
<set name="speakers" cascade="all">
<key column="eventID"/>
<one-to-many class=Speakers"/>
</set>
<set name="attendee" table="event_attendee" cascade="all">
<key column="eventID"/>
<many-to-many column="AttendeeID" class=Attendee"/>
</set>
</class>
</hibernate-mapping>
Find circular linked list
Start with two pointers. Increase one ptr by 1 and second ptr by 2. If they meet then list is circular.
Reverse a string
To reverse a string in java, tokenize the string and starting printing from len-1 to 0.
Summing column value in SQL
To sum the columns' of a record in table use '+' operator like -
select col1+col2 from table test
select col1+col2 from table test
Subscribe to:
Comments (Atom)
