List method

I will summarize the methods of List. We plan to update it from time to time.

add(E e) Adds the specified element to the end of this list.

List<String> list = new ArrayList<>();
list.add("Ah");
list.add("Eo");
list.add("asdf");
System.out.println(list);
[Ah,Eo, asdf]

add(int index, E element) Inserts the specified element at the specified position in the list.

List<String> list = new ArrayList<>();
list.add("Ah");
list.add("Eo");
//Insert at 0th
list.add(0, "ABCD");
list.add("asdf");
System.out.println(list);
[ABCD,Ah,Eo, asdf]

clear() Remove all elements from this list.

List<String> list = new ArrayList<>();
list.add("Ah");
list.add("Eo");
list.add(0, "ABCD");
list.add("asdf");
System.out.println(list);
list.clear();
System.out.println(list);
[ABCD,Ah,Eo, asdf]
[]

contains(Object o) Returns true if the specified element is included in this list.

List<String> list = new ArrayList<>();
list.add("Ah");
list.add("Eo");
list.add(0, "ABCD");
list.add("asdf");

//"asdf"Is included in the list, so true
System.out.println(list.contains("asdf"));
//"a"Is not included in the list, so false
System.out.println(list.contains("a"));
true
false

containsAll(Collection<?> c) Returns true if all elements of the specified collection are included in this list.

List<String> list = new ArrayList<>();
List<String> list3 = new ArrayList<>();
List<String> list4 = new ArrayList<>();

list.add("Ai");
list.add("up");
list.add(0, "AB");
list3.add("Ai");
list3.add("up");
list3.add(0, "AB");
list4.add("Ai");
list4.add("U");
list4.add(0, "AB");

//true because the elements contained in list and list3 are the same
System.out.println(list.containsAll(list3));
//false because the elements contained in list and list4 are different
System.out.println(list.containsAll(list4));
true
false

equals(Object o) Compares whether the specified object is equal to this list.

List<String> list = new ArrayList<>();
List<String> list3 = new ArrayList<>();
List<String> list4 = new ArrayList<>();

list.add("Ai");
list.add("up");
list.add(0, "AB");
list3.add("Ai");
list3.add("up");
list3.add(0, "AB");
list4.add("Ai");
list4.add("U");
list4.add(0, "AB");

System.out.println(list.equals(list3));
System.out.println(list.equals(list4));
true
false

get(int index) Returns the element at the specified position in this list.

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

list.add("Ai");
list.add("up");
list.add(0, "AB");

System.out.println(list.get(1));
System.out.println(list.get(2));
//IndexOutOfBoundsException
System.out.println(list.get(3));

Ai
up
IndexOutOfBoundsException

hashCode() Returns the hash code value for this list.

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

list.add("Ai");
list.add("up");
list.add(0, "AB");

System.out.println(list.hashCode());
14680320

indexOf(Object o) Returns the index of the position where the specified element was first found in this list. Returns -1 if the specified element is not in this list.

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

list.add("Ai");
list.add("up");
list.add(0, "AB");

System.out.println(list.indexOf("Ai"));
System.out.println(list.indexOf("AB"));
System.out.println(list.indexOf("q"));
1
0
-1

isEmpty() Returns true if there are no elements in this list.

List<String> list = new ArrayList<>();
List<String> list3 = new ArrayList<>();
List<String> list4 = new ArrayList<>();

list.add("Ai");
list.add("up");
list.add(0, "AB");
list3.add("");

//False because there is an element
System.out.println(list.isEmpty());
//False because the element contains blanks
System.out.println(list3.isEmpty());
//True because there are no elements
System.out.println(list4.isEmpty());
false
false
true

lastIndexOf(Object o) Returns the index of the position where the specified element was last found in this list. Returns -1 if the specified element is not in this list.

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

list.add("Ai");
list.add("Ai");
list.add("up");

