Java - xml list or one value - java

What I have to do to check I have got list of data or one argument?
I have got this in code software and he show "yes" if argument 1 but if I have got list I have got empty place. I would like to make If which can accept only one agument but no list.
System.out.println(doc.getDocumentElement().getChildNodes().item(t).getNodeName());
System.out.println(doc.getDocumentElement().getChildNodes().item(t).getLastChild().getNodeValue());
Output (because car have list of nodes and If some element have list not one value I don`t want to show this element for example "car"):
Adam
yes
car
List:
<car>
<window>yes</window>
<door>yes</door>
</car>
1 argument
<Adam>yes</Adam>

Just check the length of the list.
And you probably don't want all kinds of child nodes, rather just child elements. Try
doc.getDocumentElement().getElementsByTagName("*")
instead.

Related

using one dimension array, how to identify if duplicated or not

allows me to enter the Id numbers without fear of duplication. my program should be able to display the list of ID numbers at the end
You're looking to use a HashSet, which is built-in to Java. This is a datastructure which you can insert elements into, and also check if a certain element exists in constant time. Thus, when you read in an element, first check if the set contains said element; if so, skip it, otherwise, put it in your list of IDs and also insert it into your HashSet. You can see the documentation here

Remove all the strings in an array list containing certain characters

so i have an array list that contains strings such as:
ArrayList<String> list = new ArrayList<>();
list.add("bookshelf");
list.add("bookstore");
list.add("library");
list.add("pencil");
Now i wanna search and remove all the strings in the arraylist that contain the word "book" in them. As far as i understand list.remove("book"); will only search for the particular string "book" and not the strings that contain the word "book". How can i solve this?
You can use removeIf like this:
list.removeIf(s -> s.contains("book"));
Note: this answers applies to Java version 7 and below (of course that it will work for higher versions as well but YCF_L's answer is simpler to implement in versions 8 and above).
The requirement is to iterate the list, check every element, and if it answers a certain condition: remove it.
Since this is the case we fall into a risky scenario where we modify the list while iterating it which is problematic because when we remove an element in the list its size changes.
In order to work around this problem we can iterate the list by index from the last element and back until the first one, this way, removing an element at index n will not effect accessing any element at index < n.
I'll leave the implementation details to you in order not to "spoon feed" and destroy your exercise :)

Java stream - delete everything after first match

I have a linked hash list of some DTO that contains name (String) and isActive (boolean) properties. I need to iterate over all of them from first to last and check, if any of members have false value in isActive. When I find this element, I need to cut off this element and the rest of list and return just elements before this one.
I would like to use java streams to solve this problem.
I've tried to use stream and filter elements via predicate to check if element is active but that does not match the logic I want. As I said, I need to find first element that is inActive and cut it and everything after from the list.
If we have this list of ElementDto in LinkedHashSet<>
0 - name:test, isActive:true
1 - name:test2, isActive:true
2 - name:inActive, isActive:false
3 - name:activeAfterInactive, isActive:true
The expected output would be list with elements 0,1
The following should work for any ordered collection:
elementCollection.stream().takeWhile(el -> el.isActive())...

Why need to shift elements of ArrayList to the left after removing an element?

As far as I know, here are steps to remove an element in an ArrayList.
No need to do anything with the target element
Iterate along the array from 1 after the deleted element, to the last element
Copy each element into the location 1 before it
Set the last element to null
Why don't just set the element we want to remove to null? That takes only one step.
Could anyone explain the merit of above scheme? Thanks in advance.
Because these are not the same (let's remove the "c"):
["a", "b", "d", "e"]
["a", "b", null, "d", "e"]
There is a big difference between the removal and the replacement:
The removal changes the indices, removes an element and shifts (affects) the remaining ones. This also changes the size of the list. Ex. the element "d" is accessible at the index 2 (moved from the 3).
The replacement replaces the item and keeps the indices of the elements and the size of the list remains unchanged as well. Ex. the element "d" is accessible at the index 4 (unchanged).
Why don't just set the element we want to remove to null?
"setting" an element to null is not removing an element it's replacing an element.
There's another issue with that approach, if null in a list means the element at that index is "removed" then what if I want to actually store null as an element? how do you distinguish a removed element and an actual null element?
Leverage the remove methods in the List<T> API, don't try to reinvent the wheel as they're as good as they can get.
...because that's how Lists work. Many usecases need this dynamic sizing behaviour, so Java has an interface for the List concept. ArrayList is one of several implementations of List, LinkedList is another.
We already had the behaviour you're describing, the Array - so where you need that behaviour you would use say a String[].
ArrayLists are just like arrays with one difference - they are resizable(it does not matter in this example).
Consider the following array: [ref1,ref2,ref3,ref4] , where ref1:4 are references to some objects. It means that they do not contain the object itself, they only point to the object. Now, if you want to delete ref2 for example by declaring it null that means that it won't point to any object now (ref2 = null) but that does not mean that the reference itself dissapeard.Your new array(ArrayList) will be [ref1,null,ref3,ref4] now. You see? There is a difference between replacement and removal....

ArrayList/LinkedList get First Index number

Is is possible to get out of an ArrayList the first number of the first index?
Here's an Example:
In there are 5 items:
path = {0.5,5.0},{0.6,6.0},{0.7,7.0},{0.8,8.0},{0.9,9.0}
And I want to get the number 5.0 out of {0.5,5.0}...
I tried it with path.get(0) But it only gives me {0.5,5.0} back.
Is it possible to get 5.0 out of it without getting all the other numbers?
If your ArrayList contains arrays, this is the way to go
myList.get(0)[1] // You're getting the index 1 from the 1st array of your ArrayList
Otherwise, if it is containing other ArrayList's
myList.get(0).get(1) // Same logic as above applied to the Collection
If your curly braces in the ArrayList are actually brackets (they probably are), you can use:
myArray.get(0)[index] to get the index you want. In your example it is:
myArray.get(0)[1];
Note: if your ArrayList elements are also ArrayList then you need to use get(0).get(1) instead of get(0)[1].

Categories

Resources