Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Introducing Lambdas

Joe Di Trolio
Joe Di Trolio
3,367 Points

Confirming my understanding on Collections

Hi there,

Thank you for a helpful introduction so far into lambdas! However, I'm not totally clear on a few aspects of the Collections object and have tried to break things down.

A Collections is a framework object that contains "ready-bake" code which allows us to locally sort a group of objects. We are then calling its inbuilt "sort" method and passing the books list and a Comparator object into its' parameters.

A Comparator is an interface that also contains lots of inbuilt code and here we are overriding its' only abstract method, "compare", This method allows us compare two book titles by returning ints to the Collections object that then knows how to sort the books alphabetically .

There is a lot of complex code behind the scenes and it's not important to know how it works, what's important is understanding the result.

It would be super appreciated if you could confirm that I'm on track here before I move on. Furthermore, how exactly does the Collections/Comparator object know to sort alphabetically? Or is that once again built into the invisible, behind the scene, code and the default setting when comparing two Strings?


2 Answers

Craig Dennis
Craig Dennis
Treehouse Teacher

You are on the right track! Have you watched Java Data Structures yet? It's a biggie.

The compareTo method returns an int. It is required to be overridden by anything that implements Comparable.

Hypothetically: The sort method literally compares each one, to the next one, one pair at a time and finds out which is greater using the compareTo method. If the one below the one being checked is < then it swaps them and continues through.

The sorting can be done by any number of algorithms (bubble, quick, etc.), Java uses Timsort currently, but might change. The idea is as long as you can tell it how two objects can be compared, it can handle the sorting needs.

That help?

Hi Sir I also want to confirm my understanding of Collection Framework

  1. Collection in java - Represents single unit of objects

1.What I understand is that multiple objects grouped in to one thing ie. Collection

  1. Framework in Java - Provides ready made architecture represents set of classes and interface

  2. first point ? What is actually a framework second point - If it is group of classes and interfaces will it be not called Library? on internet they say The java.util package contains all the classes and interfaces for Collection famework?. So what is framework

3.Collection Framework - Represents a unified architecture for storing and manipulating group of objects. Interfaces and its implementations i.e. classes Algorithm 3.So combing above points I think that they created Collection Framework which has many classes and interfaces like List Queue Set Map , All these uses different Algorithms to Set Delete Iterate Sort a Collection ie. Group of Objects and as they have different Algorithms they have different time complexity therefore we sometime uses List<> temp = new ArrayList<> so that we can go to other child's of List and choose better time complexity fucntion so we are not bounded to ArrayList's function n all.

Now when we are using ArrayList (Class) which implement List (Interface) . now if we have to sort the ArrayList we use Comparable interface ie. defined in Collection Framework this Comparable interface has compareTo(Object obj) method which we have to implement in order to sort the objects so it means ArrayList must implement Comparable but if we goto https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html we dont see ArratList in All Known Implementing Classes??

Craig Dennis
Craig Dennis
Treehouse Teacher

Hi Apurva Trehan !

Can you make a new post and tag me in it please? The comment thread here is going to get weird if we don't start a new one since this is a new question ;)

Your question about Comparable can be found in the Collections.sort documentation ...

Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

The elements of the list must implement the Comparable interface.

Hope that helps,( and tag me by placing the @ sign followed by craigsdennis)

Joe Di Trolio
Joe Di Trolio
3,367 Points

Helps! Thanks for explanation Craig! Really appreciate the support and videos.