Basic usage of toString() method of Object class and contains() method of list with example.

Did you just stumble upon this post???  we want to shout out loud that this post is for beginners, it shows the basic usage and if you find any mistakes don’t forget to shout back at us 🙂

First we will show you the basic usage of the overridden toString() found inside the Object class. You will find a lot of tutorials demonstrating the toString() functionality but nothing from the practical point of view.

Lets see an example from the application perspective such as you have a User class and you want to display the list of User inside a listview.

<code>

class User{

public String name;

public User(String name){

this.name = name;

}

}

</code>

Then inside your fragment/activity you would mostly populate the listview right… lets do that.

<code>

List<User> userList = new ArrayList<User>();

userList.add(new User(“abc”));

userList.add(new User(“cde”));

userList.add(new User(“efg”));

userList.add(new User(“xyz”));

ArrayAdapter mAdapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, userList);

setListAdapter(mAdapter); or  listview.setAdapter(mAdapter);

</code>

So we’re done setting the listview and the adapter. If you run the code at this point to check the output you would sure see some values but not the names.

Ex: packageName.className@hexaDecimalValueOfTheObject. as seen in the below screen-shot.

without_tostring

So what really happened here…all classes inherit the Object class indirectly/directly. The toString() method for class Object returns a string consisting name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object.

We don’t want to create a custom adapter or override the getView() method for such a simple listview.

A simple solution to this would be by overriding the toString() method inside your User class. So your new User class will look like this.

<code>

class User{

public User(String name){

this.name = name;

}

@Override

public String toString() {

return name;

}

}

</code>

Run the code to check the output. Wola you have the names in the listview 🙂

with_tostring



Let’s look at another interesting method that is java.util.List.contains, used very rarely and when used you may implement it wrongly. The contains() method works fine for the primitive data types but when it comes to custom data types it gets a bit tricky.

Lets look at an example,

We will use the same User class to demonstrate the use of contains() method. Now you want to check whether the user “efg” exists within the list or whether your list contains the name “efg”.

<code>

List<User> userList = new ArrayList<User>();

userList.add(new User(“abc”));

userList.add(new User(“cde”));

userList.add(new User(“efg”));

userList.add(new User(“xyz”));

userList.contains(new User(“efg”));

</code>

The contains() method returns a boolean value. The above code will return false though the list contains the value. In Order to work you need to override the equals() method inside the User class.

Example:

<code>

public class User {

public String name;

public User(String name){

this.name = name;

}

@Override

public String toString() {

return name;

}

@Override

public boolean equals(Object object) {

User model = (User) object;

if (model.name.equalsIgnoreCase(this.name) && object.getClass() == getClass())

return true;

return false;

}

}

</code>

Now userList.contains(new User(“efg”)); would return true. Important point to note inside equals() method is that you need to cast an object to the underlying class type and also both the object and underlying class has to be of the same type, else it won’t work.

Advanced level users can check our post on optimizing the listview here.

You can also find the attached sample project here.

Happy coding happy learning 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s