Associations



  • Good night. I'll try to study the associations. Available: UPD

    import java.util.*;
    

    class Map {
    int a;
    String value;
    String key;
    HashMap<String, String> map;
    Map(){
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    }
    int getSize(){
    return map.size();
    }

    String getValue(String key) {
    this.key = key;
    return map.get(key);
    }
    }

    public class Pr10_2 {

    public static void main(String[] args) {
        Map mp = new Map();
        System.out.println(mp.getSize());
        System.out.println(mp.getValue("key1"));  
    } 
    

    }

    Trying. key1 value value by getValuebut the editor emphasizes the map in the line return map.get(key);♪ The same thing happened. getSize()I had to do something different. Can you tell me if I'm asking for something wrong? Or do I declare something wrong?



  • I'd rewrite the class so:

    class Map {
        HashMap<String, String> map;
    
    Map(){
        map = new HashMap&lt;&gt;();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
    }
    
    int getSize(){
        return map.size();
    }
    
    String getValue(String key) {
        return map.get(key);
    }
    

    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2