Posts

Showing posts with the label Java Program

sorting a List containing Object[] elements

Problem Statement:  How can we  sort a List containing Object[] elements? The Solution: Ever wondered how to sort a list full of Object[]? The answer is to use a Comparator I have used an anonymous class which implements compare method to sort the List of Object[] package  example.collections; import  java.util.ArrayList; import  java.util.Collections; import  java.util.Comparator; import  java.util.List; public   class  Sorting {         public   static   void  main(String[] args) {               List<Object[]> list =  new  ArrayList<Object[]>();                             list.add( new  Object[]{123,  "abc" , 212})...

Annotations in Java

Annotations are the tags that can be inserted into a Java programs so that they can be processed by the tools. In the Java programming language, an annotation is used like a modifier, and it is placed before the annotated item, without a semicolon. The name of each annotation is preceded by an @ symbol.  For Example: @Override   public boolean equals(Object obj){  ...  }  Annotation Syntax An annotation is defined by an annotation interface:  public  @interface AnnotationName {   //element declarations    type elementName () ;    type   elementName () default value ;   . . .  }  One of the following   type can be used for annotation fields: All primitives (boolean, char, short, int, long, double) String Class Enum Array of any of above types Meta-Annotations Meta annotations are annotations that are used to annotate annotations. T...

How to scale image using Java

In this post we will see how can we scale an image to a desired dimension.  In the following code snippet the scaleImage method is responsible for scaling the source image to the given height & width. Instead of overwriting the original file the code creates a new file for the scaled image version. public static String  scaleImage(String imgSrc, int width, int height, String dest){   try{ File f = new File(imgSrc); //Reads the Image from the given URL BufferedImage img = ImageIO.read(f); //Scales the BufferedImage to the desired dimensions    Image scaledImg = img.getScaledInstance(width, height, Image. SCALE_AREA_AVERAGING );         //'coz the Image object cannot be written to disk directly     //We have to recreate a new BufferedImage instance from the scaled Image instance    BufferedImage biScaledImg = toBufferedImage(scaledImg, BufferedImage. TYPE_INT_RGB );     ...

Using Solr Spellchecker from Java

Continuing from my prevoius post Implementing Spellchecker in Solr , we'll now see how to use SorlJ API to fetch spellcheck suggestions through a Java program. public static Map<String, List<String>> getSuggestions(String queryStr){ if (queryStr != null &&  !queryStr.isEmpty()) { try { SolrServer server = new HttpSolrServer("http://localhost:8983/solr ");    ModifiableSolrParams params = new ModifiableSolrParams();    params.set("qt", "/spell");    params.set("q", queryStr);    params.set("spellcheck", "true");    params.set("spellcheck.collate", "true");    QueryResponse response = server.query(params);    System.out.println("response = " + response);    if(response != null){       SpellCheckResponse scr = response.getSpellCheckResponse();     if(scr != null...

Is Binary search tree - Java Program

  /**    * Following method can be used to test if a tree meets the conditions to be a binary search tree (BST).    */    public boolean isBST(TreeNode root) {       if (root == null)           return false;       return( isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE) );   }   /**     * Recursive Method - which validates very sub tree of a given binary tree.    * Works in O(n) time -- visits each node only once.     * @param min   For left sub tree it should be Integer.MIN_Value    *                        For right sub tree it should be node.data + 1    * @param max For left sub tree it should be node.data    *                     For right sub tree it should be Integer.MAX_...