• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Storing elements in array list or hash map

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
my code goes like this:

im just wondering what should i use to store my elements?an array list or hash map?also could i put the storing element in the constructor code of book?like in my book.java?

and just a side question, whats happening at the moment in the constructor since its not storing it anywhere?is it creating an object at all?

Thanks for any help

Brendan
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where you store them should be determined by how you are going to use the collection. Do you need to be able to access them via a hash? Or do you need to be able to access the 17th one? Once you determine your application's needs you can decide which collection works best.

also could i put the storing element in the constructor code of book?


I wouldn't. your code should mimic real life (to a degree). It is possible for a book to exist outside of a library collection. Further, a book shouldn't know anything about a library, and it is possible that there is more than one library, so how would your constructor know which library to add it to... Granted these are not insurmountable problems, but they do give one pause.

whats happening at the moment in the constructor since its not storing it anywhere?is it creating an object at all?


Any time you see a "new ClassName", and object is created. and you ARE saving it. For example, on line 34 of your library class, you save the book as newBook. However, two lines later, that variable goes out of scope, so the object becomes eligible for garbage collection.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object is created by the JVM when it executes the new operator. Then it calls the constructor, using whatever information you have provided (in the form of arguments). You should make sure nothing can happen to your object before the constructor finishes its work. So everything in the constructor should be used for setting up the object’s state (or debugging).
Do you really want a no-arguments constructor? That allows you to set up a book with blank title. I don’t think that models real life.
Why have you got the isAvailable field? You appear not to use it anywhere.

As for maps versus lists, you should look at their uses in the Java™ Tutorials; you will find more if you scroll down to the implementations page with the “next” link. Lists and Maps have different uses because in one case you link K and V objects together. You have to work out what the K and what the V would be, and then you can find Maps very useful, and not difficult to use.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding library information to a book constructor is hazardous. A library has a book much more than a book has a library. You can incorrectly allow information about the book to escape if you do that.
Also don’t start an int literal with a 0. 01012011 does not print out as 1012011.
Beware of Console; if you start your application from a jar with javaw, you won’t get a Console object and you will suffer exceptions.
 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thanks for the reply, as regards my implementation, i just need to list books that are available through the user of a boolean (isAvailable), and i just need to check them in and check them out to a user!
would an array list be ok for that functionality?I just wanted to sort out all the basic items i needed before implementation.i think i have all the elements i need now, just need to sort out the storing of the data

Thanks again

Brendan
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brendan Cregan wrote:Hi,
thanks for the reply, as regards my implementation, i just need to list books that are available through the user of a boolean (isAvailable), and i just need to check them in and check them out to a user!
would an array list be ok for that functionality?



That describes what you'll do with the Book after you've found it. The distinction between List and Map would be made before that, based on how you want to find it. So, as somebody already suggested, in the pile of books, are you going to be trying to retrieve the 11th book, or are you going to be trying to retrieve the book titled "Effective Java"? And do you understand how the answer to that question relates to whether you want a List or a Map?
 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Brendan Cregan wrote:

That describes what you'll do with the Book after you've found it. The distinction between List and Map would be made before that, based on how you want to find it. So, as somebody already suggested, in the pile of books, are you going to be trying to retrieve the 11th book, or are you going to be trying to retrieve the book titled "Effective Java"? And do you understand how the answer to that question relates to whether you want a List or a Map?



Hi Again Jeff,
would it not be possible for me to use the idea of getting the 11th book as a book id?i don't need to search by book name just to return books of a certain category i.e. isAvailable?the specific functionality im aiming for is

list items by
all items in the library (available and not available)
all items by category (books, periodicals, dvds)
all available items in a category (e.g. all books that are not on loan)

Thanks again

Brendan
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brendan Cregan wrote:

Jeff Verdegan wrote:

Brendan Cregan wrote:

That describes what you'll do with the Book after you've found it. The distinction between List and Map would be made before that, based on how you want to find it. So, as somebody already suggested, in the pile of books, are you going to be trying to retrieve the 11th book, or are you going to be trying to retrieve the book titled "Effective Java"? And do you understand how the answer to that question relates to whether you want a List or a Map?



