本文整理匯總了Java中java.util.LinkedHashSet.clear方法的典型用法代碼示例。如果您正苦於以下問題:Java LinkedHashSet.clear方法的具體用法?Java LinkedHashSet.clear怎麽用?Java LinkedHashSet.clear使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.LinkedHashSet
的用法示例。
在下文中一共展示了LinkedHashSet.clear方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: sortByPriority
import java.util.LinkedHashSet; //導入方法依賴的package包/類
private static final void sortByPriority(HashMap<String, LinkedHashSet<String>> map) {
for (LinkedHashSet<String> set : map.values()) {
try {
ArrayList<Class<?>> classes = new ArrayList<>();
for (String className : set) {
classes.add(Class.forName(className));
}
classes.sort((c1, c2) -> {
Priority p1 = c1.getAnnotation(Priority.class);
Priority p2 = c2.getAnnotation(Priority.class);
int v1 = p1 == null ? 1000 : p1.value();
int v2 = p2 == null ? 1000 : p2.value();
return v2 - v1;
});
set.clear();
for (Class<?> c: classes) {
set.add(c.getName());
}
} catch (Throwable cause) {
cause.printStackTrace();
}
}
}
示例2: main
import java.util.LinkedHashSet; //導入方法依賴的package包/類
public static void main(String[] args) {
//create object of LinkedHashSet
LinkedHashSet lhashSet = new LinkedHashSet();
//add elements to LinkedHashSet object
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
System.out.println("LinkedHashSet before removal : " + lhashSet);
/*
To remove all elements from Java LinkedHashSet or to clear LinkedHashSet
object use,
void clear() method.
This method removes all elements from LinkedHashSet.
*/
lhashSet.clear();
System.out.println("LinkedHashSet after removal : " + lhashSet);
/*
To check whether LinkedHashSet contains any elements or not
use
boolean isEmpty() method.
This method returns true if the LinkedHashSet does not contains any elements
otherwise false.
*/
System.out.println("Is LinkedHashSet empty ? " + lhashSet.isEmpty());
/*
Please note that removeAll method of Java LinkedHashSet class can
also be used to remove all elements from LinkedHashSet object.
*/
}