我可以获得具有相同密钥的HashMap元素的所有值吗? (JAVA) [英] Can I get all the values of HashMap elements, having the same keys? (Java)

查看:108
本文介绍了我可以获得具有相同密钥的HashMap元素的所有值吗? (JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java编程课程上给了我一项任务。根据这个任务,我必须创建一个方法,该方法返回一些具有相同键的HashMap元素。但问题是迭代器只通过具有不同键的元素,所以无论如何,该方法返回1.什么是出路?

  package com.javarush.test.level08.lesson08.task03; 

import java.util。*;

$ * b $ *
Создатьсловарь(Map< String,String>)занестивнегодесятьзаписейпопринципу«Фамилия» - «Имя»。
Проверитьскольколюдейимеютсовпадающиесзаданнымимяилифамилию。
* /

公共类解决方案
{
public static void main(String [] args)
{
HashMap< String,String> ; friends = createMap();
getCountTheSameLastName(friends,гладких);
getCountTheSameFirstName(朋友,Виталий);
}

public static HashMap< String,String> createMap()
{
HashMap< String,String> name = new HashMap< String,String>();

name.put(гладких,Иван);
name.put(пересыпкин,Артем);
name.put(пересыпкин,Владислав);
name.put(халитов,Виталий);
name.put(чернышев,Виталий);
name.put(ивинских,Виталий);
name.put(ивинских,Альфред);
name.put(осипова,Мария);
name.put(ивинских,Павел);
name.put(гейтс,Билл);

返回名称;

$ b $ public static int getCountTheSameFirstName(HashMap< String,String> map,String name)
{
int MatchesFirstnameCount = 0; (HashMap.Entry< String,String> pair:map.entrySet()){
String s = pair.getValue();


if(s.equals(name)){
MatchesFirstnameCount ++;
}
}

返回MatchesFirstnameCount;

$ b $ public static int getCountTheSameLastName(HashMap< String,String> map,String lastName)
{
int MatchesSecondnameCount = 0;

(HashMap.Entry< String,String> pair:map.entrySet()){
if(pair.getKey()。equals(lastName))
MatchesSecondnameCount ++;
}
返回MatchesSecondnameCount;


$ / code>


解决方案

听起来像个诡计的问题。 HashMap中的每个键只能有一个与其关联的值。 HashMap 中的每个键必须是唯一的。

添加重复键时,旧值会被替换(请参阅 HashMap.put


I have a task given me on the Java programming course. According to this task I have to create a method that returns a number of the HashMap elements with identical keys. But the problem is that the iterator goes through elements with different keys only, so anyway, the method returns 1. What`s the way out?

package com.javarush.test.level08.lesson08.task03;

import java.util.*;

/* Одинаковые имя и фамилия
Создать словарь (Map<String, String>) занести в него десять записей по   принципу «Фамилия» - «Имя».
Проверить сколько людей имеют совпадающие с заданным имя или фамилию.
*/

public class Solution
{
    public static void main(String[] args)
    {
        HashMap<String, String> friends = createMap();
        getCountTheSameLastName(friends, "гладких");
        getCountTheSameFirstName(friends, "Виталий");
    }

    public static HashMap<String, String> createMap()
    {
        HashMap<String, String> name = new HashMap<String, String>();

        name.put("гладких", "Иван");
        name.put("пересыпкин", "Артем");
        name.put("пересыпкин", "Владислав");
        name.put("халитов", "Виталий");
        name.put("чернышев", "Виталий");
        name.put("ивинских", "Виталий");
        name.put("ивинских", "Альфред");
        name.put("осипова", "Мария");
        name.put("ивинских", "Павел");
        name.put("гейтс", "Билл");

        return name;
    }

    public static int getCountTheSameFirstName(HashMap<String, String>         map, String name)
    {
        int MatchesFirstnameCount = 0;

        for (HashMap.Entry<String, String> pair : map.entrySet()) {
            String s = pair.getValue();
            if (s.equals(name) ) {
                MatchesFirstnameCount++;
            }
        }

        return MatchesFirstnameCount;
    }

    public static int getCountTheSameLastName(HashMap<String, String>     map, String lastName)
    {
        int MatchesSecondnameCount = 0;

        for (HashMap.Entry<String, String> pair : map.entrySet()) {
            if (pair.getKey().equals(lastName))
                MatchesSecondnameCount++;
        }
        return MatchesSecondnameCount;
    }
}

解决方案

This sounds like a trick question. Each key in a HashMap can only have one value associated with it. Each key in a HashMap must be unique.

When adding a duplicate key the old value is replaced (see HashMap.put)

这篇关于我可以获得具有相同密钥的HashMap元素的所有值吗? (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