问题描述: -
我有两个文件。每个文件都有 order_number ,这是一个随机的字母数字字符串,它们彼此是唯一的。 这两个文件包含相同数量的 order_number ,如果一个 order_number 在一个文件中,则无论文件中的位置如何,它都必须存在于另一个文件中。
下面是示例苍蝇:
File1中
abc123
def234
skd109
djkk09
nknk323
文件2
skd109
def234
abc123
nknk323
djkk09
现在我要编写一个以 file1 为参考的程序,根据 file2 中的位置查看相对翻转的订单。
例如,考虑以上两个文件。 file1中的Order_number abc123 位于第1位,但在file2中位于第3位,它在file2中分别位于两个order_number( skd109和def234 )之后。
Order_number def234 在file2中的 skd109 之后。
并且翻转了order_number djkk09和nknk323。
我的程序输出应如下所示:
abc123 is flipped by 2 ( +ve sign because it came after )
def234 is flipped by 0
skd109 is flipped by -2 ( -ve sign because it came before )
djkk09 is flipped by 1
nknk323 is flipped by -1
注意:每个文件中都有超过百万条记录。所以解决方案应该是有效的。
以下是我的代码: -
import java.util.*;
import java.io.*;
public class Matching {
public static void main( String args[]) throws IOException{
/* Variable Declaration */
int ordersFlipped = 0;
int ordersNotFlipped = 0;
String lineFile1 = null;
String lineFile2 = null;
int indexFile2 = 0;
int indexFile1 = 0;
/* Scanner */
BufferedReader file2 = new BufferedReader(new FileReader("FlipAnalysis/file2"));
BufferedReader file1 = new BufferedReader(new FileReader("file1"));
/* File Creation */
File file = new File("Result");
/* Objects to write into the files */
PrintWriter outputResult = new PrintWriter(file);
/* Hash Map*/
Map<String,Integer> map = new HashMap<String, Integer>();
while ((lineFile2 = file2.readLine()) != null){
indexFile2++;
map.put(line,indexMe);
}
System.out.println("Map is ready. Now performing Matching.");
while (( lineFile1 = inputFile1.readLine()) != null){
indexFile1++;
if(map.containsKey(lineFile1)){
if(((indexFile - map.get(lineFile1))!= 0)
{
outputResult.println(lineFile1 + " Flipped by: " +(indexFile - map.get(lineFile1));
orderFlipped++;
}
else{
outputResult.println( lineFile1 + " Flipped by: 0" );
ordersNotFlipped++;
}
}
}
outputResult.println("\n\n--------------------------------------------------------------\n"+ "Total Order Flipped: " +ordersFlipped);
outputResult.println("\nOrder Not Flipped: " +ordersNotFlipped);
outputResult.println("\nTotal Number of Orders: " + (ordersFlipped + ordersNotFlipped));
System.out.println("Done!!");
inputFile2.close();
inputFile1.close();
outputResult.close();
}
}
答案 0 :(得分:0)
假设您拥有JDK 7或更高版本。
public static void main(String args[]) throws Exception {
System.out.println("Creating hashmap...");
HashMap<String, Integer> newOrderNumberPositionMap = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader("d:\\test\\file2.txt"), 4096)) {
String orderNumber;
int lineNo = 1;
while ((orderNumber = reader.readLine()) != null) {
newOrderNumberPositionMap.put(orderNumber, lineNo);
lineNo++;
}
}
System.out.println("Map is ready. Now performing matching and writing result...");
try (BufferedReader reader = new BufferedReader(new FileReader("d:\\test\\file1.txt"), 4096);
BufferedWriter writer = new BufferedWriter(new FileWriter("d:\\test\\result.txt"), 4096)) {
int ordersFlipped = 0;
String orderNumber;
int lineNo = 1;
while ((orderNumber = reader.readLine()) != null) {
int newLineNo = newOrderNumberPositionMap.get(orderNumber);
writer.write(orderNumber);
writer.write(" is flipped by ");
writer.write(String.valueOf(newLineNo - lineNo));
writer.newLine();
if ((newLineNo - lineNo) != 0) {
ordersFlipped++;
}
lineNo++;
}
writer.newLine();
writer.write("--------------------------------------------------------------");
writer.newLine();
writer.write("Orders Flipped: ");
writer.write(String.valueOf(ordersFlipped));
writer.newLine();
writer.write("Orders Not Flipped: ");
writer.write(String.valueOf(newOrderNumberPositionMap.size() - ordersFlipped));
writer.newLine();
writer.write("Total Number of Orders: ");
writer.write(String.valueOf(newOrderNumberPositionMap.size()));
writer.newLine();
}
System.out.println("Done.");
}