[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java Hash MAP in a Hash MAP

Posted on 2011-05-09
7
Medium Priority
?
320 Views
Last Modified: 2012-05-11
I am trying to use HashMap in a Hash Map.
can you please help me in achieving this output:
==
File.txt contents:
PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED
logs:/usr/local/bin
===
PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED
logs:/usr/local/bin1
===
output should of the form:

PASS  4   1 [PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin
          2 [PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1

FAIL  4   1 [PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin
          2 [PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1

ENHANCEMENT  1  [PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin
           2  [PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1

Basically want this to be like a Map in Map.
2nd MAP will have results number(telling the 1 st entry,...(key) and 2 entries with the String followed by location of logs.

so when I want PAss, entry 2:
it will print the String and Link.
==

previous related  question which yan helped me to understand HashMAp and with the solution.
: http://www.experts-exchange.com/Programming/Languages/Java/Q_26982258.html

Mainly I am trying to get this HashMap in HashMap.
0
Comment
Question by:vkchaitu82
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35726700
Using two Map instances is unnecessarily complex and will make the code hard to use and maintain. All you need is one Map, the key being as before and the value being a bean containing the info you need, say
Map<String, LogInfo>

Open in new window

0
 
LVL 8

Expert Comment

by:colr__
ID: 35727734
I agree with CEHJ, using a map within a map is going to create a multi-dimensional container, which is very unlikely what you actually want. It is possible though:

Map firstMap = new HashMap<String Map>();
Map secondMap = new HashMap<String, String>();
firstMap.put("someIndex", secondMap);
...
// you'd then need to create a new map and add entries to it for every entry in firstMap:
...
Map fifteenthMap = new HashMap<String, String>();
firstMap.put("somefifteenthIndex", fifteenthMap );

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35727775
This is the program.
I kept the ooutput of the list of logs
before the individual lines - it makes them look
better.
If you want, you can easily remove it.

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.*;

public class HashMapWithWord {

public HashMapWithWord() {

    HashMap m1 = new HashMap();
      HashMap m2 = new HashMap();
      ArrayList al = new ArrayList();
    ArrayList alw = new ArrayList();

    try {
        DataInputStream in = new DataInputStream(new FileInputStream("file.txt"));
        String buff = null;
         String log = null;
        String longWord = null;
        while((buff=in.readLine()) != null){
            if(buff.startsWith("==")){
                for(int j=0; j<al.size();j++)  {
                    String s = (String)al.get(j);
                    if(m1.get(s) != null){
                        Word w = (Word) m1.get(s);
                        w.increment();
                        w.addLog(log);
                        w.store(longWord,log);
                        m1.put(s,w);
                    }else
                    {
                        Word w = new Word(s, log);
                        w.store(longWord,log);
                        m1.put(s,w);

                    }

                    if(!alw.contains(s))alw.add(s);
                     /*
                    if(m1.get(s) != null){
                        Integer ii = (Integer) m1.get(s);
                        Integer iii = new Integer(ii.intValue()+1);
                          m1.put(s,iii);
                                            } else {
                        Integer ii = new Integer(1);
                        m1.put(s, ii);
                    }
                    if(m2.get(s) != null){
                        ArrayList all = (ArrayList) m2.get(s);
                        if(!all.contains(log))all.add(log);
                        m2.put(s,all);
                    }  else
                    {
                        ArrayList all = new ArrayList();
                        all.add(log);
                        m2.put(s,all);
                    }
                    */

                }
                al = new ArrayList();

                continue;
            }  else
            if(buff.startsWith("logs")){
                log = buff.substring(buff.indexOf(":")+1);
                   continue;
            } else
            {
                StringTokenizer  t = new StringTokenizer(buff);
                longWord = buff;
                 al = new ArrayList();
                while(t.hasMoreTokens()){
                    al.add(t.nextToken());
                }

            }



        }
        in.close();

        Collections.sort(alw);

        for(int j=0; j<alw.size(); j++){
            String s = (String) alw.get(j);
            System.out.print(s + "  ");

            Word w = (Word) m1.get(s);

        //    Integer ii = (Integer)m1.get(s);
            System.out.print("" + w.getCount() + "  ");
           // ArrayList logg = (ArrayList)m2.get(s);
              ArrayList logg = w.getLogs();

            for(int jj=0; jj<logg.size(); jj++){
                String ss = (String)logg.get(jj);
                if(jj<logg.size()-1)
                System.out.print("logs:" + ss + ",");
                else
                    System.out.print("logs:" + ss);


            }
            System.out.println("");
            HashMap m0 = w.getMap();
            Set ss = m0.keySet();
            Iterator it = ss.iterator();
            int count =1;
            while(it.hasNext()){
                String myS = (String)it.next();
                String myL =(String)m0.get(myS);
                System.out.println(count + "[" +myS + "]logs:" + myL);
                count++;
            }


            

            System.out.println("");
        }

    }catch(Exception ex){
        System.out.println(ex.toString());
        ex.printStackTrace();
    }






    }

    public static void main (String [] args){
        new HashMapWithWord();
    }


}

class Word {
    String name;
           int count;
           ArrayList  fileNames;

      HashMap m;
public  Word(String name, String fileName){
    count = 1;
    this.name = name;
    ArrayList al = new ArrayList();
    m = new HashMap();
    al.add(fileName);
    this.fileNames = al;

}

public void addLog(String s){
    if(!fileNames.contains(s)) fileNames.add(s);

}
public void store(String s, String log){
    m.put(s,log);

}

public HashMap getMap() { return m;}

public void increment() {
    count++;
}

  public int getCount() {
      return count;
  }

    public ArrayList getLogs(){
        return fileNames;
    }


}

Open in new window


Input:
PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED
logs:/usr/local/bin
===
PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED
logs:/usr/local/bin1
===

Open in new window


Output:

ENHANCEMENT  2  logs:/usr/local/bin,logs:/usr/local/bin1
1[PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1
2[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin

FAIL  4  logs:/usr/local/bin,logs:/usr/local/bin1
1[PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1
2[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin

FEATURE_NOT_SUPPORTED  1  logs:/usr/local/bin
1[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin

NOT_EXECUTED  2  logs:/usr/local/bin,logs:/usr/local/bin1
1[PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1
2[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin

PASS  4  logs:/usr/local/bin,logs:/usr/local/bin1
1[PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1
2[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vkchaitu82
ID: 35734823
Thanks Yan , I am looking into this.
actually I dont need logs in the output.

I just need:
FAIL  4  
1[PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1
2[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin


CEHJ,
>All you need is one Map, the key being as before and the value being a bean containing the info you n
I did not get  your comment. I feel we do need 2 hash tables to achieve this as Yan suggested .
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35734832
No more logs in the output:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.*;

public class HashMapWithWord {

public HashMapWithWord() {

    HashMap m1 = new HashMap();
      HashMap m2 = new HashMap();
      ArrayList al = new ArrayList();
    ArrayList alw = new ArrayList();

    try {
        DataInputStream in = new DataInputStream(new FileInputStream("file.txt"));
        String buff = null;
         String log = null;
        String longWord = null;
        while((buff=in.readLine()) != null){
            if(buff.startsWith("==")){
                for(int j=0; j<al.size();j++)  {
                    String s = (String)al.get(j);
                    if(m1.get(s) != null){
                        Word w = (Word) m1.get(s);
                        w.increment();
                        w.addLog(log);
                        w.store(longWord,log);
                        m1.put(s,w);
                    }else
                    {
                        Word w = new Word(s, log);
                        w.store(longWord,log);
                        m1.put(s,w);

                    }

                    if(!alw.contains(s))alw.add(s);
                     /*
                    if(m1.get(s) != null){
                        Integer ii = (Integer) m1.get(s);
                        Integer iii = new Integer(ii.intValue()+1);
                          m1.put(s,iii);
                                            } else {
                        Integer ii = new Integer(1);
                        m1.put(s, ii);
                    }
                    if(m2.get(s) != null){
                        ArrayList all = (ArrayList) m2.get(s);
                        if(!all.contains(log))all.add(log);
                        m2.put(s,all);
                    }  else
                    {
                        ArrayList all = new ArrayList();
                        all.add(log);
                        m2.put(s,all);
                    }
                    */

                }
                al = new ArrayList();

                continue;
            }  else
            if(buff.startsWith("logs")){
                log = buff.substring(buff.indexOf(":")+1);
                   continue;
            } else
            {
                StringTokenizer  t = new StringTokenizer(buff);
                longWord = buff;
                 al = new ArrayList();
                while(t.hasMoreTokens()){
                    al.add(t.nextToken());
                }

            }



        }
        in.close();

        Collections.sort(alw);

        for(int j=0; j<alw.size(); j++){
            String s = (String) alw.get(j);
            System.out.print(s + "  ");

            Word w = (Word) m1.get(s);

        //    Integer ii = (Integer)m1.get(s);
            System.out.print("" + w.getCount() + "  ");
           // ArrayList logg = (ArrayList)m2.get(s);
              ArrayList logg = w.getLogs();
                   /*
            for(int jj=0; jj<logg.size(); jj++){
                String ss = (String)logg.get(jj);
                if(jj<logg.size()-1)
                System.out.print("logs:" + ss + ",");
                else
                    System.out.print("logs:" + ss);


            }
            */

            System.out.println("");
            HashMap m0 = w.getMap();
            Set ss = m0.keySet();
            Iterator it = ss.iterator();
            int count =1;
            while(it.hasNext()){
                String myS = (String)it.next();
                String myL =(String)m0.get(myS);
                System.out.println(count + "[" +myS + "]logs:" + myL);
                count++;
            }


            

            System.out.println("");
        }

    }catch(Exception ex){
        System.out.println(ex.toString());
        ex.printStackTrace();
    }






    }

    public static void main (String [] args){
        new HashMapWithWord();
    }


}

class Word {
    String name;
           int count;
           ArrayList  fileNames;

      HashMap m;
public  Word(String name, String fileName){
    count = 1;
    this.name = name;
    ArrayList al = new ArrayList();
    m = new HashMap();
    al.add(fileName);
    this.fileNames = al;

}

public void addLog(String s){
    if(!fileNames.contains(s)) fileNames.add(s);

}
public void store(String s, String log){
    m.put(s,log);

}

public HashMap getMap() { return m;}

public void increment() {
    count++;
}

  public int getCount() {
      return count;
  }

    public ArrayList getLogs(){
        return fileNames;
    }


}

Open in new window



ENHANCEMENT  2  
1[PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1
2[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin

FAIL  4  
1[PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1
2[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin

FEATURE_NOT_SUPPORTED  1  
1[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin

NOT_EXECUTED  2  
1[PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1
2[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin

PASS  4  
1[PASS FAIL PASS FAIL ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin1
2[PASS FAIL PASS FAIL FEATURE_NOT_SUPPORTED ENHANCEMENT NOT_EXECUTED]logs:/usr/local/bin

Open in new window

0
 

Author Comment

by:vkchaitu82
ID: 35734851
Thanks Yan you are fast .. I was  trying to do that ...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734852
You are always welcome.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question