0

上記のサンプルのように、3 列に整数があり、最初の列に各数値の複数のエントリがあるテキスト ファイルがあります。

1 23 1
1 24 2
1 57 1
1 10 1
1 59 2
1 31 2
1 38 1
1 11 1
2 39 2
2 40 1
2 15 1
2 74 2

上記のコードでファイルの内容を読み取ることができます

public static void main(String[] args) {

    try (BufferedReader br = new BufferedReader(new FileReader("/home/user/Desktop/file.txt")))
        {
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }
        } 
    catch (IOException e) {
                e.printStackTrace();
        } }

しかし、ハッシュマップを使用して、最初の列のすべての異なる番号のすべてのエントリを次のように 1 行に結合したいと考えています。

1 23 1 24 2 57 1 10 1 59 2 31 2 38 1
2 39 2 40 1 15 1 74 2

何か提案はありますか?ありがとう!

4

3 に答える 3

1

開始するための基本的なコードを次に示します。これは、エントリをソートする TreeMap を使用します。HashMapの宣伝文句を読んで理由を確認してください。これは、HashMap がエントリの順序付けを保証しないことを示しています。これには HashMap を使用できますが、どこかで追加の手順が必要になる可能性があるため、お勧めしません。

TreeMap<Integer, ArrayList<Integer[]>> rowMap = new TreeMap<Integer, ArrayList<Integer[]>>();

// when you have your new line

String[] cols = sCurrentLine.split(" ");

if (cols.length < 1) {
    // handle error if there is not a key column
}

try {
    int colKey = Integer.parseInt(cols[0]);

    Integer[] colValues = new Integer[cols.length - 1];

    for (int i = 1; i < cols.length; i++) {
        colValues[i - 1] = Integer.parseInt(cols[i]);
    }

    if (!rowMap.containsKey(colKey)) {

        // add a new entry for this column number
        rowMap.put(colKey, new ArrayList<Integer[]>());
    }

    rowMap.get(colKey).add(colValues);

} catch (NumberFormatException e) {
    // handle error if any columns don't have integers
}

これで、列 1 の番号でグループ化されたすべてののリストを含む TreeMap ができました。

サンプル ドキュメントでマップ全体を印刷するには、次の操作を実行できます。

// iterate by column 1 number in numerical order
for (Integer key : rowMap.keySet()) {

    // iterate by rows in this column in the order they were added
    for (Integer[] row : rowMap.get(key)) {
        String printout = key + ":";

        // iterate by columns in this row
        for (Integer col : row) {
            printout += " " + col;
        }

        System.out.println(printout);
    }
}

これにより、次のように出力されます。

1: 23 1
1: 24 2
1: 57 1
1: 10 1
1: 59 2
1: 31 2
1: 38 1
1: 11 1
2: 39 2
2: 40 1
2: 15 1
2: 74 2

これにより、列 1 の値に基づいてドキュメントが並べ替えられることがわかります。OP で示した形式で印刷することもできます。ループを少し変更して、行が同じ行に追加されるようにするだけです。

この方法で TreeMap を使用する場合の注意事項:

  • 列 1 の数値は、任意の整数にすることができます。
  • 列 1 の数字は任意の順序で指定できます。
  • 行には、最初の列を超えて任意の数の列を含めることができます。

ただし、マップを使用してそれらをリストすると、常に列 1 の番号でグループ化され、常に番号順に並べられます。

行をソートしたままにすることを気にしない場合は、多少単純化できます。その場合、各行を連結するだけであれば、TreeMap<Integer, ArrayList<Integer>>またはTreeMap<Integer, ArrayList<String>>またはを作成できます。TreeMap<Integer, String>

于 2013-11-08T19:56:37.447 に答える
0

以下は、私にとってよりうまく機能したコードで、最初の列の異なる数値ごとに集計されたすべての数値を示しています。解決策を投稿していただきありがとうございます。これについて何らかの変更を提案する場合は、お気軽にお寄せください。

public static void main(String[] args) {

    String sCurrentLine;
    TreeMap<Integer, ArrayList<Integer[]>> rowMap = new TreeMap<Integer, ArrayList<Integer[]>>();
    try (BufferedReader br = new BufferedReader(new FileReader("/home/user/Desktop/file.txt")))
        {
            while ((sCurrentLine = br.readLine()) != null) {

                String[] cols = sCurrentLine.split(" ");

                if (cols.length < 1) {
                }

                try {
                    int colKey = Integer.parseInt(cols[0]);

                    Integer[] colValues = new Integer[cols.length - 1];

                    for (int i = 1; i < cols.length; i++) {
                        colValues[i - 1] = Integer.parseInt(cols[i]);
                    }

                    if (!rowMap.containsKey(colKey)) {
                        rowMap.put(colKey, new ArrayList<Integer[]>());
                    }
                    rowMap.get(colKey).add(colValues);
                } catch (NumberFormatException e) {
                }
                for (Integer key : rowMap.keySet()) {
                    String row = key + ""; 
                    for (Integer[] rows : rowMap.get(key)) {
                        for (Integer col : rows) {
                            row += " " + col;
                        }
                    }
                    System.out.println(row);
                }
    }
    catch (IOException e) {
            e.printStackTrace();
    }
}
于 2013-11-09T09:31:42.030 に答える