2017-05-09 12 views
0

Javaでメソッドを呼び出そうとすると、マップが実行され、入力されたキーが検索され、オブジェクトのセットとその値がコレクションから取得されます。Java内のマップ内のセット内のオブジェクトにアクセスする

コンテキストの場合、これは2つのクラス、BookとAuthorを持つアプリケーションで、authorはtitleとyearPublishedという属性を持つ書籍のコレクションを保持します。

/** 
    * This method can be used to populate test data. 
    */ 
    public void createTestData() 
    { 
     Set<Book> collection = new HashSet<>(); 
     Book book1 = new Book("Lord of the Flies",1954); 
     Book book2 = new Book("Another Lord of the Flies",1955); 
     Book book3 = new Book("Jamaica Inn",1936); 
     collection.add(book1); 
     collection.add(book2); 
     collection.add(book3); 
     bookSet.put("William Golding",collection); 

     Set<Book> collection2 = new HashSet<>(); 
     Book book4 = new Book("The Wind in the Willows",1908); 
     Book book5 = new Book("Oliver Twist",1838); 
     collection2.add(book4); 
     collection2.add(book5); 
     bookSet.put("Kenneth Grahame",collection2); 
    } 
:私は私の他のメソッドをテストすることができるように、私はまた、テストデータを移入するための方法を作成しました

クラス帳

public class Book 
{ 
    // instance variables 
    private String title; 
    private int yearPublished; 

    /** 
    * Constructor for objects of class Book 
    */ 
    public Book(String aTitle, int aYear) 
    { 
     this.title = aTitle; 
     this.yearPublished = aYear; 
    } 

クラス著者

public class Author 
{ 
    // instance variables 
    private Map<String, Set<Book>> bookSet; 

    /** 
    * Constructor for objects of class Author 
    */ 
    public Author() 
    { 
     bookSet = new HashMap<>(); 
    } 

:以下のクラス情報

私が必要とするのは、引数をとらず、マップを繰り返して、櫛をプリントアウトするメソッドですinationマップキー+書籍情報(書籍タイトルとこれまで

をyearPublished、私は次のように書かれている:

/** 
    * Prints out to the standard output the authors currently in the system and all the books written 
    * by them, together with the year it was published. 
    */ 
    public void printMap() 
    { 
     for (String key : bookSet.keySet()) 
     { 
     System.out.println(key + " " + bookSet.get(key)); 
     } 
    } 

ただし、出力はかなり奇妙です:

ウィリアム・ゴールディング[ブック@ 4f196e15 @ e8a4d45、ブック、ブックの@ 69f8d3cd] 19d6f478 @ケネス グラハム[ブック、ブック@ 6f4bff88]

私はARを得ることができる方法上の任意のアイデアその上にound?

また、1つの引数(マップキー)とマップキー(作成者の名前)とそれらによって書き込まれたすべての書籍を標準出力に出力する書籍セットを取得する方法を考え出しています。ウィリアム・ゴールディングによって書かれた

example.printMapValue("William Golding"); 

冊です:

/** 
    * Searches through the map for the key entered as argument. If the argument is a key in the map, prints 
    * textual representation of its associated value, otherwise prints an output line announcing 
    * that the key is not present. 
    */ 
    public void printMapValue(String aKey) 
    { 
     System.out.println("The books written by " + aKey + " are: " + bookSet.get(aKey)); 
    } 

結果は再び非常に奇妙です:[ブックの@ e8a4d45、ブック(タイトルとyearPublishedこれは私がこれまで持っているものです@ 4f196e15、>書籍@ 69f8d3cd]

誰かが私を助けてくれたら本当にありがたいです。

ありがとうございます。

答えて

0

すべてのJavaオブジェクトは、暗黙的にObjectクラスから拡張/継承します。オブジェクトを(例えばSystem.out.printに渡して)印刷するか、文字列に連結する(あなたの場合のように)、オブジェクトの文字列表現が必要です。この表現は、toStringメソッドからベース(Object)クラスから取得します。 toStringのデフォルトの動作では、あなたは、ユーザーフレンドリーな表現を得るためにあなたのBookクラスでそれをオーバーライドする必要があり、そのため

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

です。 おそらく1つの可能な表現は、

@Override 
public String toString() { 
    return title; 
} 
3

印刷可能な文字列を取得するには、BookクラスのtoString()メソッドをオーバーライドする必要があります。

public String toString() 
{ 
    return title; 
} 
0

単にあなたのBookクラス内部ObjectクラスのtoString()メソッドをオーバーライドします。

@Override 
public String toString() { 
     return "Book{" + 
       "title='" + title + '\'' + 
       ", yearPublished=" + yearPublished + 
       '}'; 
} 
関連する問題