Java 从列表中读取数据并创建字符串以写入平面文件
我在ArrayList中有数据行。我需要从列表中读取数据,并将其转换为另一种格式,然后写入平面文件Java 从列表中读取数据并创建字符串以写入平面文件,java,Java,我在ArrayList中有数据行。我需要从列表中读取数据,并将其转换为另一种格式,然后写入平面文件 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|
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计数为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);
}
}
public void processRows(LinkedList LinkedList、String intfCode、String bankId)
{
对于(int i=0;i遍历所有列表,并注意列表的正确顺序。
每当一个新的列表类型开始时,在迭代之前将列表数附加到字符串中。我在ArrayList中有数据行。我需要从列表中读取数据,并将其转换为另一种格式,然后写入平面文件。
以下是您如何解决问题的想法。请记住,我没有测试此代码,我的目的是向您展示如何解决问题的想法。假设您的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("|");
}
}
Map indexedContents=newhashmap();
for(字符串str:contents){
String[]splittedString=str.split(“\\\\”);
如果(splittedString.length>0){
//在第一个条目上为地图编制索引,如下所示
//“费用明细”
if(indexedContents.get(splittedString[0])==null){
put(splittedString[0],new ArrayList());
}
获取(拆分字符串[0]).add(str);
}
}
StringBuffer输出=新的StringBuffer();
for(字符串键:indexedContents.keySet()){
append(indexedContents.get(key.size()).append(“|”);
for(字符串值:indexedContents.get(键)){
append(值)。append(“|”);
}
}
地图中的条目将跟踪您的大小,因为它是一个列表。
。最终的for循环实际上创建了您的输出格式,可以根据您的需要进行更改。我希望这会有所帮助。以下是您所需的代码
代码:
导入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 );
}
}
}
公共类样本1{
公共静态void main(字符串[]str){
ArrayList=新建ArrayList();
列表。添加(“一般详细信息”| S |!|!| 66T4051 | N |马赫| a”);
列表。添加(“费用明细”| S |!|!| 66T4051 | 3827 | N |);
列表。添加(“费用明细”| S |!|!| 66T4051 | 3828 | N |);
列表。添加(“保险详情”| S |!|!| 66T4051 | f |”);
列表。添加(“保险详情”| S |!|!| 66T4051 | h |”);
列表。添加(“保险详情”| S |!|!| 66T4051 | p |”);
StringBuffer mainBuf=新的StringBuffer();
StringBuffer tempBuf=新的StringBuffer();
整数计数=0;
字符串头=null;
用于(字符串项:列表){
字符串[]值=item.split(“\\\\”);
//如果这是第一个元素
if(头==null){
标题=值[0];
计数=1;
}
//检查上一个元素和当前元素是否不同。
如果(!header.equals(值[0]),则为else{
追加(count、header、mainBuf、tempBuf.toString());
tempBuf.delete(0,tempBuf.length());
计数=1;
标题=值[0];
}否则{
计数++;
}
if(tempBuf.length()>0&&tempBuf.charAt(tempBuf.length()-1)!=“|”){
tempBuf.追加(“|”);
}
临时追加(项目);
}
追加(count、header、mainBuf、tempBuf.toString());
//将最后一个字符替换为$
int lastIndex=mainBuf.length();
mainBuf.replace(lastIndex-1,lastIndex,“$”);
System.out.println(“mainBuf=>”+mainBuf);
}
公共静态void append(int计数、字符串头、StringBuffer mainBuf、String str){
if(标题等信号案例(“一般细节”)){
mainBuf.append(“行”+计数+“|”).append(str);
}否则{
if(mainBuf.charAt(mainBuf.length()-1)!=“|”){
mainBuf.追加(“|”);
}
mainBuf.append(count+“|”)append(str);
}
}
}
请显示一些代码和努力。是的。我发布了我尝试过的代码。根据您的示例输入,您的地图似乎没有正确的项目计数。。。