How do I remove something from a List in Java?
The ArrayList.remove(int index) method to remove element from ArrayList. Remove method is overloaded.
- ArrayList.remove(E element) remove the element at specifid index.
- ArrayList.remove(E element) remove the element by value.
- ArrayList.removeIf(Predicate p) remove all elements by specified value.
1. ArrayList.remove(int index) remove element from arraylist at specified index
This method removes the specified element E at the specified position in this list. It removes the element currently at that position and all subsequent elements are moved to the left (will subtract one to their indices).
Index start with 0.
public class ArrayListExample { public static void main(String[] args) { ArrayList namesList = new ArrayList(Arrays.asList( "alex", "brian", "charles") ); System.out.println(namesList); //list size is 3 //Add element at 1 index namesList.remove(1); System.out.println(namesList); //list size is 2 } }Program output.
[alex, brian, charles] [alex, charles]2. ArrayList.remove(E element) remove element from arraylist by element value
This method removes the first occurrence of specified element E in this list. As this method removes the custom object, the list size decreases by one.
Index start with 0.
public class ArrayListExample { public static void main(String[] args) { ArrayList namesList = new ArrayList(Arrays.asList( "alex", "brian", "charles", "alex") ); System.out.println(namesList); namesList.remove("alex"); System.out.println(namesList); } }Program output.
[alex, brian, charles, alex] [brian, charles, alex]3. Remove all element from arraylist by value
ArrayList does not provide inbuilt method to remove all elements by specified value. We can use other super easy syntax from Java 8 stream to remove all elements for given element value.
Java program to use List.removeIf() for how to remove multiple elements from arraylist in java by element value.
public class ArrayListExample { public static void main(String[] args) { ArrayList namesList = new ArrayList(Arrays.asList( "alex", "brian", "charles", "alex") ); System.out.println(namesList); namesList.removeIf( name -> name.equals("alex")); System.out.println(namesList); } }Program output.
[alex, brian, charles, alex] [brian, charles]Happy Learning !!
Read More:
A Guide to Java ArrayList
ArrayList Java Docs
Let us know if you liked the post. Thats the only way we can improve.
Video liên quan
Bài đăng phổ biến
-
Như chúng ta cũng biết, chiếc điện thoại từ lâu đã trở thành một vật bất ly thân trong đời sống xã hội ngày nay. Để tiện cho công việc cũng ...
-
These tips are divided into IELTS listening exam specific skills / tips and then general English language listening skills. A lot of student...
-
Trong bài viết dưới đây Taimienphi.vn sẽ hướng dẫn bạn cách để đổi màu áo, quần bằng Photoshop . Thủ thuật đổi màu ...
-
- Tốt nghiệp đại học trở lên, ưu tiên các chuyên ngành liên quan đến marketing, quản trị kinh doanh, TMĐT - Kỹ năng tin học văn phòng. (Thàn...
-
Cách viết ngày tháng năm trong tiếng Anh, cách viết tắt dễ nhớ Hướng dẫn chi tiết cách viết ngày tháng năm trong tiếng anh thông dụng nhất g...
-
Bạn không thể không hỏi mật khẩu WiFi mỗi khi bạn đến thăm bạn bè hoặc thành viên gia đình. Nó không giống như bạn sẽ tải xuống nội dung bằn...
-
Bất kỳ ai cũng đều có khả năng bị ngộ độc thực phẩm nhưng một số người lại dễ bị ngộ độc nặng hơn. Theo thống kê, trong 10 tháng đầu năm 201...
-
I. Nước Pháp trước cách mạng 1. Tình hình kinh tế xã hội. - Cuối thế kỷ XVIII, Pháp vẫn là nước nông nghiệp: + Công cụ, kĩ thuật canh tác lạ...
-
Rate this post Thời tiết cuối xuân chuyển hè với cái nắng bắt đầu oi ả, để không bị dìm hàng tơi ...
Danh sách Blog của Tôi
Labels
- Android
- Apple
- Bài tập
- Bàn phím
- Bánh
- Bao lâu
- Bao nhiêu
- Bí quyết
- Cách
- Chia sẻ
- Chuột
- Có nên
- Công Nghệ
- Công thức
- Cpu
- Cryto
- Danh sách
- Dịch
- Đại học
- Đánh giá
- Đẹp
- Eth
- File
- Film
- Gái
- Game
- Giá
- Giá bán
- Giá rẻ
- Giới Tính
- Gpu
- Gym
- Học
- Học Tốt
- Hỏi Đáp
- Hướng dẫn
- Ios
- Ipad
- Iphone
- Khoa Học
- Khỏe
- Khỏe Đẹp
- Kinh nghiệm
- Là gì
- Làm sao
- Laptop
- Lg
- List
- Macbook
- Màn hình
- Máy
- Máy tính
- Mẹo
- Mẹo Hay
- Món
- Món Ngon
- Mua Sắm
- Nấu
- Ngân hà
- Nghĩa là gì
- Nghiên cứu
- Ngoại ngữ
- Ngôn ngữ
- Nhà
- Ở đâu
- Phát minh
- Phân tích
- Phim
- Phụ nữ
- Phương pháp
- Phương trình
- Review
- Sách
- Samsung
- Sáng kiến
- So sánh
- Son
- Tại sao
- Thể dục
- Thế nào
- Thị trường
- Thịt
- Thuốc
- Tiếng anh
- Tiếng hàn
- Tiếng trung
- Top
- Top List
- Tốt nhất
- Trade
- Trai
- Trái đất
- Trò chơi
- Trường lớp
- Váy
- Vì sao
- Xây
- Xây Đựng
0 nhận xét: