Home     RSSRSS

Java – Accept Input String and Return array of Hashmaps.

February 14, 2016 by kiranbadi1991 | Filed under Development.

Recently I came across one of the exciting interview question,write the method that accepts input string and returns array of hashmaps.

Below are my 2 attempts, first one is somewhat wrong and second one is very close to correct but bit of incomplete.


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author kiran
 */

/*
 * To execute Java, please define "static void main" on a class
 * named Solution.
 *
 * If you need more classes, simply define them inline.
 
 Write a method for parsing strings. Input is a string and then return an array of hashmaps.
 
 Input = "This is a test. And this is another test."
 Return = [{1=>"This", 2=>"is"..},{1=>"And", 2=>"this"...}..]
 
 */
public class ParseString {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your Input String: ");
        String Input = scanner.nextLine();
   //     String Input = "This is a test. And this is another test.";
        List<Map<Integer, String>> list = new ArrayList<>();
        int length = Input.length();
        StringTokenizer st = new StringTokenizer(Input);
        while (st.hasMoreElements()) {
            length++;
            for (int i = 0; i <= length; i++) {
                int key = i;
                String value = st.hasMoreTokens() ? st.nextToken() : null;
                if (value != null) {
                    Map<Integer, String> map = new HashMap<>();
                    if (!map.containsKey(key)) {
                        map.put(key, value);
                    }

                    System.out.println("key : " + key + " value : " + value);
                    list.add(map);
                }
            }
        }
        System.out.println("List is : " + list);

    }

}




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author kiran
 */

/*
 * To execute Java, please define "static void main" on a class
 * named Solution.
 *
 * If you need more classes, simply define them inline.
 
 Write a method for parsing strings. Input is a string and then return an array of hashmaps.
 
 Input = "This is a test. And this is another test."
 Return = [{1=>"This", 2=>"is"..},{1=>"And", 2=>"this"...}..]
 
 */
public class ParseStringReturnHashmap {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your Input String: ");
        String Input = scanner.nextLine();
 //       String Input = "This is a test. And this is another test.";
        List<Map<Integer, String>> list = new ArrayList<>();
        StringTokenizer st = new StringTokenizer(Input, ".");
        while (st.hasMoreElements()) {
            StringTokenizer str = new StringTokenizer((String) st.nextElement());
            int count = str.countTokens();
            while (str.hasMoreElements()) {
                Map<Integer, String> map = new HashMap<>();
                for (int i = 1; i <= count; i++) {
                    int key = i;
                    String value = str.hasMoreTokens() ? str.nextToken() : null;
                    map.put(key, value);
                }
                list.add(map);
              
            }

        }
          System.out.println("List is : " + list);
    }

}


I do code in java ee but only for my sites(My site does use lot of hashmaps/lists), and I do not play around a lot with collection just only when I need get data from server namely as DTO Objects and at times on view layers.My thoughts are that if you are doing lot of contains or chop and match kind of operation with collection, it only means that your data model is somewhat broken and you are trying to avoid refactoring impacts.That’s my experience based on my programming abilities.

← Previous

Comments are disabled