HashMap - 搜索对象的值并添加到列表中

时间:2017-05-02 20:45:49

标签: java hashmap

我有2个班级健身房和健身房成员。目前,Gym将所有gymMember存储在HashMap gymMembers中,其中String是其成员ID(String)。

gymMember包含变量:

  this.fullName = aName;
  this.address = anAddress;
  this.area = anArea;

我想去的是搜索HashMap gymMembers,找出变量this.area中区域1商店中的成员,然后将gymMember对象返回到新的集合。

到目前为止,我已经走到了这一步:

public void findCustomersInArea(int x)
{
    // create a list to store all Customers
List<gymMember> aMember = new ArrayList<gymMember>();

// iterate all entries in the map
for (Map.Entry<String, gymMember> a: gymMembers.entrySet())
{
    // get the customer
    gymMember newMember = a.getArea();
    if (newMember.getArea() == x) {
        // do what you need to add to a list
       newMember.add(newCustomer);
    }
}
}

有没有人有我出错的想法?​​

2 个答案:

答案 0 :(得分:1)

gymMember newMember = a.getValue ();
if (newMember.getArea() == x) {
    // do what you need to add to a list
   aMember.add(newMember);
}

答案 1 :(得分:1)

在迭代 public async Task StartAsync(IDialogContext context) { await context.PostAsync(ConversationHelper.CreateReschedulePromptMessage()); context.Wait(MessageReceivedAsync); } public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result) { var message = await result; var Options = new[] { "Location", "Date and Time", "Both" }; if (message.Text.ToUpper().CompareTo("PICKUP") == 0) { _rescheduleType = "pickup"; string prompt = string.Format("Is the {0} location incorrect, is the date and time incorrect, or both?", _rescheduleType); PromptDialog.Choice(context, OnResumeFromRescheduleChoice, Options, prompt, promptStyle: PromptStyle.Auto, descriptions: Options); } else if (message.Text.ToUpper().CompareTo("DROP") == 0) { _rescheduleType = "drop-off"; string prompt = string.Format("Is the {0} location incorrect, is the date and time incorrect, or both?", _rescheduleType); PromptDialog.Choice(context, OnResumeFromRescheduleChoice, Options, prompt, promptStyle: PromptStyle.Auto, descriptions: Options); } else { await context.PostAsync(ConversationHelper.CreateGenericRescheduleMessage(SUPPORTNUMBER)); } context.Done<object>(null); } private async Task OnResumeFromRescheduleChoice(IDialogContext context, IAwaitable<string> result) { var choice = await result; } 时,而不是Map,你应该这样做:

gymMember newMember = a.getArea();以下是您的代码:

gymMember newMember = a.getValue();

因为您要检查 for (Map.Entry<String, gymMember> a : gymMembers.entrySet()) { // get the customer gymMember newMember = a.getValue(); if (newMember.getArea() == x) { // do what you need to add to a list aMember.add(newCustomer);//The name of your list is aMember and not newMember } } 中每个area的{​​{1}}而不是gymMember area。在这种情况下,由于您只是访问与每个id相关联的值(即entry),您可以这样做:

map