[英]Checking if arraylist object exists
Well, my question is this. 好吧,我的问题是这个。
My class Message contains: - id - message - [User]
我的班级消息包含:-id-消息-[用户]
My class User contains: - id - name
我的班级用户包含:-id-名称
This is how I add info to my arrayList: http://pastebin.com/99ZhFASm 这是将信息添加到arrayList的方式: http ://pastebin.com/99ZhFASm
I have a arrayList wich contain id, message, User. 我有一个包含ID,消息,用户的arrayList。
I want to know if my arrayList already contain the id of "user" 我想知道我的arrayList是否已经包含“用户”的ID
Note: already tried with arraylist.contains 注意:已经尝试使用arraylist.contains
(Android) (Android)
Since you have object Message
which has unique identifier ( id
), don't place it in ArrayList
, use HashMap
or HashSet
. 由于您有对象
Message
具有唯一的标识符( id
),因此请不要将其放在ArrayList
,请使用HashMap
或HashSet
。 But first, you need to create methods equal() and hashCode() in that object: 但是首先,您需要在该对象中创建equal()和hashCode()方法:
@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
,只需执行以下操作:
ArrayList<Message> messages = new ArrayList<>(map.values());
You can also get list of ids if you need it: 如果需要,您还可以获取ID列表:
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: 如果您使用的是Java 8,则可以编写如下代码:
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, 如果您确实需要具有该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: 如果您使用的是Java 7-,则必须采用旧方法:
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.
您具有消息的ArrayList,其中消息包含ID,消息字符串和用户对象。 You are applying an ID to that Message, as well as another ID to the User.
您正在为该消息应用一个ID,并为用户应用另一个ID。 You want to make sure that the ArrayList has a match to the ID, there are two ways of doing this.
您要确保ArrayList与ID匹配,可以通过两种方法进行。
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. 但是,这似乎更适合于HashMap或SparseArray之类的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.