Home >Java >javaTutorial >Common Java collection operation methods

Common Java collection operation methods

王林
王林Original
2024-02-19 15:44:06408browse

Common Java collection operation methods

Java collection (Collection) is a commonly used data structure in Java programming, used to store and operate a set of objects. It provides a series of common methods that can easily perform common operations such as adding, deleting, modifying, and querying collections. This article will introduce commonly used methods in Java collections and provide specific code examples.

1. Common methods of Collection interface
The Collection interface is the root interface of the Java collection framework and defines some of the most basic and commonly used methods. Some of the common methods and their usage examples are introduced below:

  1. add(Object obj): Add an element to the collection.

    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    System.out.println(list); // 输出 [A, B]
  2. remove(Object obj): Remove the specified element from the collection.

    Set<Integer> set = new HashSet<>();
    set.add(1);
    set.add(2);
    set.remove(1);
    System.out.println(set); // 输出 [2]
  3. contains(Object obj): Determine whether the specified element is contained in the collection.

    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    System.out.println(list.contains("A")); // 输出 true
    System.out.println(list.contains("C")); // 输出 false
  4. size(): Get the number of elements in the collection.

    Set<Integer> set = new HashSet<>();
    set.add(1);
    set.add(2);
    System.out.println(set.size()); // 输出 2
  5. isEmpty(): Determine whether the collection is empty.

    List<String> list = new ArrayList<>();
    System.out.println(list.isEmpty()); // 输出 true
    list.add("A");
    System.out.println(list.isEmpty()); // 输出 false
  6. clear(): Clear all elements in the collection.

    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    System.out.println(list); // 输出 [A, B]
    list.clear();
    System.out.println(list); // 输出 []

2. Common methods of the List interface
The List interface is a sub-interface of the Collection interface and represents an ordered collection. It can contain duplicate elements and is sorted in insertion order. The following will introduce the commonly used methods in the List interface and their usage examples:

  1. get(int index): Get the element at the specified index position.

    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    System.out.println(list.get(0)); // 输出 A
  2. set(int index, Object obj): Replace the element at the specified index position with a new element.

    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.set(0, "C");
    System.out.println(list); // 输出 [C, B]
  3. indexOf(Object obj): Get the index of the first occurrence of the specified element in the collection.

    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    System.out.println(list.indexOf("A")); // 输出 0
    System.out.println(list.indexOf("C")); // 输出 -1
  4. remove(int index): Remove the element at the specified index position.

    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.remove(0);
    System.out.println(list); // 输出 [B]
  5. subList(int fromIndex, int toIndex): Get the subcollection within the specified range.

    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.add("C");
    List<String> subList = list.subList(0, 2);
    System.out.println(subList); // 输出 [A, B]

3. Common methods of Set interface
Set interface is a sub-interface of Collection interface, which represents a collection that does not allow duplicate elements. The following will introduce the commonly used methods in the Set interface and their usage examples:

  1. #add(Object obj): Add an element to the collection.

    Set<Integer> set = new HashSet<>();
    set.add(1);
    set.add(2);
    System.out.println(set); // 输出 [1, 2]
  2. remove(Object obj): Remove the specified element from the collection.

    Set<Integer> set = new HashSet<>();
    set.add(1);
    set.add(2);
    set.remove(1);
    System.out.println(set); // 输出 [2]
  3. contains(Object obj): Determine whether the specified element is contained in the collection.

    Set<Integer> set = new HashSet<>();
    set.add(1);
    set.add(2);
    System.out.println(set.contains(1)); // 输出 true
    System.out.println(set.contains(3)); // 输出 false
  4. iterator(): Gets the iterator of the collection, used to traverse the elements in the collection.

    Set<Integer> set = new HashSet<>();
    set.add(1);
    set.add(2);
    Iterator<Integer> iterator = set.iterator();
    while (iterator.hasNext()) {
     System.out.println(iterator.next());
    }

4. Common methods of Map interface
The Map interface is an interface used for key-value storage in the Java collection framework. The following will introduce the commonly used methods in the Map interface and their usage examples:

  1. put(Object key, Object value): Add a key-value pair to the Map.

    Map<String, Integer> map = new HashMap<>();
    map.put("A", 1);
    map.put("B", 2);
    System.out.println(map); // 输出 {A=1, B=2}
  2. get(Object key): Get the corresponding value based on the specified key.

    Map<String, Integer> map = new HashMap<>();
    map.put("A", 1);
    map.put("B", 2);
    System.out.println(map.get("A")); // 输出 1
    System.out.println(map.get("C")); // 输出 null
  3. remove(Object key): Remove the corresponding key-value pair based on the specified key.

    Map<String, Integer> map = new HashMap<>();
    map.put("A", 1);
    map.put("B", 2);
    map.remove("A");
    System.out.println(map); // 输出 {B=2}
  4. containsKey(Object key): Determine whether the Map contains the specified key.

    Map<String, Integer> map = new HashMap<>();
    map.put("A", 1);
    map.put("B", 2);
    System.out.println(map.containsKey("A")); // 输出 true
    System.out.println(map.containsKey("C")); // 输出 false
  5. containsValue(Object value): Determine whether the Map contains the specified value.

    Map<String, Integer> map = new HashMap<>();
    map.put("A", 1);
    map.put("B", 2);
    System.out.println(map.containsValue(1)); // 输出 true
    System.out.println(map.containsValue(3)); // 输出 false

The above are commonly used methods and usage examples in Java collections (Collection). By learning these methods, you can manipulate the data in the collection more flexibly and conveniently. In actual development, choosing the appropriate collection classes and methods according to specific needs can improve the readability and efficiency of the code.

The above is the detailed content of Common Java collection operation methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn