java 方法 示例
集合類syncedSet()方法 (Collections Class synchronizedSet() method)
synchronizedSet() method is available in java.util package.
可以在java.util包中使用syncedSet ()方法 。
synchronizedSet() method is used to return the synchronized view of the given set (set).
syncedSet()方法用于返回給定集合(集合)的同步視圖。
synchronizedSet() method is a static method, it is accessible with the class name and if we try to access the method with the class object then also we will not get any error.
synchronizedSet()方法是一個靜態方法,可以使用類名進行訪問,如果嘗試使用類對象訪問該方法,則也不會出現任何錯誤。
synchronizedSet() method does not throw an exception at the time of returning the synchronized set.
返回同步集時,syncedSet ()方法不會引發異常。
Syntax:
句法:
public static Set synchronizedSet(Set set);
Parameter(s):
參數:
Set set – represents the set to be viewed in synchronized set.
集集 –表示要在同步集中查看的集。
Return value:
返回值:
The return type of this method is Set, it returns synchronized view of the given set.
此方法的返回類型為Set ,它返回給定集合的同步視圖。
Example:
例:
// Java program to demonstrate the example
// of Set synchronizedSet() method of Collections
import java.util.*;
public class SynchronizedSetOfCollections {
public static void main(String args[]) {
// Instantiates a set object
Set < Integer > set = new HashSet < Integer > ();
// By using add() method is to add
//objects in a hash set
set.add(10);
set.add(20);
set.add(30);
set.add(40);
set.add(50);
// Display HashSet
System.out.println("HashSet: " + set);
// By using SynchronizedSet() method is to
// represent the hash set in synchronized view
set = Collections.synchronizedSet(set);
// Display Synchronized HashSet
System.out.println("Collections.synchronizedSet(set): " + set);
}
}
Output
輸出量
HashSet: [50, 20, 40, 10, 30]
Collections.synchronizedSet(set): [50, 20, 40, 10, 30]
翻譯自: https://www.includehelp.com/java/collections-synchronizedset-method-with-example.aspx
java 方法 示例