Java随笔-HashSet和输入流
HashSet
今天做题正好用到了HashSet那就顺便总结一下。
定义
HashSet是Java集合Set的一个实现类,Set是一个接口,其实现类除HashSet之外,还有TreeSet,并继承了Collection
举例
private transient HashMap
<E,Object
> map
;
public HashSet() {
map
= new HashMap<>();
}
public HashSet(Collection
<? extends E> c
) {
map
= new HashMap<>(Math
.max((int
) (c
.size()/.75f
) + 1, 16));
addAll(c
);
}
public HashSet(int initialCapacity
, float loadFactor
) {
map
= new HashMap<>(initialCapacity
, loadFactor
);
}
public HashSet(int initialCapacity
) {
map
= new HashMap<>(initialCapacity
);
}
实例
public final
static HashSet
<Character
> vowels
= new HashSet<>(
Arrays
.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')
);
输入流
BufferedReader br
= new BufferedReader(System
.in);
String str
= null;
try {
str
= br
.readLine();
} catch (IOException e
) {
e
.printStackTrace();
}
Scanner sc
= new Scanner(System
.in);
String name
= sc
.nextLine();
int age
= sc
.nextInt();