【问题标题】:Checking if arraylist object exists检查arraylist对象是否存在
【发布时间】:2016-05-04 16:06:48
【问题描述】:

好吧,我的问题是这样的。

我的班级消息包含: - ID - 信息 - [用户]

我的班级用户包含: - ID - 名称

这就是我将信息添加到我的 arrayList 的方式:http://pastebin.com/99ZhFASm

我有一个数组列表,其中包含 id、消息、用户。

我想知道我的arrayList是否已经包含“用户”的id

注意:已经尝试过使用 arraylist.contains

(安卓)

【问题讨论】:

  • 您的arrayList 设置如何?您的列表不能包含不同的对象,除非它们遵循通用模式。是否有一些容器类包含 id、message 和 user 对象?或者你是说你有一个消息数组列表?
  • 这就是我将信息添加到我的数组列表pastebin.com/99ZhFASm

标签: java android arraylist exists


【解决方案1】:

由于您有对象 Message,它具有唯一标识符 (id),因此不要将其放在 ArrayList 中,请使用 HashMapHashSet。但首先,您需要在该对象中创建方法 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;
}

这样,你可以利用map和set的优势。所以,这样做:

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);

如果您需要ArrayList 的消息,只需这样做:

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

如果需要,您还可以获取 id 列表:

List<Set<Integer>> idList = Arrays.asList(map.keySet());

【讨论】:

  • 您好 Adnan,感谢您的回答,但我尝试了 Arrays.asList(arrayList).contains(user) [user 因为我的对象包含用户 ID 和名称] 但不起作用
  • 以哪种方式?没有找到或给出错误(如果是这种情况,请提供堆栈跟踪)?
【解决方案2】:
arrayList.stream().anyMatch(item.id == user.id)

【讨论】:

    【解决方案3】:

    如果您使用的是 Java 8,则可以编写如下所示的代码:

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

    如果您确实需要具有该 ID 的消息 [-s],

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

    如果您使用的是 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;
    }
    

    【讨论】:

      【解决方案4】:

      所以您的问题和您的代码似乎彼此不对应。您有一个 Messages 的 ArrayList,其中 Messages 包含一个 ID、一个消息字符串和一个用户对象。您将一个 ID 应用到该消息,以及另一个 ID 应用到用户。您想确保 ArrayList 与 ID 匹配,有两种方法可以做到这一点。

      你可以这样做

      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;
      

      然而,这似乎更适合 HashMap 或 SparseArray 之类的东西。

      【讨论】:

        猜你喜欢
        • 2012-07-27
        • 1970-01-01
        • 2013-04-22
        • 1970-01-01
        • 2011-05-23
        • 1970-01-01
        • 1970-01-01
        • 2017-05-13
        相关资源
        最近更新 更多