114

I've determined that a Java ArrayList.add is similar to a JavaScript Array.push

I'm stuck on finding ArrayList functions similar to the following

  • Array.pop
  • Array.shift
  • Array.unshift I'm leaning toward ArrayList.remove[At]
dreftymac
  • 31,404
  • 26
  • 119
  • 182
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284

5 Answers5

182

ArrayList is unique in its naming standards. Here are the equivalencies:

Array.push    -> ArrayList.add(Object o); // Append the list
Array.pop     -> ArrayList.remove(int index); // Remove list[index]
Array.shift   -> ArrayList.remove(0); // Remove first element
Array.unshift -> ArrayList.add(int index, Object o); // Prepend the list

Note that unshift does not remove an element, but instead adds one to the list. Also note that corner-case behaviors are likely to be different between Java and JS, since they each have their own standards.

Federico
  • 1,925
  • 14
  • 19
Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
  • 11
    If you're doing a lot of "unshifting" but not a lot of getting at middle indices, you may find ArrayList to be inferior to LinkedList in terms of actual run times. – Patrick Nov 02 '13 at 10:34
  • while(Item item = items.remove(0)){ ... } is not equivalent to shift. – e-info128 Jul 24 '15 at 19:49
  • What about `.push`? – jameshfisher Oct 05 '16 at 16:52
  • 1
    OP said he knew `Array.push -> ArrayList.add`, and specifically asked about `pop`, `shift`, and `unshift`. Reading this again, I'm going to add more explanation and add `.push` at the same time. – Jon Egeland Oct 06 '16 at 16:20
  • Even though it was not asked, this answer feels incomplete without any mention of the complexity of these functions. – Jasper Dec 04 '18 at 14:18
  • This is subjective an may thus be flagged, but it is less efficient for ArrayLists to have different method names then their analogous counterparts in similar languages because programmers will have to repeatedly search up or take time to learn the new method names and may even be more liable to write code with logical errors. Objectively, it is dumb. – cmarangu Jan 26 '20 at 04:34
  • 1
    @Cozzbie that is not true in the case of ArrayList.remove(int index) which the answer references since this overload does return the object removed. – Sherwin F Sep 29 '22 at 15:12
34

I was facing with this problem some time ago and I found java.util.LinkedList is best for my case. It has several methods, with different namings, but they're doing what is needed:

push()    -> LinkedList.addLast(); // Or just LinkedList.add();
pop()     -> LinkedList.pollLast();
shift()   -> LinkedList.pollFirst();
unshift() -> LinkedList.addFirst();
Wirone
  • 3,304
  • 1
  • 29
  • 48
  • 1
    Why is this not accepted?! Note: `LinkeList` adds methods which would be very inefficient on `ArrayList` to the `List` interface, this was what confused me. This methods come from the `Deque` and `Queue` interfaces which it implements, but `ArrayList` does not. – Ciro Santilli OurBigBook.com May 07 '15 at 13:19
  • 1
    @CiroSantilli新疆改造中心六四事件法轮功 but how much inefficient? – Slava Jul 24 '18 at 14:14
  • @Slava O(n) vs O(1) for front insert, which is huge. – Ciro Santilli OurBigBook.com Jul 24 '18 at 14:22
  • 4
    @CiroSantilli新疆改造中心六四事件法轮功 O(n) and O(1) are just complexities. I heard that linked lists can be quite slower than array lists even for inserts/deletions. https://stackoverflow.com/questions/34170566/bjarne-stroustrup-says-we-must-avoid-linked-lists So I wonder, what about Java? – Slava Jul 25 '18 at 10:57
  • This is not a solution. a linked list is simply a different data structure with different characteristics. This questions pertains to a contiguous list – Post Self May 21 '22 at 12:16
  • This is not a solution. a linked list is simply a different data structure with different characteristics. This questions pertains to a contiguous list – Post Self May 21 '22 at 12:16
15

maybe you want to take a look java.util.Stack class. it has push, pop methods. and implemented List interface.

for shift/unshift, you can reference @Jon's answer.

however, something of ArrayList you may want to care about , arrayList is not synchronized. but Stack is. (sub-class of Vector). If you have thread-safe requirement, Stack may be better than ArrayList.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • 1
    Stack extends vector which is synchronized. ["Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector."](http://docs.oracle.com/javase/8/docs/api/java/util/Vector.html) – M.J. Rayburn Jul 08 '15 at 06:58
  • My bad, I just realized that in my sleep deprived state I didn't read the last half. – M.J. Rayburn Jul 08 '15 at 21:22
6

Great Answer by Jon.

I'm lazy though and I hate typing, so I created a simple cut and paste example for all the other people who are like me. Enjoy!

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        List<String> animals = new ArrayList<>();

        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Cat");
        animals.add("Dog");

        System.out.println(animals); // [Lion, Tiger, Cat, Dog]

        // add() -> push(): Add items to the end of an array
        animals.add("Elephant");
        System.out.println(animals);  // [Lion, Tiger, Cat, Dog, Elephant]

        // remove() -> pop(): Remove an item from the end of an array
        animals.remove(animals.size() - 1);
        System.out.println(animals); // [Lion, Tiger, Cat, Dog]

        // add(0,"xyz") -> unshift(): Add items to the beginning of an array
        animals.add(0, "Penguin");
        System.out.println(animals); // [Penguin, Lion, Tiger, Cat, Dog]

        // remove(0) -> shift(): Remove an item from the beginning of an array
        animals.remove(0);
        System.out.println(animals); // [Lion, Tiger, Cat, Dog]

    }

}
Adrian Smith
  • 1,013
  • 1
  • 13
  • 21
1

Underscore-java library contains methods push(values), pop(), shift() and unshift(values).

Code example:

import com.github.underscore.Underscore:

List<String> strings = Arrays.asList("one", "two", " three");
List<String> newStrings = Underscore.push(strings, "four", "five");
// ["one", " two", "three", " four", "five"]
String newPopString = Underscore.pop(strings).fst();
// " three"
String newShiftString = Underscore.shift(strings).fst();
// "one"
List<String> newUnshiftStrings = Underscore.unshift(strings, "four", "five");
// ["four", " five", "one", " two", "three"]
Valentyn Kolesnikov
  • 2,029
  • 1
  • 24
  • 31