Hi all,

How do i store matching substring and string inside one single arraylist?

example:

[[er, every, errant]] which saying er substring is inside string every and errant.how should i make the comparison?

basically what i did is only make 2 arraylist

a1 = new ArrayList();
a2 = new ArrayList();

a1.add("er");
a2.add("every");
a2.add("errant");

a2.add(a1); but this does not make any matching between the substring and string. thanks.

Recommended Answers

Not exactly a clear statement of requirements, but if you want to link that list of words to the substring you could use a HashSet with the substring as the Key and the ArrayList of words as its Value.

Jump to Post

You said "How do i store matching substring and string ..." so I assumed you had already compared the substring and string, and were looking for a way to store the ones that match. In the HashMap you use the substring (eg "er") as a key through which you can …

Jump to Post

Hang on, this is going wrong, and it's my fault.
It's essential that you develop your own skills in analysing problems, researching, reading the API, and trying& debugging solutions. What's happening here is that you are asking me before exhausting those processes, and I'm encouraging that by answering too quickly.

Jump to Post

one more question regarding the hashmap

So this is this morning's quota answer:
re-read previous posts. I told you repeatedly what to do with your HashMaps, and asked repeatedly for clarification as to where the duplicates were happening (which you never answered properly). There's really nothing I can add regarding …

Jump to Post

Hello again.
I don't know what this is about - where does this code come from?
TextFileIndexer and searchIndex are not classes, they are methods. They will be defined inside a (or two) classes, but that info is not in your post.
INDEX_COLLECTION is not a variable, it's a parameter …

Jump to Post

All 53 Replies

Not exactly a clear statement of requirements, but if you want to link that list of words to the substring you could use a HashSet with the substring as the Key and the ArrayList of words as its Value.

hmm..but i am not be able to compare the substring and string right?as in whether that substring belong to that string.

You said "How do i store matching substring and string ..." so I assumed you had already compared the substring and string, and were looking for a way to store the ones that match. In the HashMap you use the substring (eg "er") as a key through which you can get the ArrayList of all the matching words (eg "every", "errant") .
If that's not helpful, then maybe you can provide a clearer /more complete description of what you need to achieve?

hmm..myb my requirements not clear enough (my bad). what i mean is i have created 2 arraylist.

one arraylist store terms : every, word, errant
one arraylist store bigrams of the terms : ev, ve, er, ry, wo, or, rd, rr, ra, an, nt

so here i am trying to find the matches between those two arraylist. example: ev belong to every and i need to store them inside the arraylist so the structure of the dates would looks like

[ev [every, errant]
ve [every]
.
.
.
.
and so on
]]

thanks.

sry i mean

[ev [every]
ve [every]
er [every, errant]
.
.
.
and so on
]]

OK, that's a lot clearer, thank you. HashMap<String, Arraylist<String>> still looks like the best choice to me.

but hashmap wont be able to store the value in one arraylist right?thanks.

Sorry, don't understand your question. It would look like:

KEY (String)    VALUE (ArrayList)
"ev"            ["every"]
"ve"            ["every"]
"er"            ["every", "errant"]

hmm..but how do i make comparison in hashmap? like er is having every and errant. and to add them inside the arraylist? mind enlighten me since i never doing hashmap before.thanks

Like I said the hashmap is for storing the matches. In pseudocode it goes something like this:

for every bigram
   for every word
      if word contains bigram
         if there is already a key in the hashmap for this bigram
            get the corresponding ArrayList of words and add this word
         else
            add a new entry in the hashmap, bigram as key, new ArrayList(word) as value
HashMap<String, ArrayList<String>> test = new HashMap<String, ArrayList<String>>();
        ArrayList a1 = new ArrayList();
        ArrayList a2 = new ArrayList();

a1.add("every");
        a1.add("bobs");
        a1.add("three");
        a2.add("ev");
        a2.add("ob");

        for (Object element : a2) {
            for (Object element1 : a2) {
                if (a1.contains(a2)) {  
                    if(!test.isEmpty()) {
                        a2.add(a1);
                        System.out.println(a2);
                    }
                    else {
                        
                    }
                }
            }
        }

my codes.

but it displays empty values. did i miss anything here?thanks for the feedback.

Should line 13 be for (Object element1 : a1) ???
Line 14 should use the loop variables
line 15..
Sorry man, your code really doesn't follow the pseudocode at all

ps code would be better with the ArrayLists declared as <String> -

for (String element : a2) {
            for (String element1 : a1) {
                if (element.contains(element1)) {
                    if(!test.isEmpty()) {
                        
                    }
                    else {

                    }
                }
            }
        }

i modified the codes. but i am not sure how do i get the arraylist value out?

for (String element : a2) {
            for (String element1 : a1) {
                if (element.contains(element1)) {
                    if(element.equals(test.containsKey(element))) {
                      test.get(a1).add(element);
                    }
                    else {
                      test.put(element,a2);
                    }
                }
            }
        }
        System.out.println(test);

did this. but the value inside is empty. is something wrong with the codes?i followed according to the psuedo. thanks.

4: if (test.containsKey(element)) { // this is all yu need

5: test.get(element).add(element1) // key is bigram, value is word

8: test.put(element, ... // value here needs to be a new ArrayList containing word]

Note: variable names make this hard or easy - element/element1 doesn't help, bigram/word would make it much clearer

hi

a1.add("every");
        a1.add("bobs");
        a1.add("three");
        a2.add("ev");
        a2.add("ob");

        for (String bigram1 : a2) {
            for (String words : a1) {
                if (words.contains(bigram1)) {
                    if(test.containsKey(bigram1)){
                      test.get(bigram1).add(words);
                      
                    }
                    else {
                      test.put(bigram1, new ArrayList<String>(a1));
                      
                    }
                }
            }
        }
        System.out.println(test);

it does display the values but the whole values come out

{ev=[every, bobs, three], ob=[every, bobs, three]}

no comparison being made.anything wrong with my codes here?thanks.

Is there anything wrong with my codes as why the whole values are being displayed?thanks.

Line 15 you add a new arraylist containing the whole word list (a1), this should just be the single word that you just found (words) (NB: not the same syntax).

ps Variable words is a confusing name, change it to word because it only contains one word at a time!

pps: This is a perfect example of how good/bad variables names can help/hinder you. Your word list is called "a1", which means nothing. If you gave it a good name, eg "allTheWords" then your mistake would have been obvious... test.put(bigram1, new ArrayList<String>(allTheWords)); // obviously that's not right!

hi james,

for (String bigram1 : bigram) {
            for (String word_ : allwords) {
                    if (word_.contains(bigram1)){
                      if(test.containsKey(bigram1)) {
                      test.get(bigram1).add(word_);                      
                      }
                    else {
                      test.put(bigram1, new ArrayList<String>(word_));
                    }
                }
            }
        }

is the test.put is wrong to put new ArrayList<String>(words)? but (does not accept words).

if(test.containsKey(bigram1)) {
    test.get(bigram1).add(word_);
    break;
}

@mKorbel - OP needs to add ALL words that match, so the break is INCORRECT

@drogba123 - do NOT put in the break suggested in the previous post.
I did say it's not the same syntax to create a new ArrayList with just one String in it. You have to do something like:

ArrayList<String> temp = new ArrayList<String>(); // new arraylist...
temp.add(word_); // .. containing just the current word ...
test.put(bigram1, temp); // ... is added to the HashMap

thanks guys for the feedback.so actually i need a temporary arraylist to contain the words?

ah i got it.so temporary array is to store only the words that i wanted..thanks james.u have been helping me since yesterday. many thanks.gonna try it out.

Almost... the temp reference is for you to create a new ArrayList, add a word to it, and add the ArrayList to the HashMap. After that the temp reference goes out of scope and dies - it's not needed any more.

okay got it. is there any way to sort the hashmap based on key and value? i tried to sort using collection but it does not work

{ra=[errant], nt=[errant], ev=[every], or=[word], an=[errant], wo=[word], ve=[every], rr=[errant], rd=[word], er=[every, errant], ry=[every]}

it should be an start in the first list. any suggestions?

i do collections.sort(bigram);

okay got it. is there any way to sort the hashmap based on key and value? i tried to sort using collection but it does not work

{ra=[errant], nt=[errant], ev=[every], or=[word], an=[errant], wo=[word], ve=[every], rr=[errant], rd=[word], er=[every, errant], ry=[every]}

it should be an start in the first list. any suggestions?

i do collections.sort(bigram);

Easiest solution is probably just to change your HashMap to a TreeMap - it has all the same keys/values/methods etc, but it keeps the data sorted into order according to the keys (bigrams) automatically

tree map?will i be able to output the values inside to text file?sorry..never learn this thing before..all i learn is standard array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.