0

I've written an XML parser which parses an XML documents and returns me a Map of and ID and Name. For some reason it's skipping duplicates IDs.

Edit:

public static Multimap<String,String> getMap(String pathToFile) {

    Multimap<String,String> map = new ArrayListMultimap.create();

    try {

        Document doc = getDocument(pathToFile);
        NodeList nList = doc.getElementsByTagName("Definition");

        for(int i=0;i<nList.getLength();i++) {
            Node nNode = nList.item(i);
            if(nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                String nodeID = eElement.getElementsByTagName("Key").item(0).getTextContent(); 
                NodeList n = eElement.getElementsByTagName("Value");

                for(int j=0;j<n.getLength();j++) {
                    String name = n.item(0).getTextContent();

                    if(name.equalsIgnoreCase("")) {
                        name = "blank"; // check for empty values
                    }
                    map.put(nodeID, name);
                }
            }
        }
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
    return map;
}


public static List<String> getIDList(String pathToFile) {
    List<String> list = new ArrayList<String>();

    Multimap<String, String> map = getMap(pathToFile);

    for(String id : map.keySet()) {
        list.add(id);
    }
    return list;
}

My question is, why is this happening? Why duplicate is being ignored?

arabian_albert
  • 708
  • 1
  • 11
  • 24

1 Answers1

0

because, it's a Map: the key is unique: in a Map, if you put(ID,"AA"); and put (ID,"BB"); only ID-"BB" remains. It's the philosophy: a Map maps a key to an unique value.

So dont use a Map.

You can use some pair, in a Set, or Vector or List, like that:

Set< Pair < String,String>>, or List< Pair >, Vector< Pair< String,String>>

Or you can use MultiMap (map with a key and multiple values):

How to create a Multimap<K,V> from a Map<K, Collection<V>>?

example with Multimap:

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

Multimap<String,String> mm=ArrayListMultimap.create(); //

mm.put("AAA", "123");
mm.put("AAA", "444");
mm.put("AAA", "555");
mm.put("BBB", "777");

// to use keySet
mm.keySet();

// getting values
Collection<String> values=mm.get("AAA");

for (String a_value: values) System.out.println("VALUE:"+a_value);

if you want to use Pair, you have to recreate get, keySet, ...

Pair the_pair=new Pair(ID,Name);

courtesy of : A Java collection of value pairs? (tuples?)

public class Pair<L,R> implements java.io.Serializable  {

  private final L left;
  private final R right;

  public Pair(L left, R right) {
    this.left = left;
    this.right = right;
  }

  public L getLeft() { return left; }
  public R getRight() { return right; }

  @Override
  public int hashCode() { return left.hashCode() ^ right.hashCode(); }

  @Override
  public boolean equals(Object o) {
    if (o == null) return false;
    if (!(o instanceof Pair)) return false;
    Pair pairo = (Pair) o;
    return this.left.equals(pairo.getLeft()) &&
           this.right.equals(pairo.getRight());
  }

}
Community
  • 1
  • 1