• Post Reply Bookmark Topic Watch Topic
  • New Topic

what collection to use  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I plan to write a searchable English dictionary. I have a csv file of the dictionary. it is sorted(by key/word). in my app, the user types a query(using wildcards). he gets a list of all the word that match. then he can click one and get its definition.  I want to keep them sorted. when I first imagined this problem I thought I would use a hash map, but if I did that I would have to sort the results before putting them in the choice box. I think using two arrays is the way to go. one for keys(words), and one for values(definitions). does anyone have any other ideas?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LinkedHashMap maintains insertion order should the info be inserted in a sorted manner. There may be other collections similarly suitable. Arrays do not seem like a good approach...
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A TreeMap has ordered keys.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to both of you.
from the link bear provided
"This technique is particularly useful if a module takes a map on input, copies it, and later returns results whose order is determined by that of the copy. (Clients generally appreciate having things returned in the same order they were presented.) "
that was my point.
Bear, I'm curious why you don't like the two array solution.
for me I just want to keep all the data in one container. that's why I asked the question. but you imply the use of two arrays is somehow bad.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parallel arrays were suitable for languages like FORTRAN. In OO languages we have much more cohesive choices.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!