title: Check if Object exists in ArrayList in Java
body: <p>I have the following List of objects:</p>
  
  <pre><code>private List&lt;Object&gt; teamlist = new ArrayList&lt;Object&gt;();
  </code></pre>
  
  <p>And I'm adding objects to the list like so:</p>
  
  <pre><code>teamlist.add(new MCWarTeam(args[0], joinkey));
  </code></pre>
  
  <p>Now the objects in the list have no name, but can be referenced by using the list, right? Before I add a new element to the list, how can I check if an object with a certain attribute already exists? This is the constructor of the Objects:</p>
  
  <pre><code>public MCWarTeam(String teamname, String joinkey){
      this.teamname = teamname;
      this.joinkey = joinkey;
  }
  </code></pre>
  
  <p>I want to check if there already is a team with the name <em>teamname</em>. Alternatively, is there a better way to store the Objects? Before, I just used a HashMap to add the teamname and joinkey and it worked just fine, but figured using Objects instead would be a better way to do it.</p>
  
  <p>Here is the important code for the event handler:</p>
  
  <pre><code>        else if (cmd.getName().equalsIgnoreCase("createTeam")) {
          if (args.length &gt; 0 &amp;&amp; args.length &lt; 3) {
              String joinkey = "";
              if (args.length &gt; 1)
                  joinkey = args[1];
  
              String teamname = args[0];
  
              MCWarTeam newTeam = new MCWarTeam(teamname, joinkey);
              if (!teamlist.containsKey(teamname)) {
                  teamlist.put(teamname, newTeam);
                  sender.sendMessage("Created new team \"" + teamname + "\" with join key \"" + joinkey + "\" successfully! Teams:");
  
                  sender.sendMessage("All teams:");
                  for (String key : teamlist.keySet()) {
                      sender.sendMessage(key);
                  }
  
              } else
                  sender.sendMessage("Team already exists!");
              return true;
          }
          return false;
      }
  
      else if (cmd.getName().equalsIgnoreCase("joinTeam")) {
          if (args.length &gt; 0 &amp;&amp; args.length &lt; 3) {
              String joinkey = "";
              if (args.length &gt; 1)
                  joinkey = args[1];
  
              String teamname = args[0];
  
              if (teamlist.containsKey(teamname)) {
                  String teamKey = teamlist.get(teamname).getJoinKey();
                  if (joinkey == teamKey) {
                      teamlist.get(teamname).addPlayer(playername);
                      Bukkit.broadcastMessage("MCWar: " + playername + " joined Team \"" + teamname + "\" successfully!");
                  } else
                      sender.sendMessage("Join key incorrect!");
              } else {
                  sender.sendMessage("Team doesn't exist! Teams:");
                  for (String key : teamlist.keySet()) {
                      sender.sendMessage(key);
                  }
  
              }
              return true;
          }
          return false;
      }
  </code></pre>
  
  <p>Basically, if it returns false, the user will get a message explaining the correct usage of the command he entered.</p>