Pages

Sunday, September 26, 2010

How To Do Sorting In Languages Other Than English In Java?

Sorting is always a tricky game in any programming language and it is responsible for 50-60 percent of the total CPU time for any application. We all have our native language like Hindi, Chinese, Japanese, French and so many. Most of the time world deals with sorting of Alphabets or English words but give a eye on other languages which is growing fast and off course today we are talking about internationalization.
I am showing you a typical sorting of French word and the blunder associated with it. These are some of the common French words: 

String[] names = {"fácil", "facil", "fast","Où", "êtes-vous", "spécifique", "specific", "ou"};

and here is the typical sorting code:

String[] names = {"fácil", "facil", "fast","Où", "êtes-vous", "spécifique", "specific", "ou"};
List list = Arrays.asList(names);
Collections.sort(list);
Iterator itr = list.iterator();
while(itr.hasNext()) {
System.out.print(itr.next()+ " ");
}

And the result:
Actul : Où facil fast fácil ou specific spécifique êtes-vous
Expected êtes-vous facil fácil fast Où ou specific spécifique

which is completely wrong according to French Rules. Because sorting is simply going via UNICODE rules not by French rules.

Now remedy: Java gives us a class called Collator class in java.text Package which takes care of locale while sorting. Here goes the code:


import java.text.*;

import java.util.*;


class CollatorTest {
public static void main(String[] args) {
String[] names = {"fácil", "facil", "fast", "Où", "êtes-vous", "spécifique", "specific", "ou"};
List list = Arrays.asList(names);
Collections.sort(list);
Iterator itr = list.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
Locale[] loc = Collator.getAvailableLocales();
Collator myCollator = Collator.getInstance(new Locale("fr"));
myCollator.setStrength(Collator.PRIMARY);
Collections.sort(list, myCollator);
itr = list.iterator();
System.out.println("");
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
myCollator.setStrength(Collator.TERTIARY);
Collections.sort(list, myCollator);
itr = list.iterator();
System.out.println("");
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
} }


And here is the result:


Où  facil  fast  fácil  ou  specific  spécifique  êtes-vous


êtes-vous  facil  fácil  fast  Où  ou  specific  spécifique


êtes-vous  facil  fácil  fast  ou  Où  specific  spécifique

How To Find The Number Of JREs Running On Windows Machine?

It will only run on Windows :) because we are using Runtime class. This code is not doing anything great, just queries the registry and reflects the answer on the console. 


import java.io.*;

class NoofJRE {
static String REG_PATH = "reg query HKLMSoftwareJavaSoftJava Runtime Environment";
     public static void getJREInfo() {
try {
      Process process = Runtime.getRuntime().exec(REG_PATH);
      InputStream inputstream = process.getInputStream();
      InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
      BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
      String line;
      while ((line = bufferedreader.readLine()) != null) {
System.out.println(line);
}
}catch (Exception e) {
     System.out.println("I am in Exception");
}
}
public static void main(String s[]) {
getJREInfo();
}
}


Output

! REG.EXE VERSION 3.0
HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment CurrentVersion REG_SZ 1.6
BrowserJavaVersion REG_SZ 1.6.0_01
HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment1.6
HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment1.6.0
HKEY_LOCAL_MACHINESoftwareJavaSoftJava Runtime Environment1.6.0_01

Saturday, September 25, 2010

How To List All The Processes Running On A Machine From Java Code?

Java can't play with system process and hence invoking a runtime is only solution to get all process and here it is:

import java.io.*;

class ListProcess {
public static void main(String[] args) throws IOException {
Runtime runtime = Runtime.getRuntime();
String cmds[] = {"cmd", "/c", "tasklist"};
Process proc = runtime.exec(cmds);
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String line;
while ((line = bufferedreader.readLine()) != null) {
System.out.println(line);
}
} }
Mind you, the code is written exclusively for Windows Machine :). And one line change in this code will list you only java running process.

String cmds[] = {"cmd", "/c", "jps"}; this is nothing but running jps.exe file in bin (jdk6 onwards).

Output

