[英]How to get in to an Array of values in Hash Map?
我正在学习 Java,所以这可能是一个简单的问题。
我有一个哈希图:
private final HashMap<String, String[]> hobbies= new HashMap<String, String[]>();
并且其中只有一个键和几个值的条目很少。 在我的任务中,我需要创建一个方法,该方法将使用这些数组中的指向值的每个键带到另一个列表
示例(如果键“Adam”包含值“Yoga”,请将其添加到列表中)
我的问题是我不知道如何进入该值数组进行比较。
下面是我的代码。 我知道这是错误的以及为什么,但我不知道下一步该怎么做。
package com.company;
import java.util.*;
public class Hobbies {
private final HashMap<String, String[]> hobbies = new HashMap<String, String[]>();
public void add(String hobbyist, String... hobbies) {
this.hobbies.put(hobbyist,hobbies);
}
//Implement method that returns a list of names of people with the selected hobby in any order
public List<String> findAllHobbyists(String hobby){
LinkedList<String> finalMap = new LinkedList<>();
for(int i=0; i<hobbies.size();i++){
if(hobbies.values().toArray()[0].equals(hobby)){
finalMap.add(hobbies.keySet().toArray()[i].toString());
}
}
return finalMap;
}
public static void main(String[] args) {
Hobbies hobbies = new Hobbies();
hobbies.add("Steve","Fashion","Piano","Reading");
hobbies.add("Patty","Drama","Magic","Pets");
hobbies.add("Chad","Puzzles","Pets","Yoga");
System.out.println(Arrays.toString(hobbies.findAllHobbyists("Yoga").toArray()));
}
}
我的问题是我不知道如何进入该值数组进行比较。
假设这是你的问题。 方法values()就是你要找的。 您还可以检查方法keySet()
以查看它是否可以帮助您完成手头的任务。
您还应该在您正在编辑代码的 IDE 中查找自动格式化。更小、更简洁的代码可以让您在 Stack Overflow 中更快、更具体地获得答案。
试试下面的代码:
package com.company;
import java.util.*;
public class Hobbies {
private final HashMap<String, String[]> hobbies = new HashMap<String, String[]>();
public void add(String hobbyist, String... hobbies) {
this.hobbies.put(hobbyist, hobbies);
}
//Implement method that returns a list of names of people with the selected hobby in any order
public List<String> findAllHobbyists(String hobby) {
LinkedList<String> finalList = new LinkedList<>();
Set<Map.Entry<String, String[]>> entries = hobbies.entrySet();
for (Map.Entry<String, String[]> entry : entries) {
if (isPresent(entry.getValue(), hobby))
finalList.add(entry.getKey());
}
return finalList;
}
private boolean isPresent(String[] array, String value) {
for (String s : array) {
if (s.equals(value))
return true;
}
return false;
}
public static void main(String[] args) {
Hobbies hobbies = new Hobbies();
hobbies.add("Steve", "Fashion", "Piano", "Reading");
hobbies.add("Patty", "Drama", "Magic", "Pets");
hobbies.add("Chad", "Puzzles", "Pets", "Yoga");
System.out.println(Arrays.toString(hobbies.findAllHobbyists("Yoga").toArray()));
}
}
要阅读有关 HashMap 的更多信息,请单击此处
请看下面的代码片段。 希望它能让您了解如何访问存储为哈希映射中的值的字符串数组
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
String[] val = {"Yoga", "Eat", "Sleep"};
HashMap<String, String[]> hobbies= new HashMap<String, String[]>();
hobbies.put("Adam", val);
String[] vals = hobbies.get("Adam");
System.out.println(vals[0]);
}
}
您可以使用name
和hobbies
属性定义 Person 对象
public class Person {
String name;
List<String> hobbies;
// getter and setters
}
创建 Person 对象列表
//create Person object and set name and hoobies
Person p1 = new Person();
List<String> steveHobbies = new ArrayList<>();
steveHobbies.add("Fashion");
steveHobbies.add("Piano");
steveHobbies.add("Reading");
p1.setName("steve");
p1.setHobbies(steveHobbies);
Person p2 = new Person();
List<String> pattyHobbies = new ArrayList<>();
pattyHobbies.add("Drama");
pattyHobbies.add("Magic");
pattyHobbies.add("Pets");
p2.setName("patty");
p2.setHobbies(pattyHobbies);
Person p3 = new Person();
List<String> chadHobbies = new ArrayList<>();
chadHobbies.add("Puzzles");
chadHobbies.add("Pets");
chadHobbies.add("Yoga");
p3.setName("chad");
p3.setHobbies(chadHobbies);
// Add persons to list
List<Person> persons = new ArrayList<>();
persons.add(p1);
persons.add(p2);
persons.add(p3);
使用列表上的流,过滤具有所需爱好的人
persons.stream().filter(p -> p.getHobbies().contains("Yoga")).forEach(p -> System.out.println("Person with matching Hobby:"+p.getName()));
输出:
Person with matching Hobby:chad
要查找以Pets
为爱好的人,您可以将contains("Yoga")
更改为contains("Pets")
输出:
Person with matching Hobby:patty
Person with matching Hobby:chad
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.