Home >Java >javaTutorial >How to use collection functions in Java for collection operations and traversal
How to use set functions for set operations and traversal in Java
Introduction:
In Java, sets are very commonly used data structures that can store Elements of any type, and provides many convenient functions for set operations. This article will introduce how to use set functions to perform set operations and traversals, which can help improve our code efficiency and development efficiency in actual development.
1. Set operations:
Add elements: Use the add() function to add elements to the set. For example, we want to create a set containing integers and add elements 1, 2, 3 to it:
List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3);
Remove elements: Use the remove() function to remove the specified element from the set . For example, we delete element 2 in the collection:
list.remove(Integer.valueOf(2));
Get the element: Use the get() function to get the element at the specified position. For example, we get the first element in the collection:
Integer element = list.get(0);
Modify the element: Use the set() function to modify the element at the specified position. For example, we modify the first element in the collection to 4:
list.set(0, 4);
Find the element: Use the contains() function to determine whether the collection contains the specified element. For example, we determine whether the set contains element 3:
boolean containsElement = list.contains(3);
2. Set traversal:
Use a for loop to traverse the set. For example, we want to iterate through a collection containing integers and print the value of each element:
for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); }
Use an enhanced for loop to traverse the collection. For example, we want to iterate over a collection containing strings and print the value of each element:
for (String element : list) { System.out.println(element); }
Use an iterator to traverse the collection. For example, we want to iterate over a collection containing floating point numbers and print the value of each element:
Iterator<Double> iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
Iterate over the collection using Java 8’s Stream API. For example, we want to iterate over a set containing integers and calculate the sum of all elements:
int sum = list.stream().mapToInt(Integer::intValue).sum(); System.out.println(sum);
Summary:
This article explains how to use set functions in Java for set operations and is traversed, and corresponding code examples are provided. Understanding and mastering the use of set functions can improve code efficiency and development efficiency in actual development. Hope this article helps you!
The above is the detailed content of How to use collection functions in Java for collection operations and traversal. For more information, please follow other related articles on the PHP Chinese website!