简体   繁体   中英

Checking if arraylist object exists

Well, my question is this.

My class Message contains: - id - message - [User]

My class User contains: - id - name

This is how I add info to my arrayList: http://pastebin.com/99ZhFASm

I have a arrayList wich contain id, message, User.

I want to know if my arrayList already contain the id of "user"

Note: already tried with arraylist.contains

(Android)

Since you have object Message which has unique identifier ( id ), don't place it in ArrayList , use HashMap or HashSet . But first, you need to create methods equal() and hashCode() in that object:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Message message = (Message) o;

    return id == message.id;

}

@Override
public int hashCode() {
    return id;
}

in this way, you can use advantages of map and set. So, do this:

User user = new User();
user.setId(1);
user.setName("stackover");

Message msg = new Message();
msg.setid(10);
msg.setmessage("hi");
msg.setUser(user);

HashMap<Integer, Message> map = new HashMap<>();                 
map.add(new Integer(msg.getId()), msg);

boolean isItInMapById = map.containsKey(new Integer(10));
boolean isItInMapByObject = map.containsValue(msg);

If you need ArrayList of Messages, just do this:

ArrayList<Message> messages = new ArrayList<>(map.values());

You can also get list of ids if you need it:

List<Set<Integer>> idList = Arrays.asList(map.keySet());
arrayList.stream().anyMatch(item.id == user.id)

If you're using Java 8, you can write code that looks like this:

ID theIdWeAreMatchingAgainst = /*Whatever it is*/;
boolean alreadyHasId = 
    list
    .stream()
    .anyMatch(m -> m.getId() == theIdWeAreMatchingAgainst);

And if you actually need the message[-s] that has that ID,

Message[] msgs = 
    list
    .stream()
    .filter(m -> m.getId() == theIdWeAreMatchingAgainst)
    .toArray(Message[]::new);
Message msg = msgs[0];

If you're using Java 7-, you'll have to do it the old way:

public static List<Message> getMessage(ID id, List<Message> list) {
    List<Message> filtered = new ArrayList<Message>();
    for(Message msg : list) {
        if(msg.getId() == theIdWeAreMatchingAgainst) filtered.add(msg);
    }
    return filtered;
}

So your question and your code don't seem to correspond to one another. You have an ArrayList of Messages, where Messages contains an ID, a message String, and a user Object. You are applying an ID to that Message, as well as another ID to the User. You want to make sure that the ArrayList has a match to the ID, there are two ways of doing this.

You could do something like this

boolean matchMessageId = true;
int idToMatch = [some_id];
for(Message message : arrayList){
    int currId = matchMessageId? mesage.id: message.user.id;

    if(currId == idToMatch){
        return true;
    }
}
return false;

This, however, seems like something that would be better suited for something like a HashMap or SparseArray potentially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM