Need a good Data Structure/ Algorithm

I am working on a small problem that requires me to cache a large amount of key/value pairs of data. I must then do a lookup on the cache for a large number of words one after another until I find a word that is present as a Key and then quit.
The cache is pretty large. About 600 - 1000 words as keys and ints as values.
Is there an existing Data Structure in Java for this? If not, what type of data structure (B-tree etc) is best suited to cache such data and what algorithm should be used for lookup?

The lookup for hashmap is efficient for very large maps up to the amount of memory you have. If you tune the load factor you can minimise the impact of having very large tables.
Another option is to have a fixed array of HashMaps. You can use the hashcode (or your own) to put the key, value into the HashMap. I am not sure if this is actaully any faster, as I believe one is enough, but if you have concern this is what you can do.

Similar Messages

  • Need links for data structure and algorithms.

    Hi.
    I am just new to java but need to learn data structure and algorithms.
    Do your guys got any good links or bbs to learn?
    Thanx in advance

    http://www.amazon.com/exec/obidos/tg/detail/-/1571690956/ref=cm_huw_sim_1_3/104-7657019-1043968?v=glance
    http://www.amazon.com/exec/obidos/tg/detail/-/0534376681/ref=cm_huw_sim_1_4/104-7657019-1043968?v=glance
    http://www.amazon.com/exec/obidos/tg/detail/-/0672324539/ref=cm_huw_sim_1_2/104-7657019-1043968?v=glance
    http://www.amazon.com/exec/obidos/tg/detail/-/0201775786/qid=1060946080/sr=8-1/ref=sr_8_1/104-7657019-1043968?v=glance&s=books&n=507846
    $8 for the first

  • I need a good data base system for both my imac and my PC. Any ideas?

    I am a REaltor looking for a good data base software for my company and my agents that is usable with both the Mac computers and the PC's.

    That would be FileMaker Pro, they make versions for both PC's and Macs. You can find information at:
    http://www.filemaker.com/products/filemaker-pro/

  • Need help in Data Structure in Java

    Design a FSA which accepts all strings over {a,b} which end in b, and in which every b (except for the last one) is immediately followed by two or more a's.
    Convert the following into a deterministic finite state automaton, using the subset construction algorithm.
    a,b
    \/ a b a
    --> 0 --> 1 --> 2 ---> 3
    State 3 is accepting.
    Describe (in English) the language which the regular expression (a|ba)* denotes.
    Using the techniques discussed in class, convert the regular expression in the previous problem to an FSA with epsilon transitions.
    Now convert the FSA from the previous problem to an equivalent FSA without epsilon transitions.
    I have to solve this problem .. can anyone help me out here?

    I have to solve this problem .. can anyone help me out
    here?Your code didn't show up in the post. Please re-post with code
    describing where you are having the problem.

  • I need a good data dump for practise

    Can anyone refer me to where I can get really nice data dump for sql and plsql practise.
    Thanks
    Michael

    <p>Michael,</p>
    </p>Read this article Oracle's Sample Schemas: Saying Goodbye to Scott. You should find sample data in ORACLE_HOME\demo and ORACLE_HOME\rdbms\admin directories. </p>
    <p>With kind regards,</p>
    <p>Jornica</p>

  • Need help with Data Structures!

    I'm finally in Java 2nd year! I'm working on a problem- have been working for hours but cant seem to get the hang for it. Basically after determining that an integer is odd, and not more than 10- we are supposed to print it this way
        1
                 333
              55555
           7777777
        999999999 This is what I've done so far!
    public class Numbers {
               public  static int print (int x) {
             String [] L = new String [x.length];
           if (x <= 1) {
            return x;
            else if  (x >= 10 ||  x% 2 != 1)
             System.out.println("Number is greater than 10");
            else
            for (int i = 0;  i < L.length; i ++) {
            System.out.println(x);
            return print (x -2);
          now when I run this program my output is
    9
    9
    9
    9
    9
    9
    9
    9
    7
    7
    7
    7
    7
    7
    7
    and so on untill
    1.
    I cant figure out which approach to take next. Tips please!

    it doesnt matter, I made it unrecursive, Ill just fix ur code up here:
    public class Numbers
    public  static int print (int x)
    if (x <= 1)      
    return x;       
    else if  (x >= 10)                 
    System.out.println("Number is greater than 10");
    else if ( x% 2 != 1)    
    System.out.println("Number is even"); 
    else
    int p = print(x-2);
    for (int i = 0;  i < x; i ++)
    System.out.print(x);       
    System.out.println();           
    return p;
    }

  • Data Structures and Algorithms in java book

    Hi guys,
    I want to know a good book which is good for Data Structures and Algorithms in java. I am good at Core java but a beginner for Data Structures in Java. I am a little poor in Data Structures concepts.
    Following are the books I have found on the net. Could you help me the choose the best outta them.
    1. Data Structures and Algorithms in Java - Mitchell Waite
    2. Data Structures in Java - Sandra Anderson
    3. Fundamentals of OOP and Data Structures in Java - Richard Weiner & Lewis J. Pinson
    4. Object Oriented Data Structures Using Java - Nell Dale, Daniel T. Joyce, Chip Weems

    lieni wrote:
    I good data structures book doesn't have to be language-specific.Thx DrLazlo, my speachYes.
    The OP wrote:
    I have access to these books and dont know which one to start with.What I meant is that you shouldn't narrow your search to insist that the book you choose have "Java" in the title.

  • Looking for an efficient data structur & search algorithm

    Hi all
    i have a list of digits (international phone network prefixes) with some hundreds to some thousends entries. An entry may be in the form
    ^00[1-9]{1}[0-9]{0,7}$
    I.e. this might be 001, 0041, 00317545, 00317548, 00317549 and so on. Regarding the last three examples, it might even be that there is an additonal 0031754.
    What i need a a data structur that allows to match these prefixes against a phonenumber.
    I.e. if i have the phonennumber 001123456789 it would match the prefix 001. If i had 00317549111 it would match 00317549.
    The easiest way would be to but all prefixes into an Vector or similar and loop over all entries, trying to match the phonenumber with startsWith(). But this wouldn't always result in a absolutely perfect match, since, i.e. for the phonenumber 00317549111 the check against the prefix 0031754 would return a match even if there was a more specific match with the prefix 00317549. But more than that, this simple algorithm is not very efficient.
    So i am looking for a more efficient way/pattern to do this. I thought about a kind of tree structure, starting with 00 in the top level, than provding [1-9] in the second level, and [0-9] from third level on. Then on every node it would either store if there is a matching prefix on that level, or if there is a prefix starting with that digits on a lower level or if there is no prefix on that level or any lower.
    I.e. when i have the phonenumber 00317549111 it would start at the top level with 00. That would be ok. On the next level it would check if there is a node for digit 3. If there is, it would go one level deeper and check if there is a node for digit 1. If yes, again it would go one level deeper to check if there is a node for digit 7. If that algorithm comes to a level where, for the request digit, it get's a prefix indicator rather than a node indicator, the algortihm would know, that a matching prefix was found and that there is no more specifig match on deeper levels.
    One thing i forgot to mention - the prefixes might be read once during startup/init and there it might take some time for building up the datastructur - i don't care about that. But, when running, then the maching process should be as efficient as possible, that's the most important point for me.
    What do you think about a pattern like this? Could this be efficient? Do you see other patterns, that might be easier to implement and that might be faster/need less memory?
    Thanks a lot for your help.
    Cheers, Frank

    I would really have gone for your first approach. With mperemsky5's approach you have the loop with (potential) n iterations (Let n be the length of the number) and in each iteration to compute the hash-code for the string which again takes time proportional to the strings length.
    The tree approach takes time equal to the length of the prefix and is imho not more complicated.
    Perhaps this way:
    public class DigitTree
      private class Node {
           private Object content;
           private Node[] children = new Node[10];
      private Node root = new Node();
      public DigitTree() {
      public void addPrefix(String prefix, Object value) {
           char[] numberChars = prefix.toCharArray();
           Node node = root;
           for (int i=0; i<numberChars.length; i++) {
                int number = numberChars[i] - '0';
                if (node.children[number] == null) node.children[number] = new Node();
                node = node.children[number];
           node.content = value;
      public Object match(String phonenumber) {
           char[] numberChars = prefix.toCharArray();
           Node node = root;
           for (int i=0; i<numberChars.length; i++) {
                int number = numberChars[i] - '0';
                if (node.children[number] == null) return code.content;
                node = node.children[number];
           return node.content;
    }The method addPrefix lets you add a prefix to the tree. The content-Object can hold a String or whatever to identify the prefix. If your data is not complete (i.e. if there are numbers for which no prefix exists) you might want to initialize the content-object of a node with a default value (e.g. "not found").
    The method match lets you look up a prefix for a given number and returns the Object associated with the prefix..
    The code was not tested.
    Greetings
    Thomas

  • Data structure similar to database table

    i have the need for a data structure that will essentially be the same as a database table. this "table" can be queried against and sorted.
    currently, i am using an interface to abstract this "table" datastructure, and implementing the interface via jdbc calls.
    may someone point me in the right direction?
    in .NET, they have the DataTable and DataSet data structures, which do exactly as i require. These are objects essentially correspond to a database table and database, respectively. They are used as in-memory database.
    i have already tried using HSQL, but was wondering if there was a "better" way (i.e. more light-weight, intuitive, easier to use).
    thanks.

    Use an array and put your records in it.
    Efficient querying (simple queries) requires you to build indices for each element you wish to query on. The type of index depends on the kind on search you want to perform. Exact matches can use Hashes. Sorting, prefix or postfix matching can be performed using various tree indices.
    Complex queries (i.e. those that query on multiple parameters generally require query optimisers to figure out how to use the indies you have in the most efficient manner possible (well as close as the optimizer can get to it).
    cheers
    matfud

  • Complex data structure.

    Hi,
    This is a design issue that I have run into many times and I was wondering how you all solve it. When using complex data structures (such as array of clusters, or clusters containg arrays and/or other clusters), I have found that there are many situations where you need to wire either an "empty" stucture or the type of the structure as an input. For example, when building up the data structure, I often use shift registers, and need an empty data structure to initialize the shift register. Also when bundling a cluster by name you need the type of the cluster. My question is - what do you use in these situations? Of course, I always typedef my complex structures, and I know that there is a default value for them, because other language featu
    res depend on there being a default. Is there anyway to get the default value for a typedef? Not seeing one, I have taken to creating a Global or Dummy VI for each typedef that returns a default data structure, which is what I use in all these situations. What other approaches have you seen?

    > of the cluster. My question is - what do you use in these situations?
    > Of course, I always typedef my complex structures, and I know that
    > there is a default value for them, because other language features
    > depend on there being a default. Is there anyway to get the default
    > value for a typedef? Not seeing one, I have taken to creating a Global
    > or Dummy VI for each typedef that returns a default data structure,
    > which is what I use in all these situations. What other approaches
    > have you seen?
    You can right click on a wire or terminal and create a constant for the
    type already there. This will sometimes fool you if you are wanting the
    input to a shift register since the types flow downstream and you want
    upstream. Just click somewhere whe
    re the type is known, create the
    const and move it where you want it.
    The BIG downside to this is that typedef constants tend to be BIG. My
    favorite solution is to make a subVI that returns the default for the
    typedef. Put it in user.lib and it is readily accessible. You can
    think of it as a constructor for your datatype, if you want. In fact
    you can make several of these or parameterize it to return different
    flavors. Being the size of all other icons, it fits nicely into the
    diagram and adapts to typedef changes.
    Greg McKaskle

  • MRS for Data structure table

    I need to various data structure table for MRS

    A possible solution is create a materialized view and then a check constraint on the materialized view: this should work in a multiuser environment.
    Example:
    SQL> drop table t;
    Table supprimee.
    SQL>
    SQL> create table t (
      2  x integer,
      3  y varchar2(10)
      4  );
    Table creee.
    SQL>
    SQL> CREATE MATERIALIZED VIEW LOG on t
      2  WITH ROWID (x, y)
      3  including new values;
    Journal de vue materialisee cree.
    SQL>
    SQL> CREATE MATERIALIZED VIEW t_mv
      2  REFRESH FAST ON COMMIT AS
      3  SELECT count(*) cnt
      4  FROM t;
    Vue materialisee creee.
    SQL>
    SQL> ALTER TABLE t_mv
      2  ADD CONSTRAINT chk check(cnt<=1);
    Table modifiee.
    SQL>
    SQL> insert into t values(1,'Ok');
    1 ligne creee.
    SQL> commit;
    Validation effectuee.
    SQL>
    SQL> insert into t values(2,'KO');
    1 ligne creee.
    SQL> commit;
    commit
    ERREUR a la ligne 1 :
    ORA-12008: erreur dans le chemin de regeneration de la vue materialisee
    ORA-02290: violation de contraintes (TEST.CHK) de verification

  • Two-way lookup data structure?

    I am looking for a good data structure for two-way lookup of paired values. Can anyone point me to one?
    My list are quite short, so I could just do this with an array and not worry about it, but I'm curious what is out there.

    Bidirectional maps
    A new interface hierarchy has been added to support bidirectional maps - BidiMap. These represent maps where the the key can lookup the value and the value can lookup the key with equal ease
    ...yes, perfect. Thanks, again.

  • Urgent! Need help in deciding data structure to use

    Hi all,
    I need to implement a restaurant system by which a customer can make a reservation.
    I was wondering whether vector would be able to be store such an object, The thing is I don't want a complex data structure. just sumthin simple cos i hardly have anytime left to my submission. sighz...
    The thing is I need to able to search based on 2 properties of an object. Eg. I need to search for a reservation based on the customer name and the date he reserved a table.
    But I am totally clueless how to search thru a vector based on 2 properties of the object... Would really appreciate some help. Like an example how to so so based on my program. Feelin so lost...This is all I have so far:
    class AddReservation
         static BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
         //Main Method
         public static void main (String[]args)throws IOException
              String custName, comments;
              int covers, date, startTime, endTime;
              int count = 0;
              //User can only add one reservation at a time
              do
                   //Create a new reservation
                   Reservation oneReservation=new Reservation();                         
                   System.out.println("Please enter customer's name:");
                   System.out.flush();
                   custName = stdin.readLine();
                   oneReservation.setcustName(custName);
                   System.out.println("Please enter number of covers:");
                   System.out.flush();
                   covers = Integer.parseInt(stdin.readLine());
                   oneReservation.setCovers(covers);
                   System.out.println("Please enter date:");
                   System.out.flush();
                   date = Integer.parseInt(stdin.readLine());
                   oneReservation.setDate(date);
                   System.out.println("Please enter start time:");
                   System.out.flush();
                   startTime = Integer.parseInt(stdin.readLine());
                   oneReservation.setstartTime(startTime);
                   System.out.println("Please enter end time:");
                   System.out.flush();
                   endTime = Integer.parseInt(stdin.readLine());
                   oneReservation.setendTime(endTime);
                   System.out.println("Please enter comments, if any:");
                   System.out.flush();
                   comments = stdin.readLine();
                   oneReservation.setComments(comments);
                   count++;
              while (count<1);
              class Reservation
              private Reservation oneReservation;
              private String custName, comments;
              private int covers, startTime, endTime, date;
              //Default constructor
              public Reservation()
              public Reservation(String custName, int covers, int date, int startTime, int endTime, String comments)
                   this.custName=custName;
                   this.covers=covers;
                   this.date=date;
                   this.startTime=startTime;
                   this.endTime=endTime;
                   this.comments=comments;
              //Setter methods
              public void setcustName(String custName)
                   this.custName=custName;
              public void setCovers(int covers)
                   this.covers=covers;;
              public void setDate(int date)
                   this.date=date;
              public void setstartTime(int startTime)
                   this.startTime=startTime;
              public void setendTime(int endTime)
                   this.endTime=endTime;
              public void setComments(String comments)
                   this.comments=comments;
              //Getter methods
              public String getcustName()
                   return custName;
              public int getCovers()
                   return covers;
              public int getDate()
                   return date;
              public int getstartTime()
                   return startTime;
              public int getendTime()
                   return endTime;
              public String getComments()
                   return comments;
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    class searchBooking
         static BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
         public static void main (String[]args)throws IOException
              int choice, date, startTime;
              String custName;
                   //Search Menu
                   System.out.println("Search By: ");
                   System.out.println("1. Date");
                   System.out.println("2. Name of Customer");
                   System.out.println("3. Date & Name of Customer");
                   System.out.println("4. Date & Start time of reservation");
                   System.out.println("5. Date, Name of customer & Start time of reservation");
                   System.out.println("Please make a selection: ");          
                   //User keys in choice
                   System.out.flush();
                   choice = Integer.parseInt(stdin.readLine());
                   if (choice==1)
                        System.out.println("Please key in Date (DDMMYY):");
                        System.out.flush();
                        date = Integer.parseInt(stdin.readLine());
                   else if (choice==2)
                             System.out.println("Please key in Name of Customer:");
                             System.out.flush();
                             custName = stdin.readLine();
                   else if (choice==3)
                             System.out.println("Please key in Date (DDMMYY):");
                             System.out.flush();
                             date = Integer.parseInt(stdin.readLine());
                             System.out.println("Please key in Name of Customer:");
                             System.out.flush();
                             custName = stdin.readLine();
                   else if (choice==4)
                             System.out.println("Please key in Date (DDMMYY):");
                             System.out.flush();
                             date = Integer.parseInt(stdin.readLine());
                             System.out.println("Please key in Start time:");
                             System.out.flush();
                             startTime = Integer.parseInt(stdin.readLine());
                   else if (choice==5)
                             System.out.println("Please key in Date (DDMMYY):");
                             System.out.flush();
                             date = Integer.parseInt(stdin.readLine());
                             System.out.println("Please key in Name of Customer:");
                             System.out.flush();
                             custName = stdin.readLine();
                             System.out.println("Please key in Start time:");
                             System.out.flush();
                             startTime = Integer.parseInt(stdin.readLine());
                        }

    Please stop calling your questions urgent. Everybody's question is urgent to them. Nobody's are urgent to the people who are going to answer them. Calling your questions urgent suggests that you think they are more important than others' (They're not.) and will only serve to irritate those who would help you. It won't get your questions answered any sooner.

  • Need help on efficient searches using hashes on large #s of data structures

    I have a massive amount of data and I need a way to access it using generated indexes instead of traditional searching such as binary chop as I require a fast response time.
    I have 100,000 Array data structures each containing roughly 10 to 60 elements. The elements of the Arrays do not store any raw data they just hold references to objects. I have about 10,000 unique objects which are referenced to by the arrays.
    What I need to do is supply a number of objects as the query and the system should return all the arrays which contain two or more different objects from the query.
    So for example>
    Given query objects> &#8220;DER&#8221; , &#8220;ERE&#8221; , &#8220;YPS&#8221;
    and arrays> [DER, SFR, PPR]
         [PER, ERE, SWE, YPS]
         [ERE, PPD, DER, YPS, SWE]     
         [PRD, LDF, WSA, MMD]
    It should return arrays 2 and 3.
    I had a look at hashMaps but I&#8217;m not entirely sure if that&#8217;s what I need.
    Would I need to hash the each Array and then hash the query objects and look for overlaps in the two hashes or something similar?
    Thanks In Advance

    For a single query, it's more efficient to do less work, so don't create intermediate objects, don't create a index of objects to sets of possibles, and stop counting matches when you find enough:.
                    final HashSet<String> querySet = new HashSet<String>(Arrays.asList(query));
                    final Set<String> results = new LinkedHashSet<String>();
                    // find matches using scan of all arrays
                    for (String[] array : data) {
                        int matchCount = 0;
                        for (String elt : array) {
                            if (querySet.contains(elt)) {
                                ++matchCount;
                                if (matchCount >= 2) {
                                    results.add(Arrays.asList(array).toString());
                                    break;
                    }Using random data, 100,000 arrays of selections from a list of 10,000 trigrams, scanning all the arrays takes about as long as the set code, but doesn't require generation of the map, which can take up a lot of memory if you pre-create the sets (I didn't complete a run doing that, although it should be faster to query, it used too much memory and my computer started swapping so I gave up after a few minutes).
    Running queries of size 2 to 25, times in ms:
    linear scan of all arrays in data set:
    elapsed: 2196
    creating index of trigram to arrays containing trigram:
    elapsed: 4635
    query using set union and index lookup:
    elapsed: 2195
    query using direct scan and index lookup:
    elapsed: 164
    So if I was doing one query per dataset, just use a simple scan as above. If you're doing lots on the same dataset, then create a map<String, List<String[]>> to index the arrays, and the lookup will save quite a bit of time.
    Creating the index:.
                dataSets = new HashMap<String, List<String[]>>();
                for (String trigram : trigrams)
                    dataSets.put(trigram, new ArrayList<String[]>());
                for (String[] array : data)
                    for (String trigram : array)
                        dataSets.get(trigram).add(array);Finding matches using scan of arrays with at least one elt in query:.
                    final HashSet<String> querySet = new HashSet<String>(Arrays.asList(query));
                    final Set<String> results = new LinkedHashSet<String>();
                    for (String trigram : query) {
                        for (String[] array : dataSets.get(trigram)) {
                            for (String elt : array) {
                                if (elt != trigram && querySet.contains(elt)) {
                                    results.add(Arrays.asList(array).toString());
                                    break;
                    }

  • CO11N  I need than operation date is equal to goods movements date

    I am notifying with the CO11N, and need that the date of the movements of goods is equal to the  finish operation date .
    For default it takes the release date of the order.
    Is it possible this?
    Thank you.

    Hi
    Defaults will be like that only.
    If you want you can change the date of posting as requited.
    Can you explain your sceanrio in detail.
    Regards
    YMREDDY

Maybe you are looking for

  • How do I get the questions submitted by everyone stopped being sent to my e-mail

    I don't know if this is the right community to be asking this to, but, it's the only one I belong to.  I have tried to change my notification preferences singly and all together, and neither does any good. Does anyone know how to stop this?  In 2 day

  • File Security and Encryption - any information?

    I'm impressed with the file sharing (free version) but if its used for business information and documents how secure are they from prying eyes / unauthorised download / hacking etc. ?

  • Editable Spreadsheets in InDesign/DPS

    Anyone have any suggestions for importing Excel or Google Spreadsheet data into a customized table created in InDesign? The other issue I'm working around is I would like the option of making edits inside the spreadsheet and be able to easily update

  • Server side java tech

    hi there what does server side java tech involve? any idea?

  • Parcel Track

    I just bought a case for my zen micro can i track it's if i can how do i get the tracking number?