Mono Jamoon
Mono Jamoon

Reputation: 4607

How to remove null bean attributes present in a List?

I am using Spring-WS to populate a List<SomeBean> w.r.t. a web-service. The final list that I get after consuming the service contains null entries for some of its properties. For example, consider the following list of MangaBean which has title, author and genre as properties.

MangaBean [title=Bleach, author=Kubo Tite, genre=fantasy]
MangaBean [title=Naruto, author=Masashi Kishimoto, genre=action]
MangaBean [title=One Piece, author=Eiichiro Oda, genre=null]
MangaBean [title=Fairy Tail, author=Mashima Hiro, genre=null]
MangaBean [title=Rurouni Kenshin, author=Watsuki Nobuhiro, genre=Shounen]
MangaBean [title=Takamagahara, author=KAWAI Juuzou, genre=Supernatural]
MangaBean [title=Historys Strongest Disciple Kenichi, author=Matsuena Syun, genre=Martial arts]
MangaBean [title=Hajime no Ippo, author=Jyoji Morikawa, genre=null]

The genre entry for some beans are null. I am trying to remove the objects with null properties in the list. My questions are:

  1. Is it proper/good practice to alter the original list by removing specific entries? Or should I populate another list w.r.t. the previous list?
  2. If I should alter the list and remove those null entries, how should I do it?

I tried this but it fails as the object-refs themselves aren't null. I tried the traditional for loop but it fails to remove adjucent objects with null properties. Cannot use the enhanced-for-loop for removing items while accessing the list. Kindly render me some brain. :)

Upvotes: 2

Views: 1661

Answers (2)

pickypg
pickypg

Reputation: 22332

There is nothing about removing from a List that is bad practice, unless you cannot trust the source of the List to give you a mutable reference.

If it is mutable (not read-only), then you can use an Iterator:

List<MangaBean> list = ...;
Iterator<MangaBean> iterator = list.iterator();

while (iterator.hasNext())
{
    MangaBean bean = iterator.next();

    if ( ! isValidMangaBean(bean))
    {
        iterator.remove();
    }
}

The limitation presents itself with removing from the List if it is a read-only List. By creating a copy that only contains the non-null elements, you can guarantee that you will not hit any roadblocks, but using the above Iterator will work if the List is not read-only.

List<MangaBean> list = ...;
List<MangaBean> cleaned = new ArrayList<MangaBean>();

for (MangaBean bean : list)
{
    if (isValidMangaBean(bean))
    {
        cleaned.add(bean);
    }
}

Reference function that could be expanded upon:

boolean isValidMangaBean(MangaBean bean)
{
    return bean.getGenre() != null;
}

If you continuously run into the above situation, then consider writing a Filter interface that would allow you to perform the above filtering much more generically.

Upvotes: 2

Alex
Alex

Reputation: 25613

You can use an Iterator and remove the one having a null genre manually:

for (Iterator<MangaBean> it = mangas.iterator(); it.hasNext();) {
    if (it.next().getGenre() == null) { 
        it.remove(); 
    }
}

Or you could filter the list using some common libraries like Apache Commons CollectionUtils, or Guava Iterables.filter():

// apache commons
mangas = CollectionUtils.filter(mangas, new Predicate() {
    @Override public boolean evaluate(Object object) {
        return ((Manga) manga).getGenre() != null;
    }
});

// Guava 
Iterables.filter(mangas, new Predicate<Manga>() {
    @Override public boolean apply(Manga m) {
        return manga.getGenre() != null;
    }
});

Upvotes: 3

Related Questions