我在ArrayList中有数据行。我需要从List中读取数据并将其转换为另一种格式并写入平面文件。
List Data:
General Details|S|!|!|66T4051|N|MACH|a
Charge Details|S|!|!|66T4051| 3827|N|
Charge Details|S|!|!|66T4051| 3828|N|
Insurance Details|S|!|!|66T4051| f|
Insurance Details|S|!|!|66T4051| h|
Insurance Details|S|!|!|66T4051| p|
这里GeneralDetails count为1,这是主数据。费用详情计数为2.保险详情计数为3。
我需要将上面的列表数据转换为以下格式的一个字符串。
ROW1|General Details|S|!|!|66T4051|N|MACH|a|2|Charge Details|S|!|!|66T4051| 3827|N|Charge Details|S|!|!|66T4051| 3828|N|3|Insurance Details|S|!|!|66T4051| f|Insurance Details|S|!|!|66T4051| h|Insurance Details|S|!|!|66T4051| p$
我需要在每条记录前放置计数。我们可以说。
ROW1 - 1 is the count of General Details
|2|Charge Details - 2 is the count of Charge Details
|3|Insurance Details - 3 is the count of Insurance Details
请建议如何实现这一目标?
我得到了每一行的计数。
map>{Charge Details=2, General Details=1,Insurance Details=3}
但我之后无法继续。
public void processRows(LinkedList<LinkedList<Object>> linkedList,String intfCode,String bankId)
{
for(int i=0;i<linkedList.size();i++)
{
List<Object> list = linkedList.get(i);
//System.out.println("list>>"+i+""+list);
List<Object> tableNameList = new ArrayList<Object>();
LinkedHashMap map = new LinkedHashMap();
for(int j=0;j<list.size();j++)
{
String rowOfLine = (String)list.get(j);
String split[] = (rowOfLine.split("\\|"));
String tableName=split[0];
tableNameList.add(tableName);
}
for(int j=0;j<list.size();j++)
{
String rowOfLine = (String)list.get(j);
String split[] = (rowOfLine.split("\\|"));
String tableName=split[0];
int count = Collections.frequency(tableNameList, tableName);
System.out.println("count>>"+count);
if(!(map.containsKey(tableName)) && tableName!=null)
{
map.put(tableName, count);
}
//transformRow(tableName,intfCode,bankId,rowOfLine,collateralType);
}
System.out.println("map>"+map);
}
}
答案 0 :(得分:0)
遍历所有列表并注意列表的正确排序。 每当新的列表类型开始在迭代之前将列表的数量附加到您的String中。
答案 1 :(得分:0)
I have rows of data in ArrayList. I need to read the data from List and convert it to another format and write to flat file.
以下是您如何解决问题的想法。请记住,我没有测试过这段代码,我的目的是向您展示如何解决您的问题。假设您的ArrayList
数据名为contents
...
Map<String, List<String>> indexedContents = new HashMap<String, List<String>>();
for (String str : contents) {
String[] splittedString = str.split("\\|");
if (splittedString.length > 0) {
//Index your map on the first entry which is something like
//"Charge Details"
if(indexedContents.get(splittedString[0]) == null){
indexedContents.put(splittedString[0], new ArrayList<String>());
}
indexedContents.get(splittedString[0]).add(str);
}
}
StringBuffer output = new StringBuffer();
for(String key : indexedContents.keySet()){
output.append(indexedContents.get(key).size()).append("|");
for(String values : indexedContents.get(key)){
output.append(values).append("|");
}
}
地图中的条目会跟踪您的尺寸,因为它是List
。最后的for循环实际上会创建输出格式,可以根据需要进行更改。我希望这个对你有用。
答案 2 :(得分:0)
以下是您要求的代码。
代码:
import java.util.ArrayList;
public class Sample1 {
public static void main( String[] str ) {
ArrayList<String> list = new ArrayList<String>();
list.add( "General Details|S|!|!|66T4051|N|MACH|a" );
list.add( "Charge Details|S|!|!|66T4051| 3827|N|" );
list.add( "Charge Details|S|!|!|66T4051| 3828|N|" );
list.add( "Insurance Details|S|!|!|66T4051| f|" );
list.add( "Insurance Details|S|!|!|66T4051| h|" );
list.add( "Insurance Details|S|!|!|66T4051| p|" );
StringBuffer mainBuf = new StringBuffer();
StringBuffer tempBuf = new StringBuffer();
int count = 0;
String header = null;
for( String item : list ) {
String[] values = item.split( "\\|" );
// if this is first element
if( header == null ) {
header = values[0];
count = 1;
}
// check if the previous element and present element is not same.
else if(!header.equals(values[0])) {
append( count, header, mainBuf, tempBuf.toString() );
tempBuf.delete( 0, tempBuf.length() );
count = 1;
header = values[0];
} else {
count++;
}
if( tempBuf.length() > 0 && tempBuf.charAt( tempBuf.length() - 1 ) != '|' ) {
tempBuf.append( "|" );
}
tempBuf.append( item );
}
append( count, header, mainBuf, tempBuf.toString() );
// replace last character as $
int lastIndex = mainBuf.length();
mainBuf.replace( lastIndex - 1, lastIndex, "$" );
System.out.println(" mainBuf => " + mainBuf );
}
public static void append( int count, String header, StringBuffer mainBuf, String str ) {
if( header.equalsIgnoreCase( "General Details" ) ) {
mainBuf.append( "ROW" + count + "|" ).append( str );
} else {
if( mainBuf.charAt( mainBuf.length() - 1 ) != '|' ) {
mainBuf.append( "|" );
}
mainBuf.append( count + "|" ).append( str );
}
}
}