Map and List are two common data structures available in Java and in this article we will see how can we convert Map values or Map keys into List in Java. The primary difference between Map (HashMap, ConcurrentHashMap or TreeMap) and List is that Map holds two objects key and value while List just holds one object which itself is a value. Key in hashmap is just an addon to find values, so you can just pick the values from Map and create a List out of it. The map in Java allows duplicate value which is fine with List which also allows duplicates but Map doesn't allow duplicate key.
Sunday, December 31, 2017
Friday, July 21, 2017
How to convert String or char to ASCII values in Java
You can convert a character e.g. 'A' to its corresponding ASCII value 65 by just storing it into a numeric data type e.g. byte, int or long as shown below :
int asciiOfA = (int) 'A';
Here casting is not necessary, simply assigning a character to integer is enough to store ASCII value of character into an int variable, but casting improves readability. Since ASCII is 7-bit character encoding, you don't even need an integer variable to store ASCII values, byte data type in Java, which is 8 bits wide is enough to store ASCII value of any character. So you can also do like this :
byte asciiOfB = 'B'; // assign 66 to variable
Since String is nothing but a character array in Java, you can also use this technique to convert a String into ASCII values, as shown below :
StringBuilder sb = new StringBuilder();
char[] letters = str.toCharArray();
for (char ch : letters) {
sb.append((byte) ch);
}
System.out.println(sb.toString()); // print 749711897
You can also directly convert String to byte array, where bytes will hold ASCII value of characters as shown below :
byte[] ascii = "Java".getBytes(StandardCharsets.US_ASCII);
String asciiString = Arrays.toString(ascii);
System.out.println(asciiString); // print [74, 97, 118, 97]
You can pass character encoding as "US-ASCII" also, as we have done in our Java example, but using StandardCharsets.US_ASCII is safer because there is no chance of any spelling mistake causing UnsupportedEncodingException.
Here is our Java program, which combines all the ways we have seen to convert String and character to their respective ASCII values. You can also use the same technique to convert String to other encoding formats e.g. ISO-8859-X (1-7) , UTF-8 , UTF-16BE , UTF-16LE. These are some of the popular encoding formats internally supported by Java.
Here is ASCII table for your quick reference :
int asciiOfA = (int) 'A';
byte asciiOfB = 'B'; // assign 66 to variable
Since String is nothing but a character array in Java, you can also use this technique to convert a String into ASCII values, as shown below :
StringBuilder sb = new StringBuilder();
char[] letters = str.toCharArray();
for (char ch : letters) {
sb.append((byte) ch);
}
System.out.println(sb.toString()); // print 749711897
You can also directly convert String to byte array, where bytes will hold ASCII value of characters as shown below :
byte[] ascii = "Java".getBytes(StandardCharsets.US_ASCII);
String asciiString = Arrays.toString(ascii);
System.out.println(asciiString); // print [74, 97, 118, 97]
You can pass character encoding as "US-ASCII" also, as we have done in our Java example, but using StandardCharsets.US_ASCII is safer because there is no chance of any spelling mistake causing UnsupportedEncodingException.
Java Program to convert String and char to ASCII
Here is our Java program, which combines all the ways we have seen to convert String and character to their respective ASCII values. You can also use the same technique to convert String to other encoding formats e.g. ISO-8859-X (1-7) , UTF-8 , UTF-16BE , UTF-16LE. These are some of the popular encoding formats internally supported by Java.
Here is ASCII table for your quick reference :
import java.text.ParseException;
import java.util.Arrays;
/**
* How to convert a String to ASCII bytes in Java
*
* @author WINDOWS 8
*/
public class StringToASCII {
public static void main(String args[]) throws ParseException {
// converting character to ASCII value in Java
char A = 'A';
int ascii = A;
System.out.println("ASCII value of 'A' is : " + ascii);
// you can explicitly cast also
char a = 'a';
int value = (int) a;
System.out.println("ASCII value of 'a' is : " + value);
// converting String to ASCII value in Java
try {
String text = "ABCDEFGHIJKLMNOP";
// translating text String to 7 bit ASCII encoding
byte[] bytes = text.getBytes("US-ASCII");
System.out.println("ASCII value of " + text + " is following");
System.out.println(Arrays.toString(bytes));
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
Output
ASCII value of 'A' is : 65
ASCII value of 'a' is : 97
ASCII value of ABCDEFGHIJKLMNOP is following
[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]
That's all guys, now you know how to convert a Java char or String to their ASCII values. Remember, when you store a char data type into numeric types e.g. byte, short, int or long, their ASCII values are stored. It's also efficient to use byte data type to store ASCII values because it's a 7-bit character encoding and byte is enough to store ASCII.
Friday, May 26, 2017
Different Types of JDBC Drivers in Java
There are mainly 4 types of JDBC drivers in Java, those are referred as type 1 to type 4 jdbc drivers. I agree its easy to remember them by type rather than with there actual name, Which I have yet to get in memory except plain old JDBC-ODBC bridge driver. By the way here are there full names :
Type 1 JDBC Driver is called JDBC-ODBC Bridge driver (bridge driver)
Type 2 JDBC Driver is referred as Native-API/partly Java driver (native driver)
Type 3 JDBC Driver is called AllJava/Net-protocol driver (middleware driver)
Type 4 JDBC Driver is called All Java/Native-protocol driver (Pure java driver)
1. JDBC ODBC Bridge Driver or Type 1 JDBC driver
In case of JDBC ODBC bridge driver all JDBC calls doesn't directly goes to database instead they go via ODBC driver. JDBC-ODBC driver translates JDBC calls into ODBC callas and send them to ODBC driver for passing to database. Since type 1 driver act as bridge between JDBC and ODBC and that's why its called JDBC-ODBC bridge driver. This driver is not fast and good for production use mainly because of several layer of translation on back and fourth database traffic but it has advantage in terms of of availability and can be your last choice.
2. Native-API/partly Java driver or Type 2 JDBC driver
This is also called type 2 driver and its slightly better than type 1 JDBC driver. type 2 JDBC driver convert JDBC calls into database calls by using native API provided by database. This driver is database specific so once you switch from one database to another you need to change type 2 JDBC driver. performance is better than JDBC-ODBC bridge driver since communication layer is reduced. type 2 JDBC driver requires database native library to be available on client but it poses several version and compatibility issue. This was liked by Database vendors though because they can reuse there existing native libraries.
3. All Java/Net-protocol driver or Type 3 JDBC driver
both type 1 and type 2 JDBC drivers were not written in Java so there was need for pure Java JDBC driver to resolve portability issue. type 3 JDBC driver comes with pure java implementation (that's why All Java word ) but it uses 3 tier architecture where you have a Java client and Java Server which talk with Net protocol and Server speaking to database. type 3 JDBC driver never get popular among database vendors as it was costly for them to rewrite there existing native database library which was mainly on C and C++.
4. All Java/Native-protocol driver or Type 4 JDBC driver
type 4 JDBC driver is most popular among all four types of JDBC driver. it has not only implemented in Java but also incorporates all database call in single driver. It was pretty easy to use and deploy as well just include driver's jar in classpath and you are ready. It also removes 3 tier architecture of type 3 JDBC driver which makes it faster than type 3. Major development happens on type 4 JDBC driver when database upgrade themselves, though some of them still upgrade native database library or type 2 driver.
That's all on quick overview of different types of JDBC drivers in Java. JDBC drivers has evolved from JDBC ODBC bridge driver to type 4 JDBC driver, which is clean and portable. There has been some buzz around JDBC driver 5 on Java community which may include some advanced functionality.