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 方法 示例