Thursday, July 21, 2011

Comparator in Java

Sorting a Collection of object without implementing the Comparator/Comparable 

This java program describes how to sort collection of user defined objects using Comparator as anonymous class(without implementing the Comparator interface)

There are two ways we can sort the List of user defined objects in java

1. Implement the Comparator/Comparable interface by userdefined  class
2. Without  implementing  Comparator interface(using anonymous class)

Now am going to show the example which will sort the userdefined objects using
Comparator interface as anonymous class(ie. without implementing the Comparator interface)

My Model Object : Sample.java

package com.vels.test;

/**
 * @author velmurugan pousel
 * created on Jul 21, 2011 for test
 */

public class Sample {

 public String id;
 public String name;
 public int age;

 Sample(String ID, String nm, int ag){
    this.id = ID;
    this.name = nm;
    this.age = ag;
 }

 /**
  * @return the id
  */
 public String getId() {
  return id;
 }

 /**
  * @param id the id to set
  */
 public void setId(String id) {
  this.id = id;
 }

 /**
  * @return the name
  */
 public String getName() {
  return name;
 }

 /**
  * @param name the name to set
  */
 public void setName(String name) {
  this.name = name;
 }

 /**
  * @return the age
  */
 public int getAge() {
  return age;
 }

 /**
  * @param age the age to set
  */
 public void setAge(int age) {
  this.age = age;
 }

}

Now am going to sort the list of Sample class object based on the ID field.

Here is my main method :


package com.vels.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author velmurugan pousel
 * created on Jul 21, 2011 for test
 */

public class ComparatorTest {

 public static void main(String[] args) {

  List<Sample> list = new ArrayList<Sample>();
 
  Sample s1 = new Sample("1", "amin", 12); 
  list.add(s1);

  s1 = new Sample("5", "ver", 21);
  list.add(s1);
 
  s1 = new Sample("1", "hji", 14);
  list.add(s1);
 
  s1 = new Sample("7", "loli", 24);
  list.add(s1);
 
  s1 = new Sample("3", "zuri", 12);
  list.add(s1);
  s1 = new Sample("1", "zuri", 12);
  list.add(s1);

  for (Sample s : list) {
   System.out.println(" id..." + s.getId() + " name..." + s.getName());
  }

// here is the most important part

  Collections.sort(list, new Comparator<Sample>() {
   //string comparation here ID is the string field
              public int compare(Sample s1, Sample s2) {
                         int result = s1.getId().compareTo(s2.getId());
                         return result;
               }
     }  );

 //After sorting

  for (Sample s : list) {
   System.out.println(" name..." + s.getName());
   System.out.println(" id..." + s.getId());
  }

 } 
 
}

Output : Before Sorting

id...1 name...amin
id...5 name...ver
id...1 name...hji
id...7 name...loli
id...3 name...zuri
id...1 name...zuri

After Sorting :
id...1 name...amin
id...1 name...hji
id...1 name...zuri
id...3 name...zuri
id...5 name...ver
id...7 name...loli
..

To compare the int type

Collections.sort(list, new Comparator<Sample>() {
   //string comparation here ID is the string field
              public int compare(Sample s1, Sample s2) {
                         int result = s1.getAge() - s2.getAge();
                         return result;
               }
     }  );


 

 

Wednesday, July 13, 2011

ZULU Time validation in java

Zulu Time validation in java

Following are the zulu based time format
HH:mm:ss.SSS'Z'
HH:mm:ss'Z'
HH:mm:ss
..
...
// we can pass the pattern whatever we want

String format = "HH:mm:ss.SSS'Z'";

SimpleDateFormat zuluFmt = new SimpleDateFormat(format);
zuluFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(" checkind time" + zuluFmt.parse("14:45:50.234Z"));
....

JAXB Namespacing - including xmlns:xsi and xsi:schemalocation in namespace

How to include xmlns:xsi and xsi:schemalocation into the namespace while marshalling

1. Add the xmlns:xsi attributes in @XmlSchema in packge-info.java

  @java.xml.bind.annataion.XmlSchema (
       xmlns={ @javax.xml.bind.annataion.XMlNs(prefix="xsi" 
       namespaceURIU=http://www.vels.com/schemadef },
       namespace=http://ww.vels.com/2/,
      elementFormDefault = javax.xml.bind.annatation.XmlForm.QUALIFIED)
  package com.vels.test.schema;

2.Add the schmalocation uri JAXB_SCHEMA_LOCATION_URI in "marshallingproperties " of Jaxb2Marshaller class in spring-ws-servlet.xml

<bean id="marshaller" calss="org.springframework.oxm.jaxb.Jaxb2Marshaller">
     <property names="classesToBound">
             <list>
                    <value> com.vels.test.Sample.java </value>
              </list>
  </property>
//since its a static field property we need to use util:constant from spring dtd
 <property name="marshallingProperties">
         <map>
               <entry>
                      <key><util:constant static-field="javax.xml.bind.helpers.AbstractMarshallerImpl.JAXB_SCHEMA_LOCATION" /> </key>
<value> http://vels.com/vel.xsd </value>
</map>
...

your output xml looks like
<out xmlns="asdsd" xmlns:xsi="sdsdf" xsi:schemalocation=http://vels.com/vels.xsd>
....
</out>