1

以下に示す 3 つのベクトルがあります。ここで、Vector1、Vector2、Vector3 は同じサイズではありません。以下に示すのは、しばらくの間だけ発生する理想的な状況です。vector2 には、d=3 の余分な {} が 1 つしか含まれていない可能性があります。 、...そして vector1 と Vector3 にはその (d=3) エントリがありません。

Vector1 := [{a=Prity, b=Joshi, c=Pyarelal, d=1},{a=tiny, b=darji, c=Mohandas, d=2}]  
Vector2 := [{e=age-29, f=height-5, d=1},{e=age-52, f=height-6, d=2}]        
Vector3 := [{g=pet-dog, d=1},{g=pet-cat, d=2}]

以下に示すように、値をマージして最終的なベクトルを提供するベクトルが必要です

Vector4 := [{a=Prity, b=Joshi, c=Pyarelal, d=1,e=age-29, f=height-5, g=pet-dog}, {a=tiny, b=darji, c=Mohandas, d=2, e=age-52, f=height-6, g=pet-cat}]

私は1つのロジックを実装しましたが、時間がかかります.誰かがより良いオプションを持っていますか?????

int columnSize = Vector1.size() > Vector2.size() ? Vector1.size()
        : Vector2.size();
Hashtable finalHash[] = new Hashtable[columnSize];
for (i = 0; i < Vector1.size(); i++) {
    finalHash[i] = (Hashtable) Vector1.elementAt(i);
    for (int z = 0; z < Vector2.size(); z++) {
        Hashtable hashtwo = (Hashtable) Vector2.elementAt(z);
        if (hashtwo.containsValue(finalHash[i]
                .get("TQM_QUOTE_INCEPTION_DATE"))) {
            finalHash[i].putAll(hashtwo);
            Vector2.removeElementAt(z);
        }
    }
    for (int z = 0; z < Vector3.size(); z++) {
        Hashtable hashduerenew = (Hashtable) Vector3.elementAt(z);
        if (hashduerenew.containsValue(finalHash[i]
                .get("TQM_QUOTE_INCEPTION_DATE"))) {
            finalHash[i].putAll(hashduerenew);
            Vector3.removeElementAt(z);
        }
    }
}
columnSize = Vector2.size() > Vector3.size() ? Vector2.size() : Vector3
        .size();
Hashtable finalHashtable = new Hashtable();
for (int t = 0; t < Vector2.size(); t++, i++) {
    finalHashtable = (Hashtable) Vector2.elementAt(t);
    for (int z = 0; z < Vector3.size(); z++) {
        Hashtable hashtwo = (Hashtable) Vector3.elementAt(z);
        if (hashtwo.containsValue(finalHashtable
                .get("TQM_QUOTE_INCEPTION_DATE"))) {
            finalHash[i].putAll(hashtwo);
            Vector3.removeElementAt(z);
            break;
        }
    }
    finalHash[i].putAll(finalHashtable);
    Vector2.removeElementAt(t);
}
int t = 0;
while (t < Vector3.size()) {
    finalHash[i] = (Hashtable) Vector3.elementAt(t);
    t++;
    i++;
}
4

2 に答える 2

0

彼は次のようなことを望んでいたと思います。

private static Vector<Hashtable<Character, String>> vector1;
private static Vector<Hashtable<Character, String>> vector2;
private static Vector<Hashtable<Character, String>> vector3;

public static void main(final String[] args) {
    setUp();

    // Your final vector
    Vector<Hashtable<Character, String>> mergedVector = new Vector<Hashtable<Character, String>>();

    // Considering every vector has the same size
    addToMerged(mergedVector, vector1);
    addToMerged(mergedVector, vector2);
    addToMerged(mergedVector, vector3);

    // Print the result
    for (Hashtable<Character, String> hash : mergedVector) {
        for (Entry<Character, String> set : hash.entrySet()) {
            System.out.println("KEY: " + set.getKey() + ", VALUE: "
                + set.getValue());
        }

        System.out.println("\nNext hashtable.\n");
    }
}

private static void setUp() {
    vector1 = new Vector<Hashtable<Character, String>>();
    Hashtable<Character, String> hash11 = new Hashtable<Character, String>();
    hash11.put('a', "Prity");
    hash11.put('b', "Joshi");
    hash11.put('c', "Pyarelal");
    hash11.put('d', "1");
    vector1.add(hash11);

    Hashtable<Character, String> hash12 = new Hashtable<Character, String>();
    hash12.put('a', "tiny");
    hash12.put('b', "darji");
    hash12.put('c', "Mohandas");
    hash12.put('d', "2");
    vector1.add(hash12);

    vector2 = new Vector<Hashtable<Character, String>>();
    Hashtable<Character, String> hash21 = new Hashtable<Character, String>();
    hash21.put('e', "age-29");
    hash21.put('f', "height-5");
    hash21.put('d', "1");
    vector2.add(hash21);

    Hashtable<Character, String> hash22 = new Hashtable<Character, String>();
    hash22.put('e', "age-52");
    hash22.put('f', "height-6");
    hash22.put('d', "2");
    vector2.add(hash22);

    vector3 = new Vector<Hashtable<Character, String>>();
    Hashtable<Character, String> hash31 = new Hashtable<Character, String>();
    hash31.put('g', "pet-dog");
    hash31.put('d', "1");
    vector3.add(hash31);

    Hashtable<Character, String> hash32 = new Hashtable<Character, String>();
    hash32.put('g', "pet-cat");
    hash32.put('d', "2");
    vector3.add(hash32);
}

private static void addToMerged(
    final Vector<Hashtable<Character, String>> mergedVector,
    final Vector<Hashtable<Character, String>> vector) {
    for (int i = 0; i < vector.size(); i++) {
        Hashtable<Character, String> hashtable = vector.get(i);
        for (Entry<Character, String> entrySet : hashtable.entrySet()) {
    boolean added = false;
    for (int j = 0; j < mergedVector.size() && !added; j++) {
        Hashtable<Character, String> hashtable2 = mergedVector
            .get(j);
        if (!hashtable2.containsKey(entrySet.getKey())) {
            hashtable2.put(entrySet.getKey(), entrySet.getValue());
            added = true;
            break;
        } else if (hashtable2.get(entrySet.getKey()).equals(
            entrySet.getValue())) {
            added = true;
            break;
        }
    }
    if (!added) {
        Hashtable<Character, String> hashtable2 = new Hashtable<>();
        hashtable2.put(entrySet.getKey(), entrySet.getValue());
            mergedVector.add(hashtable2);
    }
    }
}
}
于 2013-06-27T08:48:21.973 に答える