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 4 of 4

Thread: Collections vs. Objects

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Collections vs. Objects

    Hi,

    I am new to the forum and new to collections. I am reading up on the basics of collections and trying to understand the difference between collections and objects. Firstly, are collections objects? My understanding is that collections group together similar elements: don't objects/classes do this as well? Could anything done by a collection then just be done by designing a custom class/object? I am guessing the Collections might be more optimized than doing so; but is efficiency the only reason to use collections rather than designing your own data-type/class/object?

    Thanks very much. (Feel free to link me to existing resources about this rather than writing an answer if those are available..)

    -Dan


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Collections vs. Objects

    are collections objects?
    Yes, you can think of a collection as a type of object.

    Can anything done by a collection then just be done by designing a custom class/object?
    Yes, because of the first point that collections are objects.

    I am guessing the Collections might be more optimized than doing so; but is efficiency the only reason to use collections rather than designing your own data-type/class/object
    Someone had to implement collections to begin with, and there's nothing to say that you can't do a better job than that guy did. However, chances are very good that you won't because of another important factor: time. The designer of collections focus countless hours on making them good at what they're suppose to do. Chances are you don't have the same time they have to dedicate to fine-tune your implementation.

    There's also another big factor which is often times more important than raw speed: maintainability. The more code you can re-use, the more maintainable your program is. Often times there's no point in implementing your own collection if one already exists and will suite your purposes.

    edit:

    Most general purpose collection libraries actually aren't faster than a custom implementation designed specifically for a certain situation (assuming both are implemented correctly). The power comes when you don't have to implement 10 different binary trees or a dozen different hash maps, you just have the 1 implementation which can handle most cases well enough.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collections vs. Objects

    Collection is data structure which we can insert different objects in it. For example ArrayList is the collections class which we can insert here different kinds of objects in it. But Object is the one which is single. It can be any kind of object. Simply we say Object is a singular, where Collections are the many kinds of objects. I hope it is usefull answer.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,402
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: Collections vs. Objects

    My understanding is that collections group together similar elements: don't objects/classes do this as well? Could anything done by a collection then just be done by designing a custom class/object?
    Some collections are required to contain data elements of the same type, so from that you could say the elements are similar. But classes are designed to describe things or perform functions that may contain many dissimilar elements, like several different data types. In that way, collections can be very different from other classes. And, no, I don't think ANYTHING can be done by an existing collection that COULD BE DONE by a custom class.

Similar Threads

  1. collections
    By koushik in forum Collections and Generics
    Replies: 2
    Last Post: January 14th, 2012, 11:06 AM
  2. Help needed with Comparator objects and Collections
    By iceyd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 5th, 2011, 01:41 PM
  3. Collections
    By pokuri in forum Collections and Generics
    Replies: 4
    Last Post: June 3rd, 2011, 09:08 AM
  4. collections
    By bardd in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2011, 09:31 AM
  5. how to sort objects with collections???
    By kyros in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 02:21 PM

Tags for this Thread