English 中文(简体)
利用地图内的清单(Java)
原标题:Using a list inside a map (Java)

我使用哈希姆普,在哈希姆普,我把阿雷拉主义者作为价值。

与此类似:

Map<Movie, List<Grades>> gradedMovies = new HashMap();

我试图制定一种方法,我可以通过这些价值观来审视是否已经存在钥匙。 如果是的话,我要在分配给特定关键(移除)的名单中增加新的价值(职等)。 与此类似:

public void addGrade(Movie movie, Grade grade) {
// stuff here }

最终,我wan不能够印制一部地图,在地图上添加后展示电影及其年级。

如何做到这一点? 还是我的整个方法(使用地图)完全错误?

感谢任何援助。 (这是家庭工作)

最佳回答

我认为,你回到正确的道路上来,只是确保电影物体实施<条码>平等<>>和<条码>。 因此,它能够作为散射地图的真正关键。

如果你只想做粗略的印刷,就执行环形方法。

public void addGrade(Movie movie, Grade grade) {
    if (!gradedMovies.containsKey(movie)) { 
        gradedMovies.put(movie, new ArrayList()); 
    }
    gradedMovies.get(movie).add(grade);
}

希望这一帮助, Cheers!

问题回答

你们可以使用这样的东西:

public void addGrade(Movie movie, Grade grade) {
    if (!gradedMovies.containsKey(movie)) {
        gradedMovies.put(movie, new ArrayList<Grade>());
    }
    gradedMovies.get(movie).add(grade);
}

您需要高于以下方法:<代码>等值/代码>。

I don t know why you re looking for an index particularly - the point of a Map is that you can look up entries by their keys.

因此,作为起点,您的<代码>第1行<>。 方法

List<grades> grades = gradedMovies.get(movie);

希望你们能够从那里得到。 (参看 了解地图是否含有特定电影......

I could iterate through the values to see if a key(movie) already exists

无需通过地图发布,仅打上<条码>。

请注意,在使用<代码>Movie时,作为关键事项,您应提供<代码>equis(<>/code>和hashCode()。

You! 但是,你应考虑以下两点:

虽然在地图上找到了价值,但你的电影物体必须优先于等值/strong>和hChode。 Java总是在比较中采用平等的方法,主要是在自动比较方法(例如,如果清单包含某一项,或在这种情况下,如果关键价值等于某一项)。 Remember that equals Defins uniquity of a items, sobre el be made a comparation based on a particulary separate identitye, as an identification number or (for this case) it s name.

印刷地图,iterate over the keySet, 要么是人工操作的(enced "for” loop),要么与导体(可以通过.iterator(方法直接获取)。 对于每一部电影,你以类似方式印制了各年级名单。

我不知道你是否熟悉“强硬”印刷,但某些特殊特性的组合可以添加到“努力”中,使之具有某种形式。 例如:

  • will insert a line break
  • is a tabulation

希望有助于消除一些疑虑。 好!

见http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/view/Multimap.html。 这正是它所做的。

private Multimap<Movie, Grade> map = ArrayListMultimap.create();

public void addGrade(Movie movie, Grade grade){
   map.put(movie, grade);
}

委员会将考虑为你们制定名单。

public void addGrade(Movie movie, Grade grade) {

boolean found = false;
for(Movie m : gradedMovies.keyset()) {
    // compare the movies
    if(/* match on movies */) {
        gradedMovies.get(m).add(grade);
        found = true;
    }
}
if(!found) {
    gradedMovies.put(movie, new ArrayList().add(grade));
}
}
gradedMovies.containsKey(movie);

    for(Map.Entry<Movie,List<Grades>> entry : gradedMovies.entrySet()){
            Movie key = entry.getKey();
       }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...