Search My Blog

Wednesday, November 30, 2011

Removing blank spaces from a String in Java

sText= sText.replaceAll("\\s+", "");


Reference -
http://www.mkyong.com/java/how-to-remove-whitespace-between-string-java/

Monday, November 28, 2011

using Comparator to sort a List in JAVA

Sorting a List based on one of its attributes is achieved by using the Comparator interface.

For example,

public class EmpSortByName implements Comparator{

public int compare(Employee o1, Employee o2) {
return o1.getName().compareTo(o2.getName());
}
}

import java.util.*;

public class TestEmployeeSort {

public static void main(String[] args) {

List coll = Util.getEmployees();
//Collections.sort(coll);
//use Comparator implementation
Collections.sort(coll, new EmpSortByName());
printList(coll);
}

private static void printList(List list) {
System.out.println("EmpId\tName\tAge");
for (Employee e: list) {
System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
}
}
}

This sorts the list based of employee names.

Calculate distance between 2 geo points

Given the latitude and longitude of 2 points, we can find the distance between then using the Haversine formula.

The Java function to do the same is given below :


public static double getDistance(double lat1, double lon1, double lat2, double lon2)
{
int R = 6371; // km
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double lat1mod = Math.toRadians(lat1);
double lat2mod = Math.toRadians(lat2);

double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1mod) * Math.cos(lat2mod);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c;

return d;
}

Sunday, November 20, 2011

auto shutdown Windows machine

shutdown /s /t seconds

/s - shutdown, there are other options like /r - restart
/t seconds - specifies time in seconds, after which system will be shutdown

Tuesday, October 25, 2011

complexity of algorithms

big theta - gives both an upper and lower bound on the running time of an algorithm. So, if we say running time of algorithm A is theta(n^2), we mean that given any input, the algorithm would take f(n^2) time to complete.

big O - gives an upper bound on the running time of an algorithm. So, if we say run time of A is O(n^2), we mean, given any input size, it will not take more than f(n^2) time to execute A. It may take lesser.

big omega - gives a lower bound. if run time of A is omega(n^2), we mean that it takes no less than f(n^2) time to complete. It may take more.

So, when we say worst case complexity of quick sort is theta(n^2), what we mean is, in the worst case, quick sort always takes f(n^2) time, however large the input size is. It does not say anything about the best case, or the average case !!!

Monday, May 16, 2011

replace text in file linux

sed 's/old_text/new_text/' filename > outputfile

old_text - text to be replaced in file 'filename'
new_text - text to be substituted file 'filename'

the above command can be used to remove text from a file, by leaving the new_text blank :
sed 's/old_text//' filename > outputfile

Friday, May 13, 2011

Nutch, Solr, ......

I am working on developing a custom search using Apache Nutch in Java. Being bombarded with so much new stuff - nutch, lucene, servlet container, tomcat, jetty. Had never even heard these before !!!! Its a bit confusing, and a bit intimidating, but hope to make good sense of all these terms.

Tuesday, May 10, 2011

Charging a cell phone battery when charger/charging port does not work !

Do not want to buy a new charger if your charger does not work? Or has your cell phone had a gr8 fall, and the charging port is damaged?

DO NOT WORRY !!!

You can charge your cell phone battery using a USB cable. All you need to do is strip the cable on one side, and locate the red(+ve) and black(-ve) wires. Now, connect the red wire to the +ve terminal of your battery, and the black one to the -ve, while the battery is plugged into the phone.

Better yet, watch this video on Youtube : http://www.youtube.com/watch?v=9pYcgmLhUcQ

WARNING : I do not know if this damages your battery or not, but it is worth trying ;)

Persisting Environment variables in Linux

I was tired of changing the JAVA_HOME path everyday, before starting work at office. So I dug in a bit and found that you could make environment variables persistent by editing the file /etc/environment. If you locate the file and it opens in read-only mode, try 'sudo gedit /etc/environment' at the command prompt.