Home  >  Article  >  Java  >  How to use collection framework functions to add, delete, modify and query collections in Java

How to use collection framework functions to add, delete, modify and query collections in Java

WBOY
WBOYOriginal
2023-10-25 08:45:131451browse

How to use collection framework functions to add, delete, modify and query collections in Java

How to use collection framework functions in Java to add, delete, modify, and query collections

In Java, the Collection Framework (Collection Framework) provides a series of classes and interfaces To facilitate our collection operations. These classes and interfaces contain a wealth of functions that allow us to add, delete, modify, and search collections more conveniently. Below we'll detail how to use collections framework functions to perform these operations, and provide specific code examples.

  1. Add operation of collection

In Java, you can use the add() method to add elements to a collection. First, we need to create the collection object and then add elements to the collection by calling the add() method.

Code example:

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

public class CollectionAddExample {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        
        // 向集合中添加元素
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("orange");
        
        System.out.println(fruits); // 输出:[apple, banana, orange]
    }
}
  1. Delete operation of collection

In Java, you can use the remove() method to remove from a collection Delete the specified element. We can delete by the value of the element or by the index of the element.

Code example:

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

public class CollectionRemoveExample {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("orange");
        
        // 删除指定元素
        fruits.remove("banana");
        
        System.out.println(fruits); // 输出:[apple, orange]
        
        // 根据索引删除元素
        fruits.remove(1);
        
        System.out.println(fruits); // 输出:[apple]
    }
}
  1. Collection modification operation

In Java, you can use the set() method to modify the collection element at the specified position.

Code example:

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

public class CollectionModifyExample {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("orange");
        
        // 修改指定位置的元素
        fruits.set(1, "grape");
        
        System.out.println(fruits); // 输出:[apple, grape, orange]
    }
}
  1. Search operation of collection

In Java, you can use the contains() method to determine the collection Whether the specified element is included, use the get() method to get the element based on the index.

Code example:

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

public class CollectionFindExample {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("orange");
        
        // 判断集合是否包含指定元素
        boolean containsApple = fruits.contains("apple");
        boolean containsGrape = fruits.contains("grape");
        
        System.out.println("Contains apple: " + containsApple); // 输出:Contains apple: true
        System.out.println("Contains grape: " + containsGrape); // 输出:Contains grape: false
        
        // 根据索引获取元素
        String fruit = fruits.get(1);
        
        System.out.println("Fruit at index 1: " + fruit); // 输出:Fruit at index 1: banana
    }
}

Through the above example, we can see that the collection framework provides a wealth of functions to facilitate us to add, delete, modify, and query collections. According to the specific needs, we can choose the appropriate method to operate the collection, simplifying and improving our programming efficiency.

The above is the detailed content of How to use collection framework functions to add, delete, modify and query collections in Java. 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