Remove elements from arraylist based on a condition [closed] - java

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().

Related

Is ["abcd"] and [["abcd"]] are same? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have used java ArrayList when I inserted one element in the list and i have converted the validatable response to array list when I used assert equals it is showing that both are different
like one is [abcd] other is [[abcd]]
Validatable response = given().spec(request).filter(new ErrorLoggingFilter(errorPrintStream)).pathParams("","").when.post(endpoint).then()
the response is of the form ArrayList when I printed that It came of the form [[abcd]]
To my knowledge, these two are different things
["abcd"] this means an array has one string "abcd" element.
[["abcd"]] this means an array has one array ["abcd"] element.
Yes, ["abcd"] and [["abcd"]] are completely different. Let us understand why.
Let us consider an array ["abcd"]. As you can see, it contains only one element i.e. "abcd". So this is an array that contains a single string value. Now for [["abcd"]], the outer array contains another array inside of it and the inner array contains "abcd". Though their ultimate content seem to be same, they are absolutely different. One is a string array (an array that contains a string value) and the other is an array of string arrays.
Absolutely different, a one-dimensional array, a two-dimensional array,in many languages,reference form it, [0] and [0][0]

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.

get a certain part of a hashmap [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'm converting a hashmap into 2 arrays, an integer array, and a String array. I'm trying to do this to make it usable, my HashMap looks like this:
HashMap<String, String> I'm converting the second string into an integer, and the hashmap is created from what the user puts into the configuration file.
I want to move the first string into an array, and then the second string into another array, how can do that?
in otherwords, when i do hashmap.values() is there a way to get one section, like the first and the the second one later? What's the best way around this?
I thing you basically want to create two arrays one for keys and one for values. You can use map.keySet() to get keys and map.values() to get values. Now you can convert this two into a List or Array.
I want to move the first string into an array, and then the second
string into another array, how can do that?
List<String> keyList= new ArrayList<String>( map.keySet());
List<String> valueList= new ArrayList<String>( map.values());
If you want to convert List as array than,
String[] keyArray=keyList.toArray(new String[keyList.size()]);
String[] valueArray=valueList.toArray(new String[valueList.size()]);

Assign information to element in array in java [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
Is it possible to assign information to an element in an array in java?
For example, can I have an element containing a title and a number etc?
How do you go about this? thanks
Yes, instead of having an array of primitive types (like ints) or Strings, define your own class and then create an array of that. Then you can have whatever information you want in there.
Alternatively, if the array must be an array of primitives or Strings because some other method requires it to be in that form, you could either create a second array like that on demand, or use a second array for the associated data (again you might create your own class for this).

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