匹配HashMap中的键

时间:2015-03-17 19:57:29

标签: java dicom

我正在尝试执行以下操作(在伪代码中):

  • 生成将由结果填充的HashMapOne 在DICOM文件中找到(Key被操纵以进行匹配 目的)。
  • 生成第二个HashMapTwo,它将从a中读取 文本文件。
  • 比较两个HashMaps的键,如果匹配添加结果 HashMapOne在新的HashMapThree中的值。

我很难将匹配键的值添加到HashMapThree中。尽管我声明这是一个公共静态变量,但它总是填充空值。有人可以告诉我为什么会这样吗?以下是代码段:

public class viewDICOMTags {

    HashMap<String,String> dicomFile = new HashMap<String,String>(); 
    HashMap<String,String> dicomTagList = new HashMap<String,String>(); 
    HashMap<String,String> Result = new HashMap<String, String>();
    Iterator<org.dcm4che2.data.DicomElement> iter = null;
    DicomObject working;
    public static DicomElement element;
    DicomElement elementTwo;
    public static String result;
    File dicomList = new File("C:\\Users\\Ryan\\dicomTagList.txt");

    public void readDICOMObject(String path) throws IOException
    {
        DicomInputStream din = null;
        din = new DicomInputStream(new File(path));
        try {
            working = din.readDicomObject();
            iter = working.iterator();
            while (iter.hasNext())
            {
                element = iter.next();
                result = element.toString();
                String s = element.toString().substring(0, Math.min(element.toString().length(), 11));
                dicomFile.put(String.valueOf(s.toString()), element.vr().toString());
            }   
            System.out.println("Collected tags, VR Code, and Description from DICOM file....");
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return;
        }
        finally {
            try {
                din.close();
            }
            catch (IOException ignore){
            }

        }
        readFromTextFile();
    }
    public void readFromTextFile() throws IOException
    {
        try
        {
            String dicomData = "DICOM";
            String line = null;
            BufferedReader bReader = new BufferedReader(new FileReader(dicomList));
            while((line = bReader.readLine()) != null)
            {
                dicomTagList.put(line.toString(), dicomData);
            }
            System.out.println("Reading Tags from Text File....");
            bReader.close();
        }
        catch(FileNotFoundException e)
        {
            System.err.print(e);
        }
        catch(IOException i)
        {
            System.err.print(i);
        }   
        compareDICOMSets();
    }
    public void compareDICOMSets() throws IOException
    {
        for (Entry<String, String> entry : dicomFile.entrySet())
        {

            if(dicomTagList.containsKey(entry.getKey()))
                Result.put(entry.getKey(), dicomFile.get(element.toString()));
            System.out.println(dicomFile.get(element.toString()));
        }
        SortedSet<String> keys = new TreeSet<String>(Result.keySet());
        for (String key : keys) { 
               String value = Result.get(key);
               System.out.println(key);
        }

    }
}

1 个答案:

答案 0 :(得分:2)

这行代码看起来非常错误

 Result.put(entry.getKey(), dicomFile.get(element.toString()));

如果您尝试从HashMapOne复制键/值对,那么这是不正确的。

添加到Result的每个键的值将为null,因为您在dicomFile上的get接口上调用Map方法。 get需要一个密钥作为查找值,而您正在传入

element.toString()

其中element将是从文件中读取的最后一个元素。

我认为你应该使用

Result.put(entry.getKey(), entry.getValue()));