Search code examples
javaarraysstringdata-structureslarge-data

How to deal with a very large array with Java?


First of all, sorry for my very bad English but I'm Italian and I don't know this language well, I hope you'll understand me.
I have to deal with a very large array of Strings (or a different type of data structure), particularly I have to search if a determinate word (or part of it) is contained in this array, exactly like a dictionary.
I have this list of words in a txt file, is there a way to deal with these words without load them to a data structure? If not, what do you recommend to use?
Thank you


Solution

  • As I has already written, I found the solution in a version of suffix tree that I developed. If it can be useful, I quote here the code:

       /* developed by Christian Traina
        * email address: crissstian96@gmail.com
       */
    
    public class Alpha {
    private Alpha[] alpha = new Alpha[26];
    private boolean mark = false;
    
    // SEARCH
    public boolean search(String s){
        return search(s.toCharArray());
    }
    public boolean search(char[] s){
        return search(s,0);
    }
    private boolean search(char[] s, int p){
        if(p==s.length)
            return mark;
        if(alpha[s[p]-97]!=null)
            return alpha[s[p]-97].search(s, p+1);
        else
            return false;
    }
    
    //MATCHES
    //THE ASTERISK MEANS ANY CHARACTERS
    //FOR EXAMPLE, IF YOU WANT TO SEARCH A WORD OF SIX LETTERS THAT BEGINS WITH THE LETTER A, 
    //YOU HAVE TO PASS "A*****"
    public boolean matches(String s){
        return matches(s.toCharArray(), 0);
    }
    private boolean matches(char[] s, int p){
    
        if(p==s.length)
            return mark;
        if(s[p]=='*'){
    
            for(int i=0; i<26; i++)
                if(alpha[i]!=null && alpha[i].matches(s,p+1))
                    return true;
    
            return false;
        }
        else if(alpha[s[p]-97]!=null)
            return alpha[s[p]-97].matches(s, p+1);
    
        else
            return false;
    }
    
    
    // ADD
    
    public void add(String s){
        add(s.toCharArray(), 0);
    }
    public void add(char[] s){
        add(s,0);
    }
    private void add(char[] s, int p){
        if(p==s.length)
            mark=true;
        if(p>=s.length)
            return;
        if(alpha[s[p]-97]==null)
            alpha[s[p]-97] = new Alpha();
        alpha[s[p]-97].add(s, p+1);
    }
    
    
    //PRINT
    //IT PRINTS ALL THE ITEMS OF THE TREE
    
    
    public void print(){
        for(int i=0; i<26; i++)
            if(alpha[i]!=null){
                alpha[i].print(Character.toString((char)(i+97)));
            }
        System.out.println();
    }
    
    private void print(String str){
        if(mark)
            System.out.print(str+" ");
        for(int i=0; i<26; i++)
            if(alpha[i]!=null){
                alpha[i].print(str+Character.toString((char)(i+97)));
            }
    
    }
    }