Image Name                     PID Session Name        Session#    Mem Usage
======= ======== ================ =========== ============
System Idle Process              0 Services                   0         24 K
System                           4 Services                   0      4,024 K
smss.exe                       264 Services                   0      1,080 K
csrss.exe                      356 Services                   0      4,968 K
wininit.exe                    420 Services                   0      4,380 K
csrss.exe                      436 Console                    1     27,912 K
services.exe                   472 Services                   0      9,168 K
lsass.exe                      500 Services                   0     10,604 K
lsm.exe                        516 Services                   0      4,316 K
winlogon.exe                   524 Console                    1      7,044 K
svchost.exe                    640 Services                   0      9,124 K
svchost.exe                    748 Services                   0      9,168 K
svchost.exe                    836 Services                   0     19,404 K
svchost.exe                    876 Services                   0    107,948 K
svchost.exe                    908 Services                   0     37,764 K
stacsv64.exe                   968 Services                   0      9,072 K
svchost.exe                    596 Services                   0     11,328 K
DockLogin.exe                  960 Services                   0      3,940 K
svchost.exe                   1144 Services                   0     16,072 K
WLTRYSVC.EXE                  1224 Services                   0      3,152 K
wlanext.exe                   1232 Services                   0      4,932 K
conhost.exe                   1240 Services                   0      2,508 K
BCMWLTRY.EXE                  1308 Services                   0     31,472 K
spoolsv.exe                   1380 Services                   0     11,184 K
svchost.exe                   1408 Services                   0     14,008 K
AESTSr64.exe                  1496 Services                   0      2,600 K
svchost.exe                   1572 Services                   0     13,020 K
SeaPort.exe                   1724 Services                   0      9,948 K
SfCtlCom.exe                  1820 Services                   0      6,448 K
SftService.exe                1864 Services                   0      6,920 K
TeamViewer_Service.exe        2036 Services                   0      3,716 K
taskhost.exe                  2076 Console                    1      9,856 K
dwm.exe                       2280 Console                    1     38,260 K
explorer.exe                  2432 Console                    1     58,808 K
TmProxy.exe                   2908 Services                   0     16,824 K
DSUpd.exe                     2772 Console                    1     17,184 K
conhost.exe                   1904 Console                    1      4,536 K
WmiPrvSE.exe                  2788 Services                   0      6,888 K
STService.exe                 2728 Console                    1     12,372 K
Toaster.exe                   3268 Console                    1     32,212 K
Apoint.exe                    3348 Console                    1      8,780 K
igfxtray.exe                  3364 Console                    1      6,308 K
hkcmd.exe                     3372 Console                    1      6,176 K
igfxpers.exe                  3420 Console                    1      6,148 K
igfxsrvc.exe                  3484 Console                    1      6,176 K
ApMsgFwd.exe                  3572 Console                    1      4,424 K
WLTRAY.EXE                    3592 Console                    1     30,680 K
ApntEx.exe                    3608 Console                    1      5,104 K
hidfind.exe                   3616 Console                    1      4,292 K
conhost.exe                   3632 Console                    1      4,548 K
sttray64.exe                  3660 Console                    1     16,472 K
quickset.exe                  3700 Console                    1     12,428 K
UfSeAgnt.exe                  3804 Console                    1      1,404 K
googletalk.exe                3172 Console                    1     12,704 K
SearchIndexer.exe             3460 Services                   0     23,432 K
veohwebplayer.exe             3096 Console                    1     33,712 K
sidebar.exe                   3532 Console                    1     20,732 K
jusched.exe                   2856 Console                    1      4,176 K
DataSafeOnline.exe            3756 Console                    1     35,072 K
DellDock.exe                  3836 Console                    1     29,188 K
PDVDDXSrv.exe                  940 Console                    1      8,500 K
WebcamDell2.exe                832 Console                    1      7,520 K
RoxioBurnLauncher.exe         3880 Console                    1      8,376 K
sprtcmd.exe                   3900 Console                    1        548 K
DivXUpdate.exe                1672 Console                    1     15,776 K
chrome.exe                    4692 Console                    1     75,592 K
chrome.exe                    4808 Console                    1     15,032 K
chrome.exe                    4892 Console                    1     49,148 K
chrome.exe                    4164 Console                    1      9,120 K
chrome.exe                     764 Console                    1     28,016 K
chrome.exe                     196 Console                    1      8,948 K
googletalkplugin.exe          4416 Console                    1     13,676 K
sprtsvc.exe                   2704 Services                   0      1,400 K
wuauclt.exe                   4816 Console                    1      6,512 K
TMBMSRV.exe                   4220 Services                   0      9,108 K
audiodg.exe                   4016 Services                   0     19,620 K
wmplayer.exe                  4216 Console                    1     64,424 K
eclipse.exe                    760 Console                    1      4,164 K
javaw.exe                     3736 Console                    1    150,400 K
chrome.exe                    3128 Console                    1     29,088 K
SearchProtocolHost.exe        2668 Services                   0      4,956 K
chrome.exe                    1368 Console                    1     45,940 K
javaw.exe                     4496 Console                    1     11,000 K
cmd.exe                       2736 Console                    1      2,476 K
conhost.exe                   3148 Console                    1      3,292 K
tasklist.exe                  5032 Console                    1      5,272 K
WmiPrvSE.exe                  3236 Services                   0      5,956 K

Sunday, September 12, 2010

SQL JOINS

Sql Keywords: Null, Join, Select, Insert, Set Operations, Where, Delete, Update, Hierarchical Query, Truncate, Order By, Merge, From, UpsertSql Keywords: Null, Join, Select, Insert, Set Operations, Where, Delete, Update, Hierarchical Query, Truncate, Order By, Merge, From, Upsert

SQL JOIN

The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.
Tables in a database are often related to each other with keys.
A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.

Look at the "Persons" table:
P_IdLastNameFirstNameAddressCity
1HansenOlaTimoteivn 10Sandnes
2SvendsonToveBorgvn 23Sandnes
3PettersenKariStorgt 20Stavanger

Note that the "P_Id" column is the primary key in the "Persons" table. This means that no two rows can have the same P_Id. The P_Id distinguishes two persons even if they have the same name.
Next, we have the "Orders" table:

O_IdOrderNoP_Id
1778953
2446783
3224561
4245621
53476415

Note that the "O_Id" column is the primary key in the "Orders" table and that the "P_Id" column refers to the persons in the "Persons" table without using their names.
Notice that the relationship between the two tables above is the "P_Id" column.

Different SQL JOINs

Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.
  • JOIN: Return rows when there is at least one match in both tables
  • LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
  • RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
  • FULL JOIN: Return rows when there is a match in one of the tables.

SQL INNER JOIN Keyword

The INNER JOIN keyword return rows when there is at least one match in both tables.

SQL INNER JOIN Syntax

SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
PS: INNER JOIN is the same as JOIN.


Example

The "Persons" table:
P_IdLastNameFirstNameAddressCity
1HansenOlaTimoteivn 10Sandnes
2SvendsonToveBorgvn 23Sandnes
3PettersenKariStorgt 20Stavanger

The "Orders" table:
O_IdOrderNoP_Id
1778953
2446783
3224561
4245621
53476415
Now we want to list all the persons with any orders.

We use the following SELECT statement:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

The result-set will look like this:
LastNameFirstNameOrderNo
HansenOla22456
HansenOla24562
PettersenKari77895
PettersenKari44678

The INNER JOIN keyword return rows when there is at least one match in both tables. If there are rows in "Persons" that do not have matches in "Orders", those rows will NOT be listed.

SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).

SQL LEFT JOIN Syntax

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
PS: In some databases LEFT JOIN is called LEFT OUTER JOIN.
Example

The "Persons" table:
P_IdLastNameFirstNameAddressCity
1HansenOlaTimoteivn 10Sandnes
2SvendsonToveBorgvn 23Sandnes
3PettersenKariStorgt 20Stavanger

The "Orders" table:
O_IdOrderNoP_Id
1778953
2446783
3224561
4245621
53476415

Now we want to list all the persons and their orders - if any, from the tables above.
We use the following SELECT statement:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

The result-set will look like this:
LastNameFirstNameOrderNo
HansenOla22456
HansenOla24562
PettersenKari77895
PettersenKari44678
SvendsonTove 
The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no matches in the right table (Orders).

SQL RIGHT JOIN Keyword

The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are no matches in the left table (table_name1).

SQL RIGHT JOIN Syntax

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
PS: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
Example
The "Persons" table:
P_IdLastNameFirstNameAddressCity
1HansenOlaTimoteivn 10Sandnes
2SvendsonToveBorgvn 23Sandnes
3PettersenKariStorgt 20Stavanger


The "Orders" table:
O_IdOrderNoP_Id
1778953
2446783
3224561
4245621
53476415


Now we want to list all the orders with containing persons - if any, from the tables above.
We use the following SELECT statement:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
RIGHT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName


The result-set will look like this:
LastNameFirstNameOrderNo
HansenOla22456
HansenOla24562
PettersenKari77895
PettersenKari44678
  34764


The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches in the left table (Persons).

SQL FULL JOIN Keyword

The FULL JOIN keyword return rows when there is a match in one of the tables.

SQL FULL JOIN Syntax

SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name

Example

The "Persons" table:
P_IdLastNameFirstNameAddressCity
1HansenOlaTimoteivn 10Sandnes
2SvendsonToveBorgvn 23Sandnes
3PettersenKariStorgt 20Stavanger

The "Orders" table:
O_IdOrderNoP_Id
1778953
2446783
3224561
4245621
53476415

Now we want to list all the persons and their orders, and all the orders with their persons.
We use the following SELECT statement:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
FULL JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

The result-set will look like this:
LastNameFirstNameOrderNo
HansenOla22456
HansenOla24562
PettersenKari77895
PettersenKari44678
SvendsonTove 
  34764

The FULL JOIN keyword returns all the rows from the left table (Persons), and all the rows from the right table (Orders). If there are rows in "Persons" that do not have matches in "Orders", or if there are rows in "Orders" that do not have matches in "Persons", those rows will be listed as well.