0

The input my program gets are a few strings of characters which represents rows in a matrix. I would like to put the columns of this matrix in a LinkedHashMap, but without first storing everything in an array and then construct the columns and put them in the LinkedHashMap. The idea is that this should be done as fast as possible.

Example:

Input:

abfad

grgaa

rtwag

The strings that should be saved in a LinkedHashMap are:

agr

brt

fgw

aaa

dag

Edit:

This is how my program does it right now:

String[][] array = new String[n][m];
LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();  
for (int i = 0; i < n; i++) {
        array[i] = br.readLine().split("");
}
for (int i = 0; i < m; i++) {
    String column = "";
    for (int j = 0; j < n; j++) {
        column += array[j][i];
    }
    map.put(i, column);
}
Community
  • 1
  • 1
borisjo
  • 158
  • 1
  • 16
  • What have you tried doing so far? Show your code and explain what problem you have with it – UnholySheep Mar 02 '17 at 12:46
  • I store the individual characters in an array and then append the columns and store them in a LinkedHashMap, see the edit. My program runs too slow and this is the only part where I can improve my program on. – borisjo Mar 02 '17 at 12:51
  • Well, you could try using a 1-dimensional array of strings (and not splitting) and then accessing the individual chars via [`charAt`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int)) in the inner loop. Also you should consider using a `StringBuilder` instead of appending via `+=`. Another thing you could try would be to store the characters in the map right after you read the line (and append to the strings stored in the map after each line read) - you'll have to test whether any of these approaches is good enough – UnholySheep Mar 02 '17 at 13:08
  • I would use `String.toCharArray()` and build a 2-dimensional `char` array. Then use a loop (and `StringBuilder` as @UnholySheep said) to construct the strings for the map. – Ole V.V. Mar 02 '17 at 13:15
  • @UnholySheep StringBuilder worked, thank you! Now I remember that appending strings is very time inefficient and this solution made my program efficient enough. – borisjo Mar 02 '17 at 13:32
  • Why are you using a `Map` of consecutive integers as keys? That gives you exactly the same functionality as a one-dimensional array (i.e. absolutely no advantages over a simple array) but _much_ slower. – Klitos Kyriacou Mar 03 '17 at 09:41

1 Answers1

1

I am not sure why this is downvoted.

It's an interesting problem that you should perhaps ask a little better.

Does it help if you think of this as a real-time transform of a matrix? What should happen when you read the first line? What should happen for each subsequent line? We are assuming that all lines have same length, otherwise we'll need to specify what happens when they are not.

After you have tried on your own, maybe you can take a look below. I have not used LinkedHashMap there, also I have used the mutable StringBuilder. But you can easily modify it to use the data structure of your choice.

public class RealTimeTransform {
    public static void main(String[] args) throws IOException {
        Map<Integer, StringBuilder> columns = new HashMap<>();
        readColumnsFromRows(columns, System.in);
        System.out.println(columns.values());
    }

    private static void readColumnsFromRows(Map<Integer, StringBuilder> columns, InputStream in) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        int rc = 0;
        String row;
        while ((row = reader.readLine()) != null) {
            if (rc == 0) {
                for (int i = 0; i < row.length(); i++) {
                    columns.put(i, new StringBuilder(10).append(row.charAt(i)));
                }
            } else {
                for (int i = 0; i < row.length(); i++) {
                    columns.get(i).append(row.charAt(i));
                }
            }
            rc += 1;
        }
    }
}

This prints:

aman
plan
yess
[apy, mle, aas, nns]

per your specification.

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
  • Using StringBuilder as someone else commented aswell, my program indeed became significantly faster. For the particular problem I face, I need to make use of a LinkedHashMap but the problem would be the same for a HashMap. Thanks for your time. – borisjo Mar 02 '17 at 13:35