Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

475
Views
Java equivalent of Python List

In Python there is a data structure called 'List'. By using 'List' data structure in Python we can append, extend, insert, remove, pop, index, count, sort, reverse.

Is there any similar data structure in Java where we can get all that function like Python List?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The closest Java has to a Python List is the ArrayList<> and can be declared as such

//Declaring an ArrayList
ArrayList<String> stringArrayList = new ArrayList<String>();

//add to the end of the list
stringArrayList.add("foo");

//add to the beggining of the list
stringArrayList.add(0, "food");

//remove an element at a spesific index
stringArrayList.remove(4);

//get the size of the list
stringArrayList.size();

//clear the whole list
stringArrayList.clear();

//copy to a new ArrayList
ArrayList<String> myNewArrayList = new ArrayList<>(oldArrayList);

//to reverse
Collections.reverse(stringArrayList);

//something that could work as "pop" could be
stringArrayList.remove(stringArrayList.size() - 1);

Java offers a great selection of Collections, you can have a look at a tutorial that Oracle has on their site here https://docs.oracle.com/javase/tutorial/collections/

IMPORTANT: Unlike in Python, in Java you must declare the data type that your list will be using when you instatiate it.

over 4 years ago · Santiago Trujillo Report

0

Several collections exist, but your probably looking for ArrayList

In Python you can simply declare a list like so:

myList = []

and begin using it.

In Java, it better to declare from the interface first so:

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

Python          Java
append          add
Remove          remove
len(listname)   list.size

Sorting a List can require a little more work, for example, depending on the objects you may need to implement Compactor or Comparable.

ArrayList will grow as you add items, no need to extend it on your own.

As for reverse() and pop(), I'll refer you can refer to:

How to reverse a list in Java?

How to pop items from a collection in Java?

over 4 years ago · Santiago Trujillo Report

0

Java has an interface called list, which has implementations such as ArrayList, AbstractList, AttributeList, etc.

https://docs.oracle.com/javase/8/docs/api/java/util/List.html

However, each one has different functionalities, and I don't know if they have everything you've specified such as .reverse().

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!