Hi Again Jeff,
would it not be possible for me to use the idea of getting the 11th book as a book id?



Well, yes, it's certainly possible. But that's not the point. The point is, how do you want to find that book? What do your requirements say? Certainly if I were to walk into a library, I would be much more likely to ask the librarian for "Effective Java" than for "the 11th book on the 3rd shelf to the left."

i don't need to search by book name just to return books of a certain category i.e. isAvailable?the specific functionality im aiming for is

list items by
all items in the library (available and not available)
all items by category (books, periodicals, dvds)
all available items in a category (e.g. all books that are not on loan)



Now it's getting a bit more complex. In general, I would not want to do those kinds of queries just on a List. However, unless this is an assignment specifically intended to teach about data structures and algorithms for that kind of retrieval, I'd say you're better off just using a List here. If you did go the Map route, to really make use of it, you'd need 3 Maps for the above, and the values for those Maps would have to be Lists of Books, not just individual Books.
 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,

When you say list do you mean the arraylist that i was talking about?sorry just want to have it clear in my own mind

Thanks again for the help

Brendan
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brendan Cregan wrote:Hi Jeff,

When you say list do you mean the arraylist that i was talking about?sorry just want to have it clear in my own mind

Thanks again for the help

Brendan



I mean any implementation of the List interface. ArrayList is the usual default go-to List implementation. If you're selecting books by index, than that would be a reason to choose ArrayList, as its random access is O(1), whereas for LinkedList, it's O(n). On the other hand, if you're going to be doing a lot of adding or removing anywhere but at the end of the list, the LinkedList might be a more appropriate choice.
 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Brendan Cregan wrote:Hi Jeff,

When you say list do you mean the arraylist that i was talking about?sorry just want to have it clear in my own mind

Thanks again for the help

Brendan



I mean any implementation of the List interface. ArrayList is the usual default go-to List implementation. If you're selecting books by index, than that would be a reason to choose ArrayList, as its random access is O(1), whereas for LinkedList, it's O(n). On the other hand, if you're going to be doing a lot of adding or removing anywhere but at the end of the list, the LinkedList might be a more appropriate choice.



Thanks Jeff,

This helps alot,your becoming a bit of a hero!haha!Im sure i'll encounter more problems along the way but this gives me a solid foundation!

Thanks again

Brendan
 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
im having trouble implementing my arraylist, i think it could be because it has multiple data types(int and string).Is that allowed in an arraylist?getting conflicting answers in my searches.
this is how i created it

and this is how i tried to add in a book

getting a strange compiling error i havent seen before as well

"Note: Library.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details."
Any ideas?

Thanks

Brendan
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brendan Cregan wrote:Hi,
im having trouble implementing my arraylist, i think it could be because it has multiple data types(int and string).



Why would you want to do that? A Collection (such as an ArrayList) should contain one type.

You can certainly declare it to hold Object, but that's a hack, and suggests a design flaw.

If these two values go together as part of one thing--like person's name and his height--then they should be encapsulated in one class--say a Person class, with a String name variable and an int height variable--and then you have a list of that type, List<Person> in this example.


Is that allowed in an arraylist?getting conflicting answers in my searches.
this is how i created it

and this is how i tried to add in a book



So you're not adding ints and Strings. You're adding Books. That is the right approach.

getting a strange compiling error i havent seen before as well

"Note: Library.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details."
Any ideas?



That's not an error. It's a warning. It means you're doing something that, while syntactically legal, could lead to logic errors. In this case, it's warning you that you're not using generics. Your code will still compile and produce a usable .class file, but the compiler is telling you to double check what you did and make sure you meant to do it that way and that you understand the potential problems.



Google for Java generics tutorial for more information.
 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhhh, that makes sense!
Thanks Again Jeff


Brendan
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brendan Cregan wrote:Ahhhh, that makes sense!
Thanks Again Jeff


Brendan



You're quite welcome.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic