skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Showing posts with label Java Map Example. Show all posts
Showing posts with label Java Map Example. Show all posts
Friday, October 19, 2012

Array to Map

Posted by Raju Gupta at 9:00 PM – 0 comments
 
This method will convert data type Array to Map in key value pair.

String[][] countries = { { "United States", "New York" }, { "United Kingdom", "London" },
        { "Netherland", "Amsterdam" }, { "Japan", "Tokyo" }, { "France", "Paris" } };
 
    Map countryCapitals = ArrayUtils.toMap(countries);

[ Read More ]
Read more...
Tuesday, October 16, 2012

Splitting the Elements in One Array and Adding into Map based on Length in Second Array

Posted by Raju Gupta at 9:00 AM – 0 comments
 
The Code Snippet will split the array elements using Comma and add this into List. After this it will add the id value as key and extract the SubList from the parentList, adding the SubList as value based on the length from a different array.

/**
  * @param args
  * 
  */
 public static void main(String[] args){
  
  String[] array = {"23,29,1", "3,36,1", "13,43,1", "15,50,1", "23,57,1", "23,64,1", "23,71,1", "23,78,1", "23,85,1", "23,92,1", "23,99,1"};
  String[] arraySize = {"6", "5"};
  String[] idKey = {"29", "30"};
  
  //Initialising List holds the values for all sections
  List<String> averageList = new ArrayList<String>();
  //Initialising sectionMap idKey and averageList values as key and value pairs. 
  HashMap<String, List> sectionMap = new HashMap<String, List>();
  //Iterate the Array to split the elements using Comma
  for (String average: array){
   String[] details = average.split(",");
   averageList.add(details[0]); 
  } 
  
  int endIndex = 0;
  int beginIndex = 0;
  /* Iterate the arraySize to add idKey 
   * and averageList values as key and value pairs. 
   */ 
  for(int i=0; i<arraySize.length; i++){
   if (i==0){
    beginIndex = 0;
    //Assigning the endIndex from the arraySize
    endIndex = new Integer(arraySize[i]);
    //Adding the idKey and extracting the subList from the averageList into sectionMap
    sectionMap.put(idKey[i], averageList.subList(beginIndex, endIndex));
   }else {
    //Assigning endIndex as beginIndex for i>0
    beginIndex = endIndex;
    endIndex = endIndex + new Integer(arraySize[i]);
    sectionMap.put(idKey[i], averageList.subList(beginIndex, endIndex));
   }
  }
  System.out.println("sectionMap -->" +sectionMap);
 }

[ Read More ]
Read more...
Monday, October 15, 2012

Print contents of a map of type String ArrayList

Posted by Raju Gupta at 12:00 PM – 0 comments
 

The code snippet allows user to iterate through the contents of a map with String , ArrayList entryset

The solution provides an easy way to iterate through the map. The solution can be used in phonebook examples, where many contact numbers can be stored against a single user.


import java.io.IOException;
import java.util.*;

public class NewClass1 {
 public static void main(String[] args) throws IOException {
  Map<String, List> numMap = new HashMap<String, List>();
  numMap.put("Key1", new ArrayList(Arrays.asList("K1V1", "K1V2", "K1V3")));
  numMap.put("Key2", new ArrayList(Arrays.asList("K2V1", "K2V2", "K2V3")));
  numMap.put("Key3", new ArrayList(Arrays.asList("K3V3", "K3V2", "K3V3")));
  numMap.put("Key4", new ArrayList(Arrays.asList("K4V1", "K4V2", "K4V3")));

  Iterator itr = numMap.entrySet().iterator();

  while (itr.hasNext()) {

   Map.Entry keyValue = (Map.Entry) itr.next();

   System.out.println("Key: " + keyValue.getKey());

   /* Iterate through the list of each key */

   List value = (List) keyValue.getValue();

   for (int i = 0; i < value.size(); i++) {

    if (value != null) {

     if (value.get(i) != null) {

      System.out.println("Value: " + value.get(i));

     }

    }

   }

  }

 }
}

[ Read More ]
Read more...

Sort map key in ascending order

Posted by Raju Gupta at 10:00 AM – 0 comments
 
The code snippet can be plugged in to any java code, to sort the defined map in ascending order

public class NewClass1 {

 public static void main(String[] args) throws IOException {
  Map numMap = new HashMap();
  numMap.put("Key1", new ArrayList(Arrays.asList("K1V1", "K1V2", "K1V3")));
  numMap.put("Key2", new ArrayList(Arrays.asList("K2V1", "K2V2", "K2V3")));
  numMap.put("Key3", new ArrayList(Arrays.asList("K3V3", "K3V2", "K3V3")));
  numMap.put("Key4", new ArrayList(Arrays.asList("K4V1", "K4V2", "K4V3")));

  /* Sort map in ascending order */

  Map sortedMap = new TreeMap(numMap);
  Iterator itr = sortedMap.entrySet().iterator();
  System.out.println("In Ascending Order: ");

  /* Print map contents */

  while (itr.hasNext()) {

   Map.Entry keyValue = (Map.Entry) itr.next();

   System.out.println("Key: " + keyValue.getKey());

   List value = (List) keyValue.getValue();

   for (int i = 0; i < value.size(); i++) {

    if (value != null) {

     if (value.get(i) != null) {

      System.out.println("Value: " + value.get(i));

     }

    }

   }

  }
 }
}


[ Read More ]
Read more...

Sort map in descending order

Posted by Raju Gupta at 8:00 AM – 0 comments
 
The code snippet provides a solution to sort the map in descending order.

public class NewClass1 {

 public static void main(String[] args) throws IOException {
  System.out.println("In Descending Order: ");

  /* Compares the keys and sorts the map in descending order */

  TreeMap map = new TreeMap(new Mycompare());
  map.put("Key1", new ArrayList(Arrays.asList("K1V1", "K1V2", "K1V3")));
  map.put("Key2", new ArrayList(Arrays.asList("K2V1", "K2V2", "K2V3")));
  map.put("Key3", new ArrayList(Arrays.asList("K3V3", "K3V2", "K3V3")));
  map.put("Key4", new ArrayList(Arrays.asList("K4V1", "K4V2", "K4V3")));
  Iterator itr1 = map.entrySet().iterator();

  /* To print the contents of the map */

  while (itr1.hasNext()) {

   Map.Entry keyValue = (Map.Entry) itr1.next();

   System.out.println("Key: " + keyValue.getKey());

   List value = (List) keyValue.getValue();

   for (int i = 0; i < value.size(); i++) {

    if (value != null) {

     if (value.get(i) != null) {

      System.out.println("Value: " + value.get(i));
     }
    }
   }
  }
 }
}

class Mycompare implements Comparator {

 public int compare(Object o1, Object o2)
 {
  String i1 = (String) o1;
  String i2 = (String) o2;
  return -i1.compareTo(i2);
 }

}


[ Read More ]
Read more...
Older Posts
Subscribe to: Posts ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger