匹配 HashMap 中的键

Matching Keys in a HashMap

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

我无法将匹配键的值添加到 HashMapThree。尽管我将其声明为 public 静态变量,但它始终填充空值。谁能告诉我为什么会这样?以下是代码片段:

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);
        }

    }
}

这行代码看起来很不对

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

如果您试图从 HashMapOne 复制 key/value 对,那么这是不正确的。

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

element.toString()

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

我认为你应该使用

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