public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
//添加元素
boolean f = set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
System.out.println(f);
}
我们打断点调试一下:
可以看到愚生浅末四个字符已经装入set,且f为true证明添加成功。
我们再试试删除:
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
//添加元素
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
boolean f = set.remove("生");
}
可以看到set已经没有生了,且f为true代表删除成功。
判断是否存在:
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
//添加元素
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
boolean f = set.contains("末");
}
末是存在于set的,所以返回值为true。
获取集合的大小:
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
//添加元素
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
//获取集合的大小
int size = set.size();
添加了愚生浅末四个字符,所以可以得到size是4.
遍历
1.转换为数组遍历
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
String[] strings = set.toArray(new String[0]);
for (int i = 0; i < strings.length; i++) {
System.out.println(strings[i]);
}
}
结果:
前面说过:存储元素的顺序和遍历获取出来的顺序可能不一致。
2.使用迭代器遍历
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
Iterator<String> it = set.iterator();
while (it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
结果:
3.foreach遍历
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
for (String s : set) {
System.out.println(s);
}
}
结果:
常用Map集合
Map集合的概述
Map接口是双列集合的顶层接口,下面是Map接口的定义
interface Map<K,V> K:键的类型;V:值的类型
存储的数据必须包含key和value。
key和value在Map集合中是一一对应的关系。一个key对应一个value。
key在map集合中是不会重复的。
HashMap
HashMap集合的特点
底层数据结构是哈希表
存储元素的顺序和遍历获取出来的顺序可能不一致
key不会重复
创建对象
HashMap<key的数据类型,value的数据类型> map = new HashMap<>();
例如:
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<>();
HashMap<String,Integer> map = new HashMap<>();
}
常用方法
方法
解释
V put(K key, V value)
添加元素,如果key不存在就添加,如果key
V get(Object key)
根据key获取对应的value值返回。如果key不存在就返回null
V remove(Object key)
根据key删除map中对应的键值对。并且把删除的value返回
boolean containsKey(Object key)
判断key是否存在
int size()
集合中键值对的对数
void clear()
清空集合中的所有键值对
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<>();
// map.put()
//添加元素
map.put("name", "愷龍");
map.put("age", "20");
String v = map.put("name", "愚生浅末");//将原来的愷龍替换为愚生浅末
String name = map.get("name");//获取名字:愷龍
String age = map.get("age");//获取age:20
//删除元素
String delV = map.remove("age");//返回值为20
//判断key是否存在
if(map.containsKey("name")){
String agea = map.get("name");//null
System.out.println(agea.length());
}
//size
int size = map.size();
map.clear();
}
遍历
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
//添加元素
boolean f = set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
System.out.println(f);
}
我们打断点调试一下:
可以看到愚生浅末四个字符已经装入set,且f为true证明添加成功。
我们再试试删除:
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
//添加元素
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
boolean f = set.remove("生");
}
可以看到set已经没有生了,且f为true代表删除成功。
判断是否存在:
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
//添加元素
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
boolean f = set.contains("末");
}
末是存在于set的,所以返回值为true。
获取集合的大小:
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
//添加元素
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
//获取集合的大小
int size = set.size();
添加了愚生浅末四个字符,所以可以得到size是4.
遍历
1.转换为数组遍历
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
String[] strings = set.toArray(new String[0]);
for (int i = 0; i < strings.length; i++) {
System.out.println(strings[i]);
}
}
结果:
前面说过:存储元素的顺序和遍历获取出来的顺序可能不一致。
2.使用迭代器遍历
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
Iterator<String> it = set.iterator();
while (it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
结果:
3.foreach遍历
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("愚");
set.add("生");
set.add("浅");
set.add("末");
for (String s : set) {
System.out.println(s);
}
}
结果:
常用Map集合
Map集合的概述
Map接口是双列集合的顶层接口,下面是Map接口的定义
interface Map<K,V> K:键的类型;V:值的类型
存储的数据必须包含key和value。
key和value在Map集合中是一一对应的关系。一个key对应一个value。
key在map集合中是不会重复的。
HashMap
HashMap集合的特点
底层数据结构是哈希表
存储元素的顺序和遍历获取出来的顺序可能不一致
key不会重复
创建对象
HashMap<key的数据类型,value的数据类型> map = new HashMap<>();
例如:
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<>();
HashMap<String,Integer> map = new HashMap<>();
}
常用方法
方法
解释
V put(K key, V value)
添加元素,如果key不存在就添加,如果key
V get(Object key)
根据key获取对应的value值返回。如果key不存在就返回null
V remove(Object key)
根据key删除map中对应的键值对。并且把删除的value返回
boolean containsKey(Object key)
判断key是否存在
int size()
集合中键值对的对数
void clear()
清空集合中的所有键值对
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<>();
// map.put()
//添加元素
map.put("name", "愷龍");
map.put("age", "20");
String v = map.put("name", "愚生浅末");//将原来的愷龍替换为愚生浅末
String name = map.get("name");//获取名字:愷龍
String age = map.get("age");//获取age:20
//删除元素
String delV = map.remove("age");//返回值为20
//判断key是否存在
if(map.containsKey("name")){
String agea = map.get("name");//null
System.out.println(agea.length());
}
//size
int size = map.size();
map.clear();
}
遍历