You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2018/05/03 07:36:00 UTC

[jira] [Commented] (GROOVY-8565) Method from java.util.Deque to be added to Groovy List?

    [ https://issues.apache.org/jira/browse/GROOVY-8565?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16462053#comment-16462053 ] 

Paul King commented on GROOVY-8565:
-----------------------------------

There are some existing options for some of these:
{code}
LinkedList list1 = [2, 3]
list1.addFirst(1)
assert list1 == [1, 2, 3]
assert list1[0] == list1.peekFirst()
assert list1[-1] == list1.peekLast()

def list2 = [4, 5] // An arraylist
list2 << 6 // addLast(6)
assert list2 == [4, 5, 6]
def list3 = [3] + list2 // addFirst(3)
assert list3 == 3..6
list3.add(0, 2) // alternative addFirst(2)
assert list3 == 2..6
assert list3[0] == list3.head() // peekFirst alternative
assert list3[-1] == list3.last() // peekLast alternative
{code}
I think the offerXXX variants only make sense for a list with a capacity - in which case it will probably already have the offerXXX methods.

> Method from java.util.Deque to be added to Groovy List?
> -------------------------------------------------------
>
>                 Key: GROOVY-8565
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8565
>             Project: Groovy
>          Issue Type: New Feature
>            Reporter: Mike Rodent
>            Priority: Minor
>
> I was surprised to find that List in the GDK hasn't added `addFirst` method. AKA `shift`.  Also `removeFirst` AKA `unshift`.
> I'm intrigued to see that in Groovy 3.0.0 (not in beta at time of writing) `push` and `pop` *appear* to be changing from adding/removing at the end, as currently, to adding/removing at the *beginning*, "to align with java.util.Stack".  So can we conclude that the need for more stack/queue/deque methods has been recognised?
> Of course there are multiple ways of doing this... it's just that that means that it's slightly less convenient to use a standard Groovy List as an interesting form of queue or deque.  What you really want is parameterless methods for these standard queue-type operations.
> java.util.Deque has some other interesting methods which one might want to think about too: offerXXX, peekXXX, getXXX.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)