0

I am wondering if I can access the values I get after iterating the hashmap in the following code (I know they are stored in map but want to use them outside loop). The key and corresponding values are iterated inside a loop. Can concurrent hashmap help me to get the values and use them outside the loop.

Thank You.

     public static void main(String[] args) {
    Map<String, List<String>> maleMap = new LinkedHashMap<String,  List<String>>();
Map<String, List<String>> femaleMap = new LinkedHashMap<String, List<String>>();
    try {

Scanner scanner = new Scanner(new FileReader(.txt));

        while (scanner.hasNextLine()) {
            String nextLine = scanner.nextLine();
            String[] column = nextLine.split(":");
            if (column[0].equals("male") && (column.length == 4)) {
                maleMap.put(column[1],
                Arrays.asList(column[2], column[3]));
        } else if (column[0].equals("female") && (column.length == 4)) {
        femaleMap.put(column[1],
                    Arrays.asList(column[2], column[3]));
            }
        }
        Set<Entry<String, List<String>>> entries = maleMap.entrySet();
        Iterator<Entry<String, List<String>>> entryIter = entries
                .iterator();
        while (entryIter.hasNext()) {
            Map.Entry entry = (Map.Entry) entryIter.next();
            Object key = entry.getKey(); // Get the key from the entry.

            List<String> value = (List<String>) entry.getValue();
            Object value1 = " ";
            Object value2 = " ";
            int counter = 0;
            for (Object listItem : (List) value) {
                Writer writer = null;
                Object Name = key;
                Object Age = null;
                Object ID = null;
                if (counter == 0) {// first pass assign value to value1
                    value1 = listItem;
                    counter++;// increment for next pass
                } else if (counter == 1) {// second pass assign value to
                                            // value2
                    value2 = listItem;
                    counter++;// so we dont keep re-assigning listItem for
                                // further iterations
                }
            }
            System.out.println(key + ":" + value1 + "," + value2);
            scanner.close();
            Writer writer = null;
            Object Name = key;
            Object Age = value1;
            Object ID = value2;

            try {
                String filename = ".txt";
            FileWriter fw = new FileWriter(filename, true); 

 fw.write("# Table" +  Name + "\n" + "map:"+ Name + " a d2rq:ClassMap;" + "\n"
 + "    dataStorage map:database;" + "\n"+ "Pattern " +"\""+ Name + "/@@"+ Age +    
 "." + ID + "@@\";" + "\n"+ "   class :" + Name +";"+"\n"+ "     ."+"\n");// 
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
2
  • 1
    Are you actually using multiple threads? Commented Aug 23, 2012 at 18:13
  • 1
    a) ConcurrentHashMap is a thread-safe map and this doesn't look mutlithreaded and b) which of the two loops? and c) what variables? Commented Aug 23, 2012 at 18:14

2 Answers 2

2

ConcurrentHashMap is designed to be thread-safe.
If you aren't using multiple threads, it's worse than useless.

You should just use a MultiMap.

You can always access values outside the loop.

Sign up to request clarification or add additional context in comments.

Comments

0

ConcorrentHashMap is fail-safe. it won't give any concurrent modification exceptions. it works good for multi threaded operations. The whole implementation of ConcurrentHashMap is same as HashMap but the while retrieving the elements , HashMap locks whole map restricting doing further modifications which gives concurrent modification exception.' But in ConcurrentHashMap, the locking happens at bucket level so the chance of giving concurrent modification exception is not present.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.