1

我有 3 个向量,如下所示。这里 Vector1、Vector2、Vector3 的大小不同。下面给出的是一种理想情况,只会发生一段时间。vector2 可能只包含一个额外的 {},d=3 ,... 和 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}]

我实现了一个逻辑,但它很耗时。有没有人有更好的选择??????

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 回答
0

Hashtable不会保持您添加元素的顺序,因此如果您想存档您将不得不使用LinkedHashTable.

考虑到之前的评论,您可以像这样合并向量:

public static void main(String[] args) {
    Vector<Hashtable<Character, String>> 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);

    Vector<Hashtable<Character, String>> 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);

    Vector<Hashtable<Character, String>> 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);

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

    // Considering every vector has the same size
    for (int i = 0; i < vector1.size(); i++) {
        mergedVector.add(vector1.get(i));
        mergedVector.add(vector2.get(i));
        mergedVector.add(vector3.get(i));
    }

    // 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");
    }
}
于 2013-06-27T08:26:19.870 回答