本文整理汇总了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.
*/
}