Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Please help to search key and return value from text file using data structure in java

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help to search key and return value from text file using data structure in java

    Hi all,

    I am having a file contains key and value pairs separated by space.

    I want to read that text file and store into a data structure in java so that, If i want to search a key in file then it should have to iterate only upto the length of search string (key) entered by end user, not to iterate through the whole file for searching the matching key.

    e. g. If the keys in file are like
    java, java program, java programming

    and if the search string is like "Programming"

    then the search should like :

    if array[0][p] = "p" -> false
    if array[1][r] = "pr" -> false
    if array[2][o] = "pro" -> false
    if array[3][g] = "prog" -> false
    if array[4][r] = "progr" -> false
    if array[5][a] = "progra" -> false
    if array[6][m] = "program" -> true //match found
    if array[7][m] = "programm" -> false
    if array[8][i] = "programmi" -> false
    if array[9][n] = "programmin" -> false
    if array[10][g] = "programming" -> true //match found

    so the longest match is 10

    Then the program should have to return the value related to the key " java programming".


    Please suggest how can we achieve the same feature using java.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Please help to search key and return value from text file using data structure in java

    Not sure what the usage of the second dimension index in the example shows. I assume those variables are ints. How do they get assigned values? Their values appear related to the names which has no relationship in java code.
    The variable named a has nothing to do with the String "a"

    Have you looked at using a Map?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help to search key and return value from text file using data structure in java

    Quote Originally Posted by Norm View Post
    Not sure what the usage of the second dimension index in the example shows. I assume those variables are ints. How do they get assigned values? Their values appear related to the names which has no relationship in java code.
    The variable named a has nothing to do with the String "a"

    Have you looked at using a Map?
    ------------------------------------------------------------------------------------
    Thanks for reply..

    I checked with map searching is working fine. But because of some rule of searching I need to iterate the hash map completely, and it is not giving performance.

    That's the reason, I want to implement it by using data structure in java, so that the iteration of the data structure should be upto the length of search string only.

    Kindly please suggest the data structure that will be the beast for fast reading , searching string in file and return the value accordingly.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Please help to search key and return value from text file using data structure in java

    Can you explain how using a single key to access a Map isn't efficient enough?

    up to the length of search string only.
    Can you explain what that means?

    Are you using substrings of the search string?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help to search key and return value from text file using data structure in java

    the search string we are searching in map is not a key only. It could be a string of multiple lines with one or more keys.

    e. g. Search string :-
    "The Longest rivers in the world is Nile and Amazon. Nile belongs to Africa and Amazon belongs to South America. etc.......... "

    Suppose the above search string contains the key as:-
    rivers, longest, South

    so we have to search this strings into the file and returns the related urls.

    And If we read the complete file and stored it into the map then to search the matching string in the map we have to iterate the map untill we found the matched key.

    so want the fastest search using and data structure. Because by using hash map we got the better performance using hashmap for a shorter search string.

    but if the search string is too long then hash map also taking time.

    so please suggest and algorithm.. can be used

    thanks

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Please help to search key and return value from text file using data structure in java

    Can you explain what you mean here by Search String:
    Search string :-
    "The Longest rivers in the world is Nile and Amazon. Nile belongs to Africa and Amazon belongs to South America. etc.......... "
    Is that what you want to find
    or is that a list of keys you want to search for?

    Or are you separating the separate tokens in the "Search String" and doing a lookup for each token. More tokens will cause more lookups.

    Can you explain this:
    if the search string is too long then hash map also taking time.
    A Map that is given one key will very quickly tell you if it contains that key. I don't know that the length of the key will be an issue.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help to search key and return value from text file using data structure in java

    Quote Originally Posted by Norm View Post
    Can you explain what you mean here by Search String:
    Is that what you want to find
    or is that a list of keys you want to search for?

    Or are you separating the separate tokens in the "Search String" and doing a lookup for each token. More tokens will cause more lookups.

    Can you explain this:
    A Map that is given one key will very quickly tell you if it contains that key. I don't know that the length of the key will be an issue.


    yes the search string can contains multiple tokens. And it's length can be of any size, so we have to split that search string and then search for the matching keys and return the url.

    I have already posted the code implemented by using hash map in the same forum, but I want to implement it by using any different algorithms so that the performance will be faster for any length of string.

    you can search the post on the same forum by searching:
    "Fastest way to read and search a string in a large file using core java"

    Please suggest accordingly.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Please help to search key and return value from text file using data structure in java

    search for the matching keys and return the url.
    How are the search results gathered together? If each token returns a different results, what would be done with all those separate results?

    I have already posted
    If you have posted another thread somewhere, please post a link to it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help to search key and return value from text file using data structure in java

    Hi All,

    I found the solution for the same.

    I created own tree structure and store the keys in file as character by character.

    And now the searching is very fast as compare to other solutions...

Similar Threads

  1. search string in a text file
    By dewet in forum Android Development
    Replies: 2
    Last Post: April 10th, 2012, 02:39 PM
  2. Replies: 2
    Last Post: June 15th, 2011, 03:49 PM
  3. Replies: 1
    Last Post: June 11th, 2011, 05:39 AM
  4. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  5. [SOLVED] i cant make a java jar file which can search data from mysql
    By talha07 in forum JDBC & Databases
    Replies: 6
    Last Post: January 20th, 2011, 06:09 AM