1

値を含む文字列があります:

12345    5
54321    4
98765    10

最初の値は数値、2 番目の値はカウントです。文字列値は、次のコードによって取得されます。

for(ClusterListBean bean : clusterList) {
    line += bean.getMSISDN()+"\t"+bean.getRewardCount()+"\n";               
}

現在、同じ内容でカウント値が異なるファイルを読み取っています。

これは、次の方法で実現されます。

BufferedReader ln = FileCreatorUtil.readFile(configFileUtil.prevFile().getFileDir()+prevFile.clusterPrevFile().getFileName());

ここでやりたいことは、数値を検索し、それとペアになっているカウントを取得し、そのカウントを減算することです。例:

BufferedReader の内容:

12345    5
54321    4
98765    10

文字列行の内容:

12345    7
54321    9
98765    15

出力は次のようになります。

12345    2
54321    5
98765    5
4

2 に答える 2

3

HashMapMSISDN がキーで、カウントが値である場所にデータを入れます。2 番目のファイルを読み取ると、そのマップが参照され、値が減算されます。

于 2012-07-20T12:04:56.220 に答える
1

「文字列」をハッシュマップに入れてみませんか?

Map<String,Integer> map = new HashMap<String,Integer>();
for(ClusterListBean bean : clusterList) {
    map.put(bean.getMSISDN(),bean.getRewardCount());               
}

次に、ファイルを読み取ります。

BufferedRead ln = null;
try{
    ln = new BufferedReader(new FileReader(configFileUtil.prevFile().getFileDir()+prevFile.clusterPrevFile().getFileName()));
    String line;
    while((line=br.readLine())!=null){
        String[] linesplit = line.split("\\t");
        if (map.containsKey(linesplit[0])){
             //do whatever you need with something like:
             System.out.println(map.get(linesplit[0])-Integer.parseInt(linesplit[1]));
        }
    }
    ln.close();
}catch(IOException e){
     e.printStackTrace();
}
于 2012-07-20T12:11:05.037 に答える