T
T
thatmaniscool2017-05-18 22:56:34
Java
thatmaniscool, 2017-05-18 22:56:34

How to pass an argument of type Map to a method?

First of all I create a Person class to hold some information

public class Person {
  private String name, surname;
  
  public Person (String name, String surname){
    this.name = name;
    this.surname = surname;
  }
  
  public String toString (){
    return "Person: " + surname + " " + name;
  }
}

After that, I create a common class with three subclasses, inheriting from the common class.
public class Data {
// Class of Car	
  public class Car extends Data {
    private String Mark;
    public Car (String Mark){
      this.Mark = Mark;
    }
    
    public String toString (){
      return "Car: " + Mark;
    }
  }
  
// Class of House	
  public class House extends Data {
    private String Adress;
    public House (String Adress){
      this.Adress = Adress;
    }
    
    public String toString (){
      return "House: " + Adress;
    }
  }
  
// Class of Work	
  public class Work extends Data {
    private String Work;
    public Work (String Work){
      this.Work = Work;
    }
    
    public String toString (){
      return "Work: " + Work;
    }
  }
}

Then I create the main class
public class MainClass {
  // Массив Map
  public static Map <Person, List <? extends Data>> info =
      new HashMap <Person, List <? extends Data>> ();
  
  // Вывод информации
  public static void ShowInfo (){
    for (Person person : info.keySet()){
      System.out.println (person);
      for (Data e : info.get(person)){
        System.out.println(e);
      }
    }
  }
  
  
  // Как ставить аргумент типа Arrays.asList ();???
  public static void addPerson (Person person){
    
  }
  
  public static void main(String[] args) {
  
    // Как передать этот участок кода как аргумент методу?
    info.put(new Person ("Nikolay", "Nikolaev"),
        Arrays.asList(
            new Data().new Car ("Jiguli"),
            new Data().new House ("Muhosransk"),
            new Data().new Work ("alcogolic")));
    
    ShowInfo ();
  }
}

And here I have a problem, namely, how to transfer this piece of code

info.put(new Person ("Nikolay", "Nikolaev"),
Arrays.asList(
new Data().new Car("Jiguli"),
new Data().new House("Muhosransk"),
new Data() .new Work("alcogolic")));

As an argument to this function

// How to put an argument like Arrays.asList ();???
public static void addPerson (Person person){
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question