Java HashMap/ArrayList object keeps asking for initialCapacity/LoadFactor instead of the requested K/V pair

36 views Asked by At

I´m unable to create HashMap/ArrayList objects. If I try to create a new HashMap, IntelliJ is asking for initialCapacity and LoadFactor, which is supposed to be an int/float. If I declare a HashMap with String/String K/V the code is underlined, IntelliJ can recognise it as a new object, but the put method is missing or not recognised. I´m not sure what could cause it, I have reinstalled the JDK and IntelliJ but the problem remains. I have also opened some older rojects where anything written previously works fine, however new lines produce the same issue. Here is snippet of my code just as an example:


import java.util.*;

public class TestClass {
    HashMap<String, String> testMap = new HashMap<>("firstString", "secondString");
    testMap.put("first", "second");
}

screenshot

Some data that might be relevant: -java version "17.0.5" 2022-10-18 LTS -IntelliJ community IDEA 2022.2.3

Thanks a lot for the help in advance, and sorry for the bad formatting, I´ll make sure to improve it in the future.

1

There are 1 answers

2
Javier On

There is no constructor of HashMap that receives a sequence of keys and values.

Either use:

HashMap<String, String> testMap = new HashMap<>();
testMap.put("firstString", "secondString");
testMap.put("first", "second");

or

Map<String,String> testMap = Map.of("firstString", "secondString", "first", "second");

(although the latter is immutable)