How do I convert a TreeMap that is a <String, <List<String>> to String[][]?

lag

I'm not sure what I should initialize my array String [][] ans to be.

If I set it to be the size of the treeMap then there's a NPE.

String [][] ans = new String[size][];

So I initialized to be bigger than expected but then there are nulls in my array.

Currently my treeMap is {C=[E], M=[F, B], S=[F, B], T=[F, B, L], TD=[E, L], W=[E, L], Z=[F]}

I want it to be

[[C,E], [M, F, B], [S, F, B]....]

import java.util.*;

public class Copy {
    public static String[][] oMI(String[][] m){

        //ans[0][0]= "test";
        Map<String, List<String>>  map  = new HashMap<>();
        int length = m.length;
        for (int i=0; i < m.length;i++){
            for (int j=1; j < m[i].length;j++){
                String key = m[i][j];

                map.computeIfAbsent(key, k-> new ArrayList<String>()).add(m[i][0]);
            }
        }
        Map<String,List<String>> treeMap = new TreeMap<String, List<String>>(map);
        System.out.println(treeMap);

        int size = treeMap.size();

        String [][] ans = new String[20][20];
        int l = 0;
        int s = 0;
        String ing = "";
        for (Map.Entry<String,List<String>> entry: treeMap.entrySet()){
            s = entry.getValue().size();

            if (entry.getKey() !=null)
                ing = entry.getKey();
            else ing ="null";

            ans[l][0] = ing;

            int mn = 0;
            for (String v : entry.getValue()) {
                ans[l][mn] = v;       
                l++;
                mn++;
            }
        }
        return ans;
    }

    public static void main(String[] args){

        String  [][] ma =
                {
                        {"F", "T", "Z", "S", "M"},
                        {"E", "C", "W", "TD"},
                        {"B", "M", "S", "T"},
                        {"L", "W", "T", "TD"}

                };
        oMI(ma);
    }
}
c0der

If you need a solution without using streams, you can use the following :

    int size = treeMap.size();
    String [][] ans = new String[size][]; //initialize the known dimension only
    int arrayRow = 0;
    for (Map.Entry<String,List<String>> entry: treeMap.entrySet()){

        int rowSize = entry.getValue().size() + 1;
        String[] tempArray = new String[rowSize]; //an temp array to hold key + vlaues
        tempArray[0] = entry.getKey();            //add key to temp array

        int mn = 1;
        for (String s : entry.getValue()) { tempArray[mn++] = s; } //add values to temp array
        ans[arrayRow++] = tempArray; //add temp array to output array
    }

    return ans;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i convert a String to a List<Map<String,String>>

How do I convert a string to a list of chars?

How can I convert a List<String> to String []?

how do I convert a list of custom type to a list of String

How do I convert a list represented as a string to a list?

convert TreeMap<String,Object> to List<HashMap<String,Object>>

How do I convert a string into a string array String()?

How do I convert from &alloc::string::String to a string literal?

How do I convert an array list to hash separated string?

How do I convert a C# List<string[]> to a Javascript array?

How do I convert a list of ascii values to a string in python?

How do I convert a list into a string with spaces in Python?

How do I convert a list of numbers to a string in k?

How do I convert csv string to list in pandas?

How do I convert a list of chars to a string in purescript

How do I convert a string to a list of Maybe Int

How do I convert a list of integers to a string - Python

How to convert a string to a list of string?

How do I convert List<String?> to List<String> in .NET 6 / C# 10?

how do convert a string List into type IEnumerable?

Using Java 8 streams how do I convert Map<String, List<Something>> to Map<String, Integer>?

How do I create a converter so that JsonConvert.DeserializeObject will convert List<string> properties to just string properties

How do I convert List<String[]> values from UTF-8 to String?

How to convert a list into a string?

How to convert list to string?

How to convert string into list?

How to convert a string to a list?

How can I convert a String into a Char list?

How I convert string response into Array List

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive