Current location - Quotes Website - Personality signature - Java problem, looking for a relatively simple and reasonable algorithm.

Waiting online~

Java problem, looking for a relatively simple and reasonable algorithm.

Waiting online~

After editing for a long time, it is not displayed again. ssssss public?static?void?main(String[]?args){

//Constructing data

List?list?=?new?ArrayList();

list.add(new?Data("Three Kingdoms 12",?"03-01",?"5") );

list.add(new?Data("Thunder Fighter",?"03-01",?"2"));

list.add(new?Data ("Dynasty Warriors 7",?"03-02",?"4"));

list.add(new?Data("Dynasty Warriors 12",?"03-03",? "5"));

list.add(new?Data("Thunder Fighter",?"03-04",?"5"));

list.add (new?Data("Dynasty Warriors 7",?"03-05",?"5"));

String[][]?tableData?=?toTable(list);

String[][]?tableData?=?toTable(list);

p>

if?(log.isDebugEnabled()){

log.debug(JsonUtil.format(tableData));

}

} /** *?Assemble?config into?two-dimensional array?table. * *?@param?list *the?list *?@param?nameComparator *the?name?comparator *?@param?keyComparator *the?key ?comparator *?@return?the?string[][] */

private?static?String[][]?toTable(List?list){

//The core of this problem is to get the value through name and key.

//Therefore, for the sake of performance, design a map. The key of this map is spliced ??together by the name and key of config.

Map?map?=?new?LinkedHashMap();

//LinkedHashSet? ensures the order of the loop

Set?nameSet?=?new?LinkedHashSet();

Set?keySet?=?new?LinkedHashSet();

for?(Data?config?:?list){

String?key?=?config.getKey();

String?name?=?config.getName ();

nameSet.add(name);

keySet.add(key);

//Ensure that the keys in the map are not repeated

p>

String?k?=?key?+?"@"?+?name;

map.put(k,?config);

}

}

p>

int?rowLength?=?list.size();

int?columnLength?=?nameSet.size()?+?1;

String[] []?table?=?new?String[rowLength][columnLength];

//The first row contains name?title

table[0]?=?CollectionsUtil. toArray(nameSet);

int?i?=?0;

for?(String?key?:?keySet){

String[]? array?=?new?String[columnLength];

array[0]?=?key;

int?j?=?0;

for ?(String?name?:?nameSet){

String?k?=?key?+?"@"?+?name;

Data?config?=?map .get(k);

String?value?=?null;

if?(null?=?config){

value?=? config.getValue();

}

array[j?+?1]?=?value;

j++;

}

table[i?+?1]?=?array;

i++;

}

return?table;< /p>

} 13:34:00?DEBUG?(CopyOfDataTest.java:90)?[main()]?[

[

"Three Kingdoms 12",

"Thunder Fighter",

"Dynasty Warriors 7"

],

[

" 03-01",

"5",

"2",

null

],

[

"03-02",

null,

null,

"4"

],

[

"03-03",

"5",

null,

null

],

[

"03-04",

null,

"5" ,

null

],

[

"03-05",

null,

p>

null,

"5"

]

]