I have a Set instance:
Set<String> siteIdSet = (Set<String>) pContext.getParent().getPropertyValue(getCatalogProperties().getSitesPropertyName());
The pContext.getParent().getPropertyValue()
is out-of-the-box code upon which I don't have any control to modify.
I wanted to get the first default element out of it (always). However, I couldn't find a method get(index)
like in an ArrayList
.
Hence, right now, I am doing like this.
for (Iterator<String> it = siteIdSet.iterator(); it.hasNext();) { siteId = it.next(); break; }
Is there any (other) efficient way (short and better) of achieving this?
begin() function is used to return an iterator pointing to the first element of the set container. begin() function returns a bidirectional iterator to the first element of the container. Syntax: setname.
The get() method of the ArrayList class accepts an integer representing the index value and, returns the element of the current ArrayList object at the specified index. Therefore, if you pass 0 to this method you can get the first element of the current ArrayList and, if you pass list.
To create a Sorted Set, firstly create a Set. Set<Integer> s = new HashSet<Integer>(); Add elements to the above set.
The firstElement() method returns the first element of the Stack object.
This will return the first element
set.iterator().next();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With