Following is my code that returning false even if the key exists:
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class SequenceNumber {
public static int getSequenceNumber (String TcOrderId){
// Create a hash map to set key values pair.
Map<String, Integer> map = new HashMap<String, Integer>();
int i= 1;
// check if hashmap contains the key.
System.out.println("key present " +map.containsKey(TcOrderId));
if (map.containsKey(TcOrderId))
{
//Key Present
System.out.println("Inside IF ");
int value = map.get(TcOrderId);
System.out.println("value from the key " + value);
map.remove(value);
map.put(TcOrderId, value+1);
return map.get(TcOrderId);
}
else
{
//Key Not present
System.out.println("INSIDE ELSE ");
map.put(TcOrderId, i);
System.out.println("map "+ map);
return map.get(TcOrderId);
}
}
public static void main(String[] args) throws IOException {
String sCurrentLine;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("C:\\Users\\BongAn\\Desktop\\Package\\testing.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
{
while ((sCurrentLine = br.readLine()) != null) {
//String orderid = sCurrentLine.substring(0, 6);
System.out.println("reading line " +sCurrentLine);
int seqvalue = getSequenceNumber(sCurrentLine);
System.out.println("seqvalue "+seqvalue);
}
}
}
}
Input data in the file:
1233
1233
1234
The result should be
1
2
1
But everytime its going in the else loop and the result is
1
1
1
I am trying to use HASHMAP as I am creating my own index.
In your CODE everytime you call getSequenceNumber function - you create new HashMap. I believe this is not something you want.
To avoid that - you can simply move Map<String, Integer> map = new HashMap<String, Integer>(); into the body of class. Since the function getSequenceNumber is a static function - you will need to make the variable static. Hope this helps.
Snippet:
public class SequenceNumber {
// PUT STATIC VARIABLE HERE:
static Map<String, Integer> map = new HashMap<String, Integer>();
public static int getSequenceNumber (String TcOrderId){
// Create a hash map to set key values pair.
// (REMOVE) Map<String, Integer> map = new HashMap<String, Integer>();**
int i= 1;
// check if hashmap contains the key.
...
}
...
}
Another alternative
(perhaps better) would be to avoid static functions and variables and create an instance of SequenceNumber object. That way you could keep a couple of different instance numbers separately.
Simple snippet:
public class SequenceNumber {
// Your hashmap here:
Map<String, Integer> map = new HashMap<String, Integer>();
public int getSequenceNumber (String TcOrderId) {
// ...
}
public static void main(String[] args) throws IOException {
// Instance of SequenceNumber object:
SequenceNumber sequenceNumber = new SequenceNumber();
String sCurrentLine;
BufferedReader br = null;
// ...
while ((sCurrentLine = br.readLine()) != null) {
//String orderid = sCurrentLine.substring(0, 6);
System.out.println("reading line " +sCurrentLine);
int seqvalue = sequenceNumber.getSequenceNumber(sCurrentLine);
System.out.println("seqvalue "+seqvalue);
}
// ...
}
}
Something like this should work. Haven't tried running it though.
public class SequenceNumber {
public static int getSequenceNumber (String TcOrderId, Map<String, Integer> map){
if(!map.contains(TcOrderId)){
map.put(TcOrderId, 0);
}
map.put(TcOrderId, map.get(TcOrderId)+1);
return map.get(TcOrderId);
}
public static void main(String[] args) throws IOException {
String sCurrentLine;
BufferedReader br = null;
Map<String, Integer> map = new HashMap<String, Integer>();
try {
br = new BufferedReader(new FileReader("C:\\Users\\BongAn\\Desktop\\Package\\testing.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
{
while ((sCurrentLine = br.readLine()) != null) {
//String orderid = sCurrentLine.substring(0, 6);
System.out.println("reading line " +sCurrentLine);
int seqvalue = getSequenceNumber(sCurrentLine, map);
System.out.println("seqvalue "+seqvalue);
}
}
Related
Already done this but can't make it work.
Also tried to create another while ((line = br.readLine()) != null) {}, and placed the sort before it, but it won't read this while so it wouldnt print anithing.
The file looks like this:
1-Fred-18-5-0
2-luis-12-33-0
3-Helder-23-10-0
And wanted it to print like this:
2-luis-12-33-0
3-Helder-23-10-0
1-Fred-18-5-0
public static void lerRanking() throws IOException {
File ficheiro = new File("jogadores.txt");
BufferedReader br = new BufferedReader(new FileReader(ficheiro));
List<Integer> jGanhos = new ArrayList<Integer>();
int i = 0;
String line;
String texto = "";
while ((line = br.readLine()) != null) {
String[] col = line.split("-");
int colunas = Integer.parseInt(col[3]);
jGanhos.add(colunas);
i++;
if(i>=jGanhos.size()){
Collections.sort(jGanhos);
Collections.reverse(jGanhos);
for (int j = 0; j < jGanhos.size(); j++) {
if(colunas == jGanhos.get(i)){
texto = texto + line + "\n";
}
}
}
}
PL(texto);
}
Make it step by step:
public static void lerRanking() throws IOException {
File ficheiro = new File("jodagores.txt");
// read file
BufferedReader br = new BufferedReader(new FileReader(ficheiro));
List<String> lines = new ArrayList<>();
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
// sort lines
lines.sort(new Comparator<String>() {
#Override
public int compare(String s1, String s2) {
// sort by 3rd column descending
return Integer.parseInt(s2.split("-")[3]) - Integer.parseInt(s1.split("-")[3]);
}
});
// concat lines
String texto = "";
for (String l : lines) {
texto += l + "\n";
}
System.out.println(texto);
// PL(texto);
}
Okay so first of all I thounk you should introduce a Java class (in my code this is ParsedObject) to manage your objects.
Second it should implement the Comparable<ParsedObject> interface, so you can easily sort it from anywhere in the code (without passing a custom comparator each time).
Here is the full code:
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
lerRanking();
}
public static void lerRanking() throws IOException {
File ficheiro = new File("jodagores.txt");
// read lines to a list
List<String> lines = readLines(ficheiro);
// parse them to a list of objects
List<ParsedObject> objects = ParsedObject.from(lines);
// sort
Collections.sort(objects);
// print the output
writeLines(objects);
}
public static List<String> readLines(File ficheiro) throws IOException {
// read file line by line
BufferedReader br = new BufferedReader(new FileReader(ficheiro));
List<String> lines = new ArrayList<>();
String line;
while((line = br.readLine()) != null) {
lines.add(line);
}
br.close(); // THIS IS IMPORTANT never forget to close a Reader :)
return lines;
}
private static void writeLines(List<ParsedObject> objects) throws IOException {
File file = new File("output.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
for(ParsedObject object : objects) {
// print the output line by line
bw.write(object.originalLine);
}
bw.flush();
bw.close(); // THIS IS IMPORTANT never forget to close a Writer :)
}
// our object that holds the information
static class ParsedObject implements Comparable<ParsedObject> {
// the original line, if needed
public String originalLine;
// the columns
public Integer firstNumber;
public String firstString;
public Integer secondNumber;
public Integer thirdNumber;
public Integer fourthNumber;
// parse line by line
public static List<ParsedObject> from(List<String> lines) {
List<ParsedObject> objects = new ArrayList<>();
for(String line : lines) {
objects.add(ParsedObject.from(line));
}
return objects;
}
// parse one line
public static ParsedObject from(String line) {
String[] splitLine = line.split("-");
ParsedObject parsedObject = new ParsedObject();
parsedObject.originalLine = line + "\n";
parsedObject.firstNumber = Integer.valueOf(splitLine[0]);
parsedObject.firstString = splitLine[1];
parsedObject.secondNumber = Integer.valueOf(splitLine[2]);
parsedObject.thirdNumber = Integer.valueOf(splitLine[3]);
parsedObject.fourthNumber = Integer.valueOf(splitLine[4]);
return parsedObject;
}
#Override
public int compareTo(ParsedObject other) {
return other.thirdNumber.compareTo(this.thirdNumber);
}
}
}
If you have any more question feel free to ask :) An here is an the example objects list after parsing and sorting.
The easiest way is to first create a class that will hold the data from your file provided your lines keep the same format
public class MyClass {
private Integer column1;
private String column2;
private Integer column3;
private Integer column4;
private Integer column5;
public MyClass(String data) {
String[] cols = data.split("-");
if (cols.length != 5) return;
column1 = Integer.parseInt(cols[0]);
column2 = cols[1];
column3 = Integer.parseInt(cols[2]);
column4 = Integer.parseInt(cols[3]);
column5 = Integer.parseInt(cols[4]);
}
public synchronized final Integer getColumn1() {
return column1;
}
public synchronized final String getColumn2() {
return column2;
}
public synchronized final Integer getColumn3() {
return column3;
}
public synchronized final Integer getColumn4() {
return column4;
}
public synchronized final Integer getColumn5() {
return column5;
}
#Override
public String toString() {
return String.format("%d-%s-%d-%d-%d", column1, column2, column3, column4, column5);
}
}
Next you can get a list of your items like this:
public static List<MyClass> getLerRanking() throws IOException {
List<MyClass> items = Files.readAllLines(Paths.get("jogadores.txt"))
.stream()
.filter(line -> !line.trim().isEmpty())
.map(data -> new MyClass(data.trim()))
.filter(data -> data.getColumn4() != null)
.sorted((o1, o2) -> o2.getColumn4().compareTo(o1.getColumn4()))
.collect(Collectors.toList());
return items;
}
This will read your whole file, filter out any blank lines, then parse the data and convert it to MyClass.
It will then make sure that column4 isn't null in the converted objects.
Finally it will reverse sort the objects based off from the value in column 4 and create a list of those items.
To print the results you can do something like this
public static void main(String[] args) {
List<MyClass> rankingList = getLerRanking();
rankingList.forEach(item -> System.out.println(item));
}
Since we overrode the toString() method, it will print it out the object as it is displayed in the file.
Hope this helps.
I need to read two text files and display all the unique words in both the text files.(the words in both 2 files can only be printed once)
file1.txt
lion
tiger
cheetah
elephant
cow
file2.txt
mouse
dog
cow
cat
lion
expected output :
lion
tiger
cheetah
elephant
cow
dog
cat
mouse
public class Workshop {
static int count1 = 0;
static int count2 = 0;
private static final String FILE1 = "C:\\Users\\shagi\\Desktop\\file1.txt";
private static final String FILE2 = "C:\\Users\\shagi\\Desktop\\file2.txt";
static String arrayLines1[] = new String[countLines(FILE1)];
static String arrayLines2[] = new String[countLines(FILE2)];
static String totalArray[] = new String[arrayLines1.length + arrayLines2.length];
static String arrayLines1new[]=new String[countLines(FILE1)];
static int flag = 0;
static int k=arrayLines1.length;
public static void main(String[] args) throws IOException {
readFile(FILE1, FILE2);
displaySimilar();
displayAll();
}
public static int countLines(String File) {
int lineCount = 0;
try {
BufferedReader br = new BufferedReader(new FileReader(File));
while ((br.readLine()) != null) {
lineCount++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lineCount;
}
public static void readFile(String File1, String File2) {
String contents1 = null;
String contents2 = null;
try {
FileReader file1 = new FileReader(File1);
FileReader file2 = new FileReader(File2);
BufferedReader buf1 = new BufferedReader(file1);
BufferedReader buf2 = new BufferedReader(file2);
while ((contents1 = buf1.readLine()) != null) {
arrayLines1[count1] = contents1;
count1++;
}
while ((contents2 = buf2.readLine()) != null) {
arrayLines2[count2] = contents2;
count2++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
There are two methods which i tried to find the ans for my question
Method 1
public static void displayAll() {
for (int i =0; i<k-1;i++){
System.out.println(totalArray[i]);
}
System.out.println(totalArray[k-1]);
System.out.println("");
int p=0;
for (int i=0;i<arrayLines2.length;i++){
for (int j=0;j<arrayLines1.length;j++){
if (arrayLines2[i].equals(arrayLines1[j])){
flag=1;
break;
} else {
flag=0;
}
if (flag==1){
arrayLines1new[p]=arrayLines2[i];
p++;
}
}
}
Method 2
public static void displayAll() {
for (int i=0;i<arrayLines1.length;i++){
String a=arrayLines1[i];
for (int j=0;j<arrayLines2.length;j++){
String b =arrayLines2[j];
if (!a.equals(b)){
System.out.println(a);
}
}
}
}
But both doesnt give the expected output
There is lot of redundant code. Here is a simpler and shorter version.
I am using Set and its operations to find common (intersection), uncommon and all unique words.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class Workshop {
private static final String FILE1 = "C:\\Users\\shagi\\Desktop\\file1.txt";
private static final String FILE2 = "C:\\Users\\shagi\\Desktop\\file2.txt";
static Set<String> file1Words = new HashSet<String>();
static Set<String> file2Words = new HashSet<String>();
static Set<String> allWords = new HashSet<String>();
static Set<String> commonWords = new HashSet<String>();
static Set<String> uncommonWords = new HashSet<String>();
public static void main(String[] args) throws IOException {
file1Words.addAll(readFile(FILE1));
file2Words.addAll(readFile(FILE2));
System.out.println("file1 : " + file1Words);
System.out.println("file2 : " + file2Words);
displaySimilar();
System.out.println("common : " + commonWords);
displayAll();
System.out.println("all : " + allWords);
displayUnCommon();
System.out.println("uncommon : " + uncommonWords);
}
public static void displaySimilar() {
commonWords.addAll(file1Words);
commonWords.retainAll(file2Words);
}
public static void displayUnCommon() {
uncommonWords.addAll(file1Words);
uncommonWords.addAll(file2Words);
uncommonWords.removeAll(commonWords);
}
public static Set<String> readFile(String file) {
Set<String> words = new HashSet<String>();
try {
FileReader fileReader = new FileReader(file);
BufferedReader buffer = new BufferedReader(fileReader);
String content = null;
while ((content = buffer.readLine()) != null) {
words.add(content);
}
} catch (Exception e) {
e.printStackTrace();
}
return words;
}
public static void displayAll() {
allWords.addAll(file1Words);
allWords.addAll(file2Words);
}
}
Sample Run:
file1 : [lion, cheetah, tiger, elephant, cow]
file2 : [lion, mouse, cat, cow, dog]
common : [lion, cow]
all : [cheetah, lion, cat, mouse, tiger, elephant, cow, dog]
uncommon : [cheetah, cat, mouse, tiger, elephant, dog]
This would be a good situation for a HashMap. The keys would be the words and the values would be the number of occurrences. You could then print out the keys with a value of 1. The pseudo code would look like this:
Initialize the map: HashMap <String, Integer> wordMap = new HashMap<>();
For each file:
-- For each word:
---- Put the word in wordMap with the appropriate value.
For each key in wordMap:
-- If wordMap.get(key) == 1, print out the key
You could also accomplish the same thing using two arrayLists, using one to keep track out the words and another to keep track of the counts.
Both methods have an O(N) time complexity, but using the map is more performant because the maps's values can be updated in O(1).
I'm attempting to parse a CSV file into a HashMap. The CSV file contains name, email and age.
I've attempted it below but haven't had any luck progressing with it - beginner to Java
public class Extract {
public HashMap<String, Map<String, Integer>> readFile(String filename) {
HashMap<String, Map<String, Integer>> people = new HashMap<>();
try {
Scanner in = new Scanner(new File(filename));
String line;
while(in.hasNext()) {
line = in.nextLine();
String[] keyValue = line.split(",");
people.put(keyValue[0], keyValue[2], keyValue[3]);
}
in.close();
}
catch(Exception e) {
e.printStackTrace();
}
return people;
}
}
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Extract e = new Extract();
String peopleFile = ("relationships.csv");
HashMap<String, String> person1 = e.readFile(peopleFile);
person1.get("Bob");
}
}
So here is the code as it should suit you. Try to understand what happens here. It seems that you're still struggling a little when it comes to Maps and how to use them properly.
public class Extract {
public HashMap<String, HashMap<String, String>> readFile(String filename) {
HashMap<String, HashMap<String, String>> people = new HashMap<>();
HashMap<String, String> metaData = new HashMap<>();
try {
Scanner in = new Scanner(new File(filename));
String line;
while(in.hasNext()) {
line = in.nextLine();
String[] keyValue = line.split(",");
// 1st put email address and age in the inner Map
metaData = new HashMap<>();
metaData.put("email", keyValue[1]);
metaData.put("age", keyValue[2]);
// 2nd put the inner Map into the outer Map, referenced by the person's name
people.put(keyValue[0], metaData);
}
in.close();
}
catch(Exception e) {
e.printStackTrace();
}
return people;
}
}
public class Main {
public static void main(String[] args) {
Extract e = new Extract();
String peopleFile = ("relationships.csv");
HashMap<String, HashMap<String, String>> person1 = e.readFile(peopleFile);
// 1st: get the inner Map of Bob
// If you use the get()-method, you tell the outer Map to hand you the inner Map of Bob.
HashMap<String, String> metaData = person1.get("Bob");
// 2nd: get the metaData off the inner Map
String email = metaData.get("email");
Integer age = Integer.valueOf(metaData.get("age"));
}
}
This is my first question here so please bear with me. I have a text file that looks like something like this;
userId,itemId,rating
1,101,2.5
4,103,3.0
1,103,3.0
6,104,3.5
2,101,3.0
4,106,4.5
2,103,1.5
5,102,4.0
2,105,3.5
7,106,3.0
3,101,2.5
3,102,3.0
What I'am trying to accomplish is to read the file line per line and seperated each value. After that is done place all unique userIds in the key section of this map; Map<Integer, Map<Integer, Double>>. And place the itemIds with the rating in the second map corresponding to the key of the first map. Can you still follow me?? Here is the code I produced so far with not the outcome I want.
This is my User class:
package com.example;
import java.util.HashMap;
import java.util.Map;
public class User {
private int mUserId;
private Map<Integer, Double> mRatedItems;
public User(int userId) {
mUserId = userId;
mRatedItems = new HashMap<>();
}
public int getUserId() {
return mUserId;
}
public void setUserId(int userId) {
mUserId = userId;
}
public Map<Integer, Double> getRatedItems() {
return mRatedItems;
}
public void addRatedItems(int itemId, double rating) {
mRatedItems.put(itemId, rating);
}
}
This is the file I need to modify:
package com.example;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class TextFile {
List<User> mUserIdList;
private User mUser;
public Map<Integer, Map<Integer, Double>> readFile(String fileName) {
mUserIdList = new ArrayList<>();
Map<Integer, Map<Integer, Double>> userItemData = new HashMap<>();
try (
FileInputStream fis = new FileInputStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis))
) {
String line;
while ((line = reader.readLine()) != null) {
String[] args = line.split(",");
int userId = Integer.parseInt(args[0]);
int itemId = Integer.parseInt(args[1]);
double rating = Double.parseDouble(args[2]);
if (!mUserIdList.contains(userId)) {
mUser = new User(userId);
mUserIdList.add(mUser);
}
if (!mUserIdList.isEmpty()) {
addItemRatings(userId, itemId, rating);
}
if (!checkDuplicates()) {
userItemData.put(mUser.getUserId(), mUser.getRatedItems());
}
}
} catch (IOException e) {
System.out.printf("Problem loading: %s %n", fileName);
e.printStackTrace();
}
return userItemData;
}
private boolean checkDuplicates() {
Set<Integer> set = new HashSet<>();
for (User user : mUserIdList) {
if (!set.add(user.getUserId())) {
return true;
}
}
return false;
}
public void addItemRatings(int userId, int itemId, double rating) {
for (User user : mUserIdList) {
if (user.getUserId() == userId) {
user.addRatedItems(itemId, rating);
}
}
}
public int showTextLines(String fileName) {
int textLines = 0;
try (
FileInputStream fis = new FileInputStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis))
) {
while (reader.readLine() != null) {
textLines++;
}
reader.close();
} catch (IOException e) {
System.out.printf("Problem showing total lines from: %s %n", fileName);
e.printStackTrace();
}
return textLines;
}
}
This is my main class:
package com.example;
import java.util.Map;
public class Main {
public static void main(String[] args) {
TextFile textFile = new TextFile();
String location = "D:\\IdeaProjects\\KMeans\\src\\com\\quincy\\userItem1.data";
Map<Integer, Map<Integer, Double>> readFile = textFile.readFile(location);
int textLines = textFile.showTextLines(location);
System.out.printf("File: %s %n", readFile);
System.out.printf("Number of lines in file: %s %n", textLines);
}
}
And my output is:
File: {1={101=2.5, 103=3.0}, 4={103=3.0, 106=4.5}}
Number of lines in file: 12
My code is only adding the first two userIds with their corresponding itemIds and rating. Can somebody please explain to me what I' am doing wrong?? And maybe show how I can improve this code to work properly??
Thanks in advance.
The problem is in your checkDuplicates() method. I don't know what exactly you're trying to achieve on that function but if you want to check for duplications. then, I suggest you just use the function containsKey() like this.
if (!userItemData.containsKey(mUser.getUserId())) {
userItemData.put(mUser.getUserId(), mUser.getRatedItems());
}
I am attempting to do the following (in psuedocode):
Generate HashMapOne that will be populated by results
found in a DICOM file (the Key was manipulated for matching
purposes).
Generate a second HashMapTwo that will be read from a
text document.
Compare the Keys of both HashMaps, if a match add the results of
the value of HashMapOne in a new HashMapThree.
I am getting stuck with adding the matched key's value to the HashMapThree. It always populates a null value despite me declaring this a public static variable. Can anyone please tell me why this may be? Here is the code snippets below:
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);
}
}
}
This line of code looks very wrong
Result.put(entry.getKey(), dicomFile.get(element.toString()));
If you are trying to copy the key/value pair from HashMapOne, then this is not correct.
The value for each key added to Result will be null, because you are calling get method on Map interface on dicomFile. get requires a key as a lookup value, and you are passing in
element.toString()
where element will be the last element that was read from your file.
I think you should be using
Result.put(entry.getKey(), entry.getValue()));