Skip to main content

Posts

Showing posts from 2011

Transform XML into Objects

In the last post we saw how to serialize an object into XML content. Here we are going to just do the reverse of it. So create a project named XMLBinding2 as described in the last post. Copy past the following XSD and the sample XML to your project as shown in the screenshot. student-marks.xsd <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xml.netbeans.org/schema/student-marks" xmlns:tns="http://xml.netbeans.org/schema/student-marks" elementFormDefault="qualified"> <xsd:element name="report"> <xsd:complexType> <xsd:sequence> <xsd:element name="students"> <xsd:complexType> <xsd:sequence> <xsd:element name="student" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> ...

Hidden facts about HashMap and HashSet

We all know that hashcode is used when you store objects in map or set. And we all know that the hashcode() method of the object is used to uniquely identify the object stored in the set and that HashSet will not store duplicate values. What do we derive from these? Shall we say, "Objects having same hashcode are considered as same"?! Guess what is the output of the following program. public class HashCodeTest { static class Person { int hashCode = 1; @Override public int hashCode() { return hashCode; } } public static void main(String[] args) { Set persons = new HashSet (); persons.add(new Person()); persons.add(new Person()); persons.add(new Person()); persons.add(new Person()); System.out.println("Set size: " + persons.size()); Map map = new HashMap (); map.put(new Person(), 1); map.put(new Person(), 2); map.put(new Person(), 3); map.put(new Person(), 4); System.out.pr...

Basic Java Interview Questions

Show answers Hide answers What is the difference between Java and C++? Java C++ Memory managed (or garbage collected) language Programmer has to manage the heap memory. Compiled java code is platform independent C++ binary code is platform specific. Compiled and Interpreted language Compiled language Pure object oriented Functions can be disassociated from any class Supports multilevel inheritance Supports multiple and multilevel inheritance Supports function overloading Supports function and operator overloading Supports only signed numeric data types Supports both signed and unsigned data types Supports unicode characters and char type takes 2 bytes in memory Doesn't support unicode characters by default Is Java a compiled language or an interpreted language? Java is both compiled (to byte code) and interpreted language. What is byte code? Source java file is compiled into a intermediate code before interpreted by JVM. This intermediate code is called byt...

Transform Object into XML using JAXB

In the last post we had seen how to transform XML data into user presentable HTML format . What if you don't have data in XML format? What if the data is collected from a legacy system or from a collection of services? Here we are going to look at how to transform the Java object to XML and this XML can be presented in any format we want. Like JAXP, Java comes to rescue us here with JAXB (Java Architecture for XML Binding). All we need is an XSD which defines the structure of the Java Object and the XML. JAXB allows us to transform data in both the direction, means, you can convert Object to XML and XML to Object. Wow, what are we then waiting for? Let's program. Open Netbeans and create a Java Application File->New Project->Java Application Enter "XMLBinding" in the Project Name field Click Finish Create an XSD file Open Projects tab Right click xmlbinding package node Choose XML-> XML Schema Enter "student-marks...

Collection of user defined comparable objects (Part 2)

Yesterday we discussed how to make user defined objects comparable to themselves . Let’s take that to one step further today. In yesterdays post, our requirement was to sort the collection of employees that belong to a division, by employee id, so that the end result will be a collection of employees in seniority order. Now there comes the payroll division and it says, for each division, you need to print the list of employees based on the salary drawn. You may say “It is very simple; I had done all the ground work yesterday; so just need to change the comparison logic as below”. public class Employee implements Comparable<Employee> { private String name; private int employeeNo; private float salary; public int compareTo(Employee e) { if (salary == e.salary) { return 0; } else if (salary > e.salary) { return 1; } else { return -1; } } } Now the human resources division shouts “What have you done? Now e...

Collection of user defined comparable objects (Part 1)

Let us take array of integers. int[] numbers = new int[] {2,5,4,8,9}; Now, how will you print the numbers in ascending order? You can use Arrays class to sort any type of arrays as follows, java.util.Arrays.sort(numbers); System.out.println(Arrays.toString(numbers)); Your output will be [2, 4, 5, 8, 9] . How can you achieve the same if the data structure is a collection? You can use the class java.util.Collections as follows, List<Integer> numberList = new ArrayList<Integer>(Arrays.asList(2, 5, 4, 8, 9)); Collections.sort(numberList); System.out.println(numberList.toString()); Now assume that you are designing an employee management system. So you will have a class to represent the employee details. Let’s call that class as Employee. class Employee { String name; int employeeNo; ... } Assume that each division maintains a collection of employees. Now you are asked to list the employees in each division in ascending order sorted by employeeNo. You may think what is wro...

How to inherit an inner class?

Consider the following example, class A { class B { B(int i) { ... } } ... } Sub-classing B inside class A is very simple because both class B and its sub-class, say C, require the instance of class A and can be done as follows. class A { class B { B(int i) { ... } } class C extends B { C(int i) { super(i); ... } } ... } However, if you want to sub-class B outside class A, as follows, class C extends A.B { C(int i) { super(i); ... } ... } you will get a compiler error as "an enclosing instance that contains A.B is required". In order to solve this problem, you need to get an instance of A, as follows, class C extends A.B { C(A a, int i) { a.super(i); } } Here is the compelete code. public class A { class B { int i; B(int i) { this.i = i; } } } class C extends A.B { C(A a, int i) { a.super(i); } public static void main(String[] args) { A a = new A(); ...

How to use XML and XSL in Java?

Almost everyday, somewhere we come across XML and we know that many systems use XML as their data store; more precisely, they talk in XML language. Now, how do I write an application where I need to present the data, which is in XML format, to users? Is it possible to write XML processing application in Java? Yes, Java SE provides extensive support for XML parsing and processing. Java supports two types of XML parsers, viz, SAX and DOM . These parsers and their supporting classes are collectively called JAXP , Java API for XML Processing. Now given a XML document, how do I transform it in a user presentable format rather than parsing the XML document? Or do I need to parse the XML to transform it? No and may be. No - you don't have to parse it to transform it; java will do it for you with the help of XSL . May be - if you want to. Enough of theory. Let's program. Here is the sample XML file. <?xml version="1.0" encoding="UTF-8"?...