You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Chen Guoping (Jira)" <ji...@apache.org> on 2019/12/20 08:22:00 UTC
[jira] [Commented] (COLLECTIONS-742) Create new method in
CollectionUtils to return first element if not null
[ https://issues.apache.org/jira/browse/COLLECTIONS-742?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17000714#comment-17000714 ]
Chen Guoping commented on COLLECTIONS-742:
------------------------------------------
Hello Wesllhey Holanda,
What about the CollectionUtils.get(Object object, int index) with index=0 ?
Line 1316:
{code:java}
/**
* Returns the {@code index}-th value in {@code object}, throwing
* {@code IndexOutOfBoundsException} if there is no such element or
* {@code IllegalArgumentException} if {@code object} is not an
* instance of one of the supported types.
* <p>
* The supported types, and associated semantics are:
* </p>
* <ul>
* <li> Map -- the value returned is the {@code Map.Entry} in position
* {@code index} in the map's {@code entrySet} iterator,
* if there is such an entry.</li>
* <li> List -- this method is equivalent to the list's get method.</li>
* <li> Array -- the {@code index}-th array entry is returned,
* if there is such an entry; otherwise an {@code IndexOutOfBoundsException}
* is thrown.</li>
* <li> Collection -- the value returned is the {@code index}-th object
* returned by the collection's default iterator, if there is such an element.</li>
* <li> Iterator or Enumeration -- the value returned is the
* {@code index}-th object in the Iterator/Enumeration, if there
* is such an element. The Iterator/Enumeration is advanced to
* {@code index} (or to the end, if {@code index} exceeds the
* number of entries) as a side effect of this method.</li>
* </ul>
*
* @param object the object to get a value from
* @param index the index to get
* @return the object at the specified index
* @throws IndexOutOfBoundsException if the index is invalid
* @throws IllegalArgumentException if the object type is invalid
*/
public static Object get(final Object object, final int index) {
final int i = index;
if (i < 0) {
throw new IndexOutOfBoundsException("Index cannot be negative: " + i);
}
if (object instanceof Map<?,?>) {
final Map<?, ?> map = (Map<?, ?>) object;
final Iterator<?> iterator = map.entrySet().iterator(); {code}
> Create new method in CollectionUtils to return first element if not null
> ------------------------------------------------------------------------
>
> Key: COLLECTIONS-742
> URL: https://issues.apache.org/jira/browse/COLLECTIONS-742
> Project: Commons Collections
> Issue Type: New Feature
> Components: Collection
> Affects Versions: 4.4
> Reporter: Wesllhey Holanda
> Priority: Minor
> Original Estimate: 8h
> Remaining Estimate: 8h
>
> When we try to get the first element into collection, we have to lead with null point exception. So with this method, when the collection is null or empty, the method will return null, and when not, the method will return the first element.
> In this way, the user don't need to write nothing to lead with null point exception.
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)