-2

Say we have a HashMap of keys and values, where keys are Integers and values are Lists, then I throw construct a treeMap using the hashMap to put it in order:

Map<Integer, List<Boolean>> map = new HashMap<>();
Map<Integer, List<Boolean>> orderedMap = new TreeMap<>(map);

If I were to do

map.entrySet().forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue())

It spits out each and every key with its value after every line. This is something that I'm going for but I think its easier on the eye to maybe print out certain rows x columns. Maybe possibly printing 10 keys:values per row before moving onto the next one, or preferably maybe printing out 10 keys:values in order in the first column, and then the next column print the next 10 and so forth until it reaches the end (last column does not have to be exactly 10), but I have no idea how to reach this goal

Sample output wanted:

entry1  entry11 ...
entry2  entry12
entry3  entry13
entry4  entry14
entry5  entry15
entry6  .
entry7  .
...     .
entry10 entry20
IHaveAQuestion
  • 143
  • 2
  • 12

3 Answers3

0

You might want to go for something like this using two nested loops (one for rows and one for columns) and Iterator<Entry<Integer, List<Boolean>>> as long as you cannot get an entry from a map based on its index like it's possible with a List.

// define the row size and the Iterator
int rows = 10; 
Iterator<Map.Entry<Integer, List<Boolean>>> iterator = map.entrySet().iterator();

for (int i=0; i<rows; i++) {                                           // for each row
    for (int j = 0; j < map.size(); j += rows) {                       // .. for each column
        if (iterator.hasNext()) {                                      // .. if there is a next value 
            Map.Entry<Integer, List<Boolean>> entry = iterator.next(); // .... get it
            System.out.print(entry);                                   // .... print the line
        } else break;                                                  // .... or else break the loop
        System.out.print(" \t");                                       // .. and print the tabulator
    }
    System.out.println();                                              // move to the next line (row)
}

Notice where is used print and println. Here is a sample output for 38 entries:

0=[false, true]     1=[true, false]     2=[false, true]     3=[true, false]     
4=[false, true]     5=[true, false]     6=[false, true]     7=[true, false]     
8=[false, true]     9=[true, false]     10=[false, true]    11=[true, false]    
12=[false, true]    13=[true, false]    14=[false, true]    15=[true, false]    
16=[false, true]    17=[true, false]    18=[false, true]    19=[true, false]    
20=[false, true]    21=[true, false]    22=[false, true]    23=[true, false]    
24=[false, true]    25=[true, false]    26=[false, true]    27=[true, false]    
28=[false, true]    29=[true, false]    30=[false, true]    31=[true, false]    
32=[false, true]    33=[true, false]    34=[false, true]    35=[true, false]    
36=[false, true]    37=[true, false]    

Disclaimer: The code asks for a lot of improvements, i.e. the tab adjusting when irregular length of the output occurs etc.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
0

If you are looking only for the debugging/logging purpose, then here is a simple one which prints entry by columns and not by rows and doesn't iterate your map more than once.

Here maxCols is the number of columns you want to have when you print.

    public static void main(String[] args) {
        Map<Integer, List<Boolean>> map = new HashMap<>();

        map.put(1, Arrays.asList(true, false, false));
        map.put(2, Arrays.asList(false, false, false));
        map.put(3, Arrays.asList(true, true, false));
        map.put(4, Arrays.asList(true, false, true));
        map.put(5, Arrays.asList(false, true, true));
        map.put(6, Arrays.asList(true, false, false));
        map.put(7, Arrays.asList(false, false, false));
        map.put(8, Arrays.asList(true, true, false));
        map.put(9, Arrays.asList(true, false, true));
        map.put(10, Arrays.asList(false, true, true));

        int counter = 1;
        int maxCols = 5;
        for (Map.Entry<Integer, List<Boolean>> entry : map.entrySet()) {
            System.out.printf("%s ", entry);
            counter++;

            if (counter == maxCols) {
                System.out.println();
                counter = 1;
            }
        }
    }
Jay
  • 1,539
  • 1
  • 16
  • 27
-1

I would look at the entries as a list and traverse it with two normal loops. Here a pseudo-code:

List<HashMap.Entry> entries = ...;
for (int i = 0; i < entries.size()/10; i++) {
  for (int j = i; j < entries.size(); j+=10) {
    HashMap.Entry entry = map.getEntry(j);
    System.out.print(entry + '\t');
  }
  System.out.println();
}

Alternatively, you can create a StringBuffer array with 10 rows and when traversing, append the output to the correct line. Then you print all the lines. Storing the output in a 2D array prior to printing is a valid option as well and might be the best if you want to make sure that all entries in a column have the proper amount of tailing spaces/tabulators.

akrinah
  • 119
  • 1
  • 8