I do not understand what the problem is? enter image description here

Console:

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer 

I put the keys, and the keys are Integer. Why is he trying to cast them in a String ?!

  private HashMap<Integer, String> RanksList = new HashMap<>(); if (this.getConfig().contains("PlayerRank")) { this.RanksList = (HashMap) this.getConfig().getConfigurationSection("PlayerRank").getValues(false); } else { this.getConfig().set("PlayerRank.100", "\u00a7a\u00a7lНовичок\u00a7r"); this.getConfig().set("PlayerRank.500", "\u00a7e\u00a7lОпытный\u00a7r"); this.getConfig().set("PlayerRank.1500", "\u00a71\u00a7lВетеран\u00a7r"); this.getConfig().set("PlayerRank.3000", "\u00a75\u00a7lМастер\u00a7r"); this.getConfig().set("PlayerRank.4500", "\u00a7c\u00a7lЛегенда Зоны\u00a7r"); this.RanksList = (HashMap) this.getConfig().getConfigurationSection("PlayerRank").getValues(false); } 

This is a part of my code from uploading the plugin config to the minecraft server. Bukkit

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

2 answers 2

Suppose the getValues method returns a Map<String, String> :

 private static Map<String, String> getValues() { HashMap<String, String> map = new HashMap<>(); map.put("abc", "def"); return map; } 

If it is incorrect to work with it (using raw-type instead of generics), then such a situation is quite possible:

 public static void main(String[] args) { HashMap<Integer, String> ranksList = (HashMap)getValues(); for (int i : ranksList.keySet()) { System.out.println("Key: " + i); } } 

Which will lead to

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

Since, in fact, String participate in the hash table as keys, and you cannot make an Integer from String using a simple type conversion.

If the code were:

 HashMap<Integer, String> ranksList = (HashMap<Integer, String>)getValues(); 

That would be a compilation error:

java.lang.RuntimeException: Uncompilable source code - incompatible types: java.util.Map<java.lang.String,java.lang.String> cannot be converted to java.util.HashMap<java.lang.Integer,java.lang.String>


It can be assumed that the getValues method returns just a Map , which is why

  HashMap<Integer, String> ranksList = (HashMap<Integer, String>)getValues(); 

will not save the situation (the code will be compiled, but ClassCastException , of course, will not go anywhere), but this will be the fault of the developers of the getValues method.


And judging by the new screenshot

screenshot

The getValues method returns a Map<String, Object> , which contains String as keys and which should not be attempted to be cast to Map<Integer, String> , because neither the type of keys does not match, nor the type of values.

If you cannot change the type returned by getValues , you can create a Map<Integer, String> based on Map<String, Object> :

 HashMap<Integer, String> ranksList = new HashMap<>(); Map<String, Object> map = getValues(); for (String key : map.keySet()) { ranksList.put(Integer.parseInt(key), String.valueOf(map.get(key))); } 

Or using Java 8:

 HashMap<Integer, String> ranksList = new HashMap<>(); Map<String, Object> map = getValues(); map.forEach((key, value) -> ranksList.put(Integer.parseInt(key), String.valueOf(value))); 

Also, do not forget that if any of the keys in the map does not fit the format adopted by the Integer.parseInt method, a Integer.parseInt will be NumberFormatException .

  • @Prototype-TV, that’s what it is: the getValues method returns a hash table, the keys of which are String . Make Integer of String simple type conversion will not work. - Regent
  • joxi.ru/Vm67BpYsD0zGb2 Solved. - Prototype - TV
  • @Prototype-TV added a couple of conversion options to its answer. Perhaps some of them will interest you. And if my answer suits you, do not forget to mark it as correct. - Regent
  • your version seems to me more correct, nevertheless I will take it. But tell me, does my version from the screenshot above have flaws? - Prototype - TV

Problem solved. Thanks to all. Correct me if I did something extra somewhere)

  if (this.getConfig().contains("PlayerRank")) { for (Map.Entry<String, Object> entry : this.getConfig().getConfigurationSection("PlayerRank").getValues(false).entrySet()) { this.RanksList.put(Integer.parseInt(entry.getKey()), String.valueOf(entry.getValue())); } } else { this.getConfig().set("PlayerRank.100", "\u00a7a\u00a7lНовичок\u00a7r"); this.getConfig().set("PlayerRank.500", "\u00a7e\u00a7lОпытный\u00a7r"); this.getConfig().set("PlayerRank.1500", "\u00a71\u00a7lВетеран\u00a7r"); this.getConfig().set("PlayerRank.3000", "\u00a75\u00a7lМастер\u00a7r"); this.getConfig().set("PlayerRank.4500", "\u00a7c\u00a7lЛегенда Зоны\u00a7r"); for (Map.Entry<String, Object> entry : this.getConfig().getConfigurationSection("PlayerRank").getValues(false).entrySet()) { this.RanksList.put(Integer.parseInt(entry.getKey()), String.valueOf(entry.getValue())); } }