Search code examples
javambeans

what is tabular dataformat in java and how i can save tabular data format in to a string array list?


Hi when you have a look at MBeans classes you will come to know few classes also return data in tabular data format of java. Can anyone let me know what is this and how i can save tabular data format in to a string array list.

Java Doc Link


Solution

  • To convert the TabularData to a list of strings containing the values of each row in the table, you can use something like this.

    (Note that TabularData is an interface - TabularDataSupport is one implementation. MBeans may use their own implementation, so it's best to code to the interface rather than the implementation class.)

    String SEPARATOR = " ";  // change this as needed, e.g. to tab, comma etc.
    TabularData data = ...; // data from the mbean
    List<String> list = new ArrayList<String>();  // the output list
    for (Object v: data.values()) {
       CompositeData row = (CompositeData)v;
       StringBuilder rowString = new StringBuilder();
       for (Object rv: row.values()) {
           if (rowString.length()!=0)
                rowString.append(SEPARATOR);
           rowString.append(rv);
       }
       list.add(rowString.toString());
    }