ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am fairly new to programming, and am writing a simple GUI address book in java. (This is _not_ any kind of homework problem, just for fun).
I have a simple class called Entry.java, which describes each entry into the address book - name, number, address, comments.
In the main class, Address.java, the GUI is drawn, and action events such as "Add entry", "Delete entry" etc. are handled.
The GUI uses tabs to move between different functions of the address book (as in add, delete, search). The first tab displays the entries by first name, last name, and number, a-c, d-f, etc, each again tabbed (sounds a bit confusing, but is actually easy to work with).
I am currently using java's TreeMap to hold all the entries, because they will be ordered, and give fast look up times. I need them to be ordered so that the panel the panel that displays the lists of names, can simply iterate through each name and decide on which subpanel to draw it to (m-0, or x-z for example).
My question is - how might I handle duplicate entries. I am now using the first name as a key to the TreeMap, so they can be ordered by first name. I need to be able to handle mulitple identical first names. If I map the entries using, say, the phone number, I could have multiple identical first names, but then they won't be in the order.
I think I need use a different data structure, say an ArrayList, and then when I want the panel to display the names in order, dump the ArrayList into something else that can order them. But not sure how I could do this - short of write my own implementation of ArrayList and add an sort() method.
Well, from what I can tell in the api is that Hashtable implements the Map interface, which doesn't allow duplicates. And I need both duplicates and sorting. I would like to be able to have two identical entries in to the Address book.
So using an Array or Linked list would allow the duplicates, but then I'd have to sort them some other way.
Using a TreeMap would sort them as I entered them, but not allow duplicates.
Using some Hashing implementation wouldn't let me do either.
That's why I'm thinking ArrayList, and then have a sorting function built in. So each time I added an entry, I would just call my sort() method on it right after.
Please correct me if my understanding is off - still new to this.
Thanks again.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.