wilbur4321
wilbur4321

Reputation: 865

Finding an item in an ArrayList in Java?

In C#, finding an item in an ArrayList that have a certain property, it's quite easy:

mSelectedBoard = mBoardConnections.FirstOrDefault(bcd => bcd.Id == id);

This is the easiest I've found to do the same in Java (wish I could afford MonoTouch for android):

for ( BoardConnectionData bcd : mBoardConnections ) {
    if (bcd.getID() == id) {
      mSelectedBoard = bcd;
      break;
    }
}

Am I missing an easier way to do this?

Thanks!

Upvotes: 2

Views: 2612

Answers (8)

Simon HN
Simon HN

Reputation: 21

In Java 8 you can do like this:

mSelectedBoard = mBoardConnections
    .stream().filter(bcd -> bcd.Id == id).findFirst().orElse(null);

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120178

If you need to match a single property, and multiple items in the list can have identical values for that property, then you need to loop thru like you are doing. Java has a Collections utility class with a bunch of static methods; it might make sense for you to define your own collections utility methods to handle cases like this.

In general though, if you wan't to find an element, just use indexOf and be sure to implement equals properly.

Upvotes: 0

Amir Raminfar
Amir Raminfar

Reputation: 34149

You really should ask your self if ArrayList is the right data structure. Given similar task, I would choose a Map. Both C# and Java will need O(N) to search in an array. Using a map is O(1). If this is something you need to often then I would suggest the right structure.

Really the only difference between the C# and Java version is that one is shorter and uses closure. As many other people have suggested, you can do the same thing with some existing libraries. No one actually showed any code though. With Guava, you can do:

Iterables.find(new Predicate<Data>(){
public boolean apply(Data d){
 return d.id == someId;
)}, list);

Or if you want a default value you can use .find(predicate, list, defaultValue).

Upvotes: 0

John
John

Reputation: 1351

To pile on to Dave Newton's answer. The Collection is an ArrayList, so with an appropriate equals (plus hashCode) implementation on BoardConnectionData, the indexOf method would allow the expression

selectedBoard = boardConnections.getAt(boardConnections.indexOf(new BoardConnectionData(id)))

Of course, it's likely preferable to construct a probe object rather than creating a full-blown instance from only the id.

Upvotes: 0

Tudor
Tudor

Reputation: 62439

If you inherit from Comparable to test for the id member and use a SortedSet:

BoardConnectionData brd = 
sortedSet.tailSet(new BoardConnectionData(searchedId)).first();

It is not more efficient complexity-wise, but it is shorter code. :)

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160181

Define a meaningful equals and use Collection.contains.

There are a number of utility libraries that contain implementations using various "am I it?" implementations. (Guava, Commons Collections, etc.)

Another option is to create a typed collection containing utility methods finding elements by arbitrary criteria.

In any case, the code snippet shown shouldn't live in the mainline code, it should be abstracted into its own method, regardless of where it ends up living.

Upvotes: 1

GETah
GETah

Reputation: 21409

Assuming BoardConnectionData class correctly overrides the equals method based on the ID, the following should tell you if the object is in the list or not:

mBoardConnections.contains(object);

Upvotes: 0

solendil
solendil

Reputation: 8468

Since there are no closures in Java, there's no really easier way to do it. Of course, you can use Guava, but IMHO it offers only a bit of conciseness for a lot of complexity (for your example, at last, because Guava is quite cool otherwise)

Upvotes: 0

Related Questions