HashMap get method throwing error on hm.get(hm) [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 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.

Related

Comparator Error java.lang.IllegalArgumentException [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
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());

Adding an element into a HashSet inside a HashMap 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 6 years ago.
Improve this question
I have this problem which I looked for into the net and I could be helped... I also looked other Questions and they didnt work i dont know why... So I need your help....
So this is a field in which I create the HashMap:
private HashMap <String,HashSet<String>> userBuisness = new HashMap <String,HashSet<String>>();
And this is my try to add an element (i take a line from a file, i split it and then i add these elements into my HashMap):
String output = inputReader.nextLine();
String fields[] = output.split("\t");
userBuisness.put(fields[0],fields[1]);
As #AndyTurner said in a comment:
fields[1] is a String, not a HashSet<String>. You can build the latter using new HashSet<>(Arrays.asList(fields[1])).
But there are other issues with this snippet too. It would be better to rewrite like this, pay close attention to every little detail that I changed:
private Map<String, Set<String>> userBusiness = new HashMap<>();
...
String[] fields = output.split("\t");
userBusiness.put(fields[0], new HashSet<>(Collections.singletonList(fields[1])));

Local variable is redundant [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 7 years ago.
Improve this question
I am new to android, can anyone tell me what why is emailresult redundant?
From what I understand is that I retrieve textToUse from another method and name it email here, and then use email to undergo the matcher.find() with the result named emailresult. I then returned emailresult and after that returned the entire email.
I have mess around with it some time, like deleting emailresult and just use email. But then I will still have to create another variable to go under this location:
String emailresult = email.substring(matcher.start(), matcher.end());
It is redundant because you aren't doing anything with emailresult after assigning it a value besides returning it. You can simply do the following without the need to create a variable:
return email.substring(matcher.start(), matcher.end());
There is no need to create a variable
return email.substring(matcher.start(), matcher.end());

Remove elements from arraylist based on a condition [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
I have an ArrayList whose each element is of type DataType, where DataType is a class:
class DataType{
String dId;
String dType;
String rId;
}
I need to remove all such elements from the list whose rId is equal to any other element's dID.
i.e. if DataType D1 has value of dID as "abc" and DataType D2 has value of rID as "abc", than remove both D1 and D2 from the list.
Could someone please suggest the most appropriate approach for doing this.
The easiest would be to traverse the list once and create a HashMap<String, List<DataType>>.
You will map every object to their dID which forms the primary key.
After that you can iterate over your ArrayList, check the rId of the current object and see if it's in the HashMap. HashMap has O(1) lookup time so this should be a non issue. If the value is present, remove the current value (you're using an Iterator to prevent a ConcurrentModificationException) and remove the objects inside the value-part of the key-value pair as well.
Make sure you have correctly implemented .equals(Object o) and .hashcode().

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