从包含对象集的映射中检索值

时间:2018-04-30 14:37:48

标签: set

首先,我不太喜欢这个java百灵鸟,所以请耐心等待......

我有一张地图,其中的键是表示电影标题的字符串,值是演员的集合,由Actor类的对象表示,每个对象都有自己的属性。

我想做的是迭代地图,以有意义的方式返回电影片名和演员。

我已经获得了地图迭代部分,但我无法弄清楚如何进行下一个循环,从而产生一组演员的值。

以下是我所拥有的:

Map <String, Set> filmCollection = new HashMap<>();
Actor a1 = new Actor("Joe Smith", "20071977");
Actor a2 = new Actor("Kate Jones", "01011980");
Actor a3 = new Actor("Frank DeFrank", "02021945");
Set <Actor> s1 = new HashSet<>();
Set <Actor> s2 = new HashSet<>();
s1.add(a1);
s1.add(a2);
s2.add(a3);
s2.add(a1);
filmCollection.put("The Best Film", s1);
filmCollection.put("The Nearly Best Film", s2);

Set<String> collectionKeys = filmCollection.keySet();
for (String eachFilm : collectionKeys)
{
    System.out.println(eachFilm + " stars the actors ");
}

我知道这是基本的东西,可能很简单,但我在学习,而且我一整天都在搜索和尝试 - 我的脑袋感觉很快就会爆炸!任何指导都将不胜感激。

5 个答案:

答案 0 :(得分:1)

所以你所做的就是循环遍历地图的一组键。由于您有密钥,现在需要获取存储在地图中的每个密钥的值。为此,您必须致电filmCollection.get(eachFilm)

完整代码

Map <String, Set<Actor>> filmCollection = new HashMap<String, Set<Actor>>();
Actor a1 = new Actor("Joe Smith", "20071977");
Actor a2 = new Actor("Kate Jones", "01011980");
Actor a3 = new Actor("Frank DeFrank", "02021945");
Set <Actor> s1 = new HashSet<>();
Set <Actor> s2 = new HashSet<>();
s1.add(a1);
s1.add(a2);
s2.add(a3);
s2.add(a1);
filmCollection.put("The Best Film", s1);
filmCollection.put("The Nearly Best Film", s2);

Set<String> collectionKeys = filmCollection.keySet();
for (String eachFilm : collectionKeys)
{
    System.out.println(eachFilm + " stars the actors ");
    for (Actor actor : filmCollection.get(eachFilm)) {
        System.out.println(actor + ", ");
    }
}

答案 1 :(得分:0)

如果我理解你,你就会缺少一个循环:第一个循环在电影上,你应该为他们的演员另外一个。如果您同时需要密钥和值,请使用entrySet代替keySet。我稍微调整了你的代码,向你展示我的意思:

    Map <String, Set<Actor>> filmCollection = new HashMap<>();
    Actor a1 = new Actor("Joe Smith", "20071977");
    Actor a2 = new Actor("Kate Jones", "01011980");
    Actor a3 = new Actor("Frank DeFrank", "02021945");
    Set <Actor> s1 = new HashSet<>();
    Set <Actor> s2 = new HashSet<>();
    s1.add(a1);
    s1.add(a2);
    s2.add(a3);
    s2.add(a1);
    filmCollection.put("The Best Film", s1);
    filmCollection.put("The Nearly Best Film", s2);

    for (Entry<String, Set<Actor>> eachFilm : filmCollection.entrySet()) {
        // The key -> a movie
        System.out.println(eachFilm.getKey() + " stars the actors "); 

        // The value -> the actors
        for (Actor actor : eachFilm.getValue()) {
         System.out.println(actor);
        }
    }

答案 2 :(得分:0)

您的问题是密钥集不包含有关actor的任何信息。键集只包含键。

您可以遍历关键的地图条目 - &gt;价值对。获得演员集后,可以将其名称与rendering

连接起来
for (Map.Entry<String, Set<Actor>> film : filmCollection.entrySet())
{
    // get all the names for this film (I would usually use streams for this, but 
    // they're a bit more advanced)
    Set<String> actorsNames = new HashSet<>();
    for (Actor actor : film.getValue())
    {
        actorsNames.add(actor.getName());
    }

    // concatenate the names with String.join
    System.out.println(
        film.getKey() + " stars the actors " + String.join(", ", actorsNames)
    );
}

您还需要将地图的定义更改为:

Map<String, Set<Actor>> filmCollection = new HashMap<>();

因为这表明该集合是一组参与者。没有它,它只是一组对象。

对于我如何使用流来做这件事的好处:

filmCollection.entrySet().stream()
    .map(film -> film.getKey() + " stars the actors "
        + film.getValue().stream().map(Actor::getName).collect(Collectors.joining(", "))
    )
    .forEach(System.out::println);

答案 3 :(得分:0)

既然您正在使用电影和演员,那么只需迭代电影到演员地图的条目,因为条目包含键和值。然后,对于每个条目,打印电影的名称(entry.getKey())并获取一组演员(entry.getValue())并打印每一个。

Map <String, Set<String>> filmCollection = new HashMap<>();
// ...
for (Map.Entry<String, Set<String>> entry : filmCollection.entrySet()){
    System.out.println(entry.key() + " stars the actors " + entry.value());
    // or
    for(String actor : entry.value()){
        System.out.println(actor);
    }
}

答案 4 :(得分:0)

你去了: 您的filmCollection应为Map<String, Set<Actor>>

Map<String, Set<Actor>> filmCollection = new HashMap<>();
Actor a1 = new Actor("Joe Smith", "20071977");
Actor a2 = new Actor("Kate Jones", "01011980");
Actor a3 = new Actor("Frank DeFrank", "02021945");
Set<Actor> s1 = new HashSet<>();
Set<Actor> s2 = new HashSet<>();
s1.add(a1);
s1.add(a2);
s2.add(a3);
s2.add(a1);
filmCollection.put("The Best Film", s1);
filmCollection.put("The Nearly Best Film", s2);

Set<String> collectionKeys = filmCollection.keySet();
for (String eachFilm : collectionKeys) {
    System.out.print(eachFilm + " stars the actors : ");
    for (Actor actor : filmCollection.get(eachFilm)) {
        System.out.print(" [ " + actor.name + " ] ");
    }
    System.out.println();

示例输出:

The Best Film stars the actors :  [ Kate Jones ]  [ Joe Smith ] 
The Nearly Best Film stars the actors :  [ Frank DeFrank ]  [ Joe Smith ]