Wednesday, 9 July 2014

Find out all combinations of an array.

This is the java code to find out combinations of an array.

class Combinations
{
String DIM_ARR[]={"a","b","c"};
  public static void main (String args[])
        {
            Combinations combobj= new Combinations();
            combobj.combine(0);
        }
       
       
        private void combine(int start){
            for( int i = start;i< DIM_ARR.length; i++ ){
                tcVect.add(DIM_ARR[i]+",");
                    System.out.println(tcVect.toString());
                if ( i < DIM_ARR.length )
                combine( i + 1);
                tcVect.remove(tcVect.size()-1);
            }
        }
}

Tuesday, 8 July 2014

HashMap Sort by Value

Following is the sample code to sort hashmap by its value using custom comparator.


package code_snippet;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class SortByValueHashMap {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        HashMap<String, Integer> hMap=new HashMap<String, Integer>();
        hMap.put("a", 10);
        hMap.put("b", 1);
        hMap.put("c", 100);
        System.out.println("Without Sorting="+hMap);