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 Java Data Structures - Retired Organizing Data Comparable

I need some more clarification for comparable. does it only compare user defined objects? Couldn't we use Collections?

I need some more clarification for comparable. does it only compare user defined objects? Couldn't we use Collections?

thanks

com/example/BlogPost.java
package com.example;

import java.util.Date;

public class BlogPost {
  private String mAuthor;
  private String mTitle;
  private String mBody;
  private String mCategory;
  private Date mCreationDate;

  public BlogPost(String author, String title, String body, String category, Date creationDate) {
    mAuthor = author;
    mTitle = title;
    mBody = body;
    mCategory = category;
    mCreationDate = creationDate;
  }

  public String[] getWords() {
    return mBody.split("\\s+");
  }

  public String getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  public String getBody() {
    return mBody;
  }

  public String getCategory() {
    return mCategory;
  }

  public Date getCreationDate() {
    return mCreationDate;
  }
}

1 Answer

Jeremiah Shore
Jeremiah Shore
31,168 Points

It allows custom objects to be comparable. In other words, you are establishing the means by which something is comparable. For example, lets say you have some instances of MyCustomObject, and they each have a member variable of int mSomeAttribute. By default, other objects are going to have no idea what mSomeAttribute means, the number is arbitrary.

When we think of other member variables, like int mPositionInLine, we might naturally think, lower should go first, because first come first serve. If we think of other numbers, like int mHighScore, we might naturally want to sort our objects by highest score first. We are using the appreciated/contextual value to decide, and it is these rules for sorting that we pass along with the Comparable interface. In other words, until you define it, no other object will know how to sort your objects.

Can't we use collections? Yes we can; however, if you have an abstract class of Whatchamacallits and subclasses of Brumpkin and Snark, how exactly does a Collection<Whatchamacallits> know how to sort objects of Brumpkin and Snark in the correct order unless you explicitly define the ways in which they are comparable (and hence can be sorted)?

I hope that helps :)