//The last position to be detected is 1
System.out.println(list.lastIndexOf("Ai"));
//"Ah"Does not exist, so-1
System.out.println(list.lastIndexOf("Ah"));
1
-1

remove(int index) Deletes the element at the specified position in this list.

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

list.add("Ai");
list.add("Ai");
list.add("up");

System.out.println(list);
System.out.println(list.remove(1));
System.out.println(list);
[Ai, Ai,up]
Ai
[Ai,up]

remove(Object o) If the specified element is in this list, the first one is removed from the list.

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

list.add("Ai");
list.add("Ai");
list.add("up");

System.out.println(list);
//Can be deleted even if characters are specified
list.remove("Ai");
System.out.println(list);
[Ai, Ai,up]
[Ai,up]

removeAll(Collection<?> c) Removes all elements from the specified collection from this list.

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


list.add("Ai");
list.add("Ai");
list.add("up");

System.out.println(list);
list.removeAll(list);
System.out.println(list);
[Ai, Ai,up]
[]

retainAll(Collection<?> c) Keeps only the elements contained in the specified collection in this list.

List<String> list = new ArrayList<>(Arrays.asList("Apple","Orange", "Melon"));
List<String> list3 = new ArrayList<>(Arrays.asList("Orange","Melon"));

//Keep only those whose list contains the same elements as list3
list.retainAll(list3);
System.out.println(list);
[Orange, Melon]

set(int index, E element) Replaces the element at the specified position in this list with the specified element.

List<String> list = new ArrayList<>(Arrays.asList("Apple","Orange", "Melon"));

//0th"Apple"When"Banana"To replace
list.set(0, "Banana");
System.out.println(list);
[Banana, Orange, Melon]

size() Returns the number of elements in this list.

List<String> list = new ArrayList<>(Arrays.asList("Apple","Orange", "Melon"));

System.out.println(list.size());

list.add("Banana");
//Since it was added just before, the number of elements increased
System.out.println(list.size());
3
4

subList(int fromIndex, int toIndex) Returns a view of the part of this list from the specified fromIndex (including it) to toIndex (not including it).

ArrayList<Integer> foo = new ArrayList<>(Arrays.asList(10, 30, 5, 1, 11, 90));

System.out.println(foo.subList(0, 3));
System.out.println(foo.subList(1, 2));
System.out.println(foo.subList(4, 5));
[10, 30, 5]
[30]
[11]

toArray(T[] a) Returns an array containing all the elements in this list in the proper order (first element to last element). To convert from a list to an array, use the toArray method.

List<String> list = new ArrayList<>(Arrays.asList("Apple","Orange", "Melon"));

String[] str = new String[list.size()];
list.toArray(str);

System.out.println(Arrays.toString(str));
System.out.println(list);
[Apple, Orange, Melon]
[Apple, Orange, Melon]

Recommended Posts

List method
Method
Java method
to_i method
java (method)
getRequestDispatcher () method
merge method
Java method list (memorial) (under construction)
Map method
include method
Thread-safe list
initialize method
puts method
Work list
Java method
Class method
save! method
getParameter method
[Java] method
private method
rails method
Link list
[Java] method
List, Set, Map
Ruby to_s method
Screen transition method
Binary search binary search method
Java memorandum (list)
Method to search
Factory Method Pattern
Clone Java List.
Exception switching method
RxSwift method chain
Rails delegate method
scan method problem
Java8 method reference
[Swift] Capture list
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
[HTTP method PATCH]
Array / list / map
About the method
[Java] forEach method
[Practice] Map method
[Servlet] Basic method
variable and method
Binary search method
Template Method pattern
Template Method Pattern
0-filled (non-zero) method
JavaSilver11 study method
keycloak test-skip method
Factory Method pattern
[Ruby] initialize method
Ruby build method
undefined method `** _ path'error
java8 method reference
[Java] Random method
Wastefully creative "Hello World" output method list (Java)
[Docker] Command list