Comparator Error java.lang.IllegalArgumentException [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
How can I sort array according to the count of set bits? I'm getting an error whit below code:
Arrays.sort(arr, (o1, o2) -> {
if (Integer.bitCount(o1) <= Integer.bitCount(o2))
return 1;
return -1;
});
Exception:
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.base/java.util.TimSort.mergeLo(TimSort.java:781)
at java.base/java.util.TimSort.mergeAt(TimSort.java:518)
at java.base/java.util.TimSort.mergeCollapse(TimSort.java:448)
at java.base/java.util.TimSort.sort(TimSort.java:245)
at java.base/java.util.Arrays.sort(Arrays.java:1441)
at Compute.sortBySetBitCount(File.java:44)
at GFG.main(File.java:23)
How to fix this?

You never return 0, even if the elements are equal. Using the Comparator.comparing methods is generally better for simple sorting methods.
Arrays.sort(arr, Comparator.comparingInt(Integer::bitCount).reversed());

Related

Minecraft Spigot having problem with ImmutableList [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
in end of code is this error what problem? tried many fixes but no ones help
https://i.stack.imgur.com/jwosT.png
return (List<String>) ImmutableList.of();
The problem with this is that the type of ImmutableList.of() is determined before the cast is applied.
The type of ImmutableList.of() in that context is ImmutableList<Object>. An ImmutableList<Object> isn't an ImmutableList<String>.
I'm surprised you need any type hinting here:
return ImmutableList.of();

HashMap get method throwing error on hm.get(hm) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Could you please explain the below scenarios?
HashMap<HashMap,HashMap> hm=new HashMap<>();
hm.put(hm,hm);
hm.get(hm); // ----> On commenting this line, I get no error.
// If I uncomment it, I am getting StackOverflowError
Trying to put a HashMap as a key inside itself is a bad idea.
After hm.put(hm,hm), your HashMap contains a key whose hashCode() is hm.hashCode(). hm.hashCode() is a function of the hashCode() of all the Map's entries. The hashCode() of an entry is a function of the hashCode() of the key and value (both are hm in your case). Hence, in order to compute hm.hashCode(), you have to compute hm.hashCode(). This results in infinite recursion.
Calling hm.get(hm); required computing hm.hashCode(), leading to infinite recursion and StackOverflowError.

How do I reverse a number in java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 13 days ago.
Improve this question
Can anyone explain to me how to reverse a number start with zero in java. I trying to reverse a number 025 but output is only 52. But output should be 520
explanation much appreciated.
You are mixing types. "025" is not a number, it's a String. In a number you simply cannot distinguish between 25, 025, 0025, 00025, ... Implement your assignment as a String operation.
public String reverseString(String s) {
// put your code here
}
You may find very useful the Oracle tutorial on Strings here: https://docs.oracle.com/javase/tutorial/java/data/strings.html

put(java.lang.String,java.lang.Inte ger) in java.util.Map<java.lang.String,java.lang.Integer> cannot be applied to (java.lang.String,java.lang.String) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am getting this error while compiling my code. Please help me with this-
code is
mapConnectionProperties = new HashMap<String, Integer>();
mapConnectionProperties.put(mobileSeriesMappingDTO
.getExternalIP(), mobileSeriesMappingDTO.getExternalPort());
mobileSeriesMappingDTO.getExternalPort() is seemingly a String. Convert it to an Integer.
Integer.parseInt(mobileSeriesMappingDTO.getExternalPort())
Your mobileSeriesMappingDTO.getExternalPort() gives a String,
Transform it to an Integer with :
Integer.parseInt(mobileSeriesMappingDTO.getExternalPort())

Does Collections.sort() shuffles equal priority elements? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Or just let them alone or throws an exception?
If the third one, how can I sort elements in ArrayList saving the old order of priority equal elements?
Thanks in advance.
From the documentation:
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
To save the order of the old arraylist, simply make a copy of the arraylist ahead of time. You can do this by saying:
List<Integer> oldListOrder = new ArrayList<Integer>(listToBeSorted);
Collections.sort(listToBeSorted);
Keep in mind the elements will reference the same objects in both lists. That may not affect you, it depends on what you do next.

Categories

Resources