目录
一、Set接口
1.Set接口介绍
java.util.Set
接口和java.util.List
接口一样,同样继承自Collection
接口,与List
接口不同的是,Set
接口中元素无序,并且都会以某种规则保证存入的元素不出现重复。
Set
集合有多个子类,这里我们介绍其中的java.util.HashSet
、java.util.LinkedHashSet
这两个集合。
Set集合取出元素的方式可以采用:迭代器、增强for。不能使用普通for循环
2.HashSet集合(实现类)
java.util.HashSet
是Set
接口的一个实现类,它所存储的元素是不可重复的,并且元素都是无序的(即存取顺序不一致)。java.util.HashSet
底层的实现其实是一个java.util.HashMap
。
HashSet
是根据对象的哈希值来确定元素在集合中的存储位置,因此具有良好的存取和查找性能。保证元素唯一性的方式依赖于:hashCode
与equals
方法。
set去重的规律:
- 添加一个对象的时候,先查看set集合中是否有相同的hash值
- 如果有,继续查看equals方法
- 如果equals方法相同,认为已经有了相同的元素,不再添加
- 如果没有相同hashCode,直接就认为没有重复的元素,直接添加进 去新的元素
1)简单方法的使用:
public class HashSetDemo {
public static void main(String[] args) {
//创建 Set集合
HashSet<String> set = new HashSet<String>();
//添加元素
set.add(new String("cba"));
set.add("abc");
set.add("bac");
set.add("cba");
//遍历
for (String name : set) {
System.out.println(name);
}
}
}
输出结果如下,说明集合中不能存储重复元素:
cba
abc
bac
2)HashSet存储自定义类型元素
给HashSet中存放自定义类型元素时,需要重写对象中的hashCode和equals方法,建立自己的比较方式,才能保证HashSet集合中的对象唯一
创建自定义Student类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
public class HashSetDemo2 {
public static void main(String[] args) {
//创建集合对象 该集合中存储 Student类型对象
HashSet<Student> stuSet = new HashSet<Student>();
//存储
Student stu = new Student("于谦", 43);
stuSet.add(stu);
stuSet.add(new Student("郭德纲", 44));
stuSet.add(new Student("于谦", 43));//是添不进去
stuSet.add(new Student("郭麒麟", 23));
stuSet.add(stu);
for (Student stu2 : stuSet) {
System.out.println(stu2);
}
}
}
执行结果:
Student [name=郭德纲, age=44]
Student [name=于谦, age=43]
Student [name=郭麒麟, age=23]
3.HashSet的遍历
1)增强for循环
public class HashSet001 {
public static void main(String[] args) {
// 定义一个容器存储我们班同学的名字(不能重名,没有顺序的)
HashSet<String> hs = new HashSet<>();
//添加元素
hs.add("张三");
hs.add("李四");
hs.add("王五");
//hs.add("张三");
//获取一下容器的长度
System.out.println(hs.size());//3
//HashSet不能用普通for循环来遍历
//1.增强for循环
for(String i : hs) {
System.out.println(i);//李四 张三 王五
}
}
}
2)使用迭代器来遍历
public class HashSet001 {
public static void main(String[] args) {
// 定义一个容器存储我们班同学的名字(不能重名,没有顺序的)
HashSet<String> hs = new HashSet<>();
//添加元素
hs.add("张三");
hs.add("李四");
hs.add("王五");
//hs.add("张三");
//用迭代器来遍历
Iterator<String> it = hs.iterator();
//判断是否有下一个元素
while (it.hasNext()){
//获取下一个元素
String s = it.next();
System.out.println(s);
}//李四 张三 王五
}
}
4.TreeSet集合(实现类)
TreeSet是一个有序的集合,它的作用是提供有序的Set集合。 TreeSet 继承于AbstractSet,所以它是一个Set集合,具有Set的属性和方法。 TreeSet是基于TreeMap实现的。TreeSet中的元素支持2种排序方式:自然排序或者根据创建TreeSet 时提供的 Comparator 进行排序。这取决于使用的构造方法。
二、Map接口
1.概述
Collection
中的集合,元素是孤立存在的(理解为单身),向集合中存储元素采用一个个元素的方式存储。Map
中的集合,元素是成对存在的(理解为夫妻)。每个元素由键与值两部分组成,通过键可以找对所对应的值。Collection
中的集合称为单列集合,Map
中的集合称为双列集合。- 需要注意的是,
Map
中的集合不能包含重复的键,值可以重复;每个键只能对应一个值。
2. Map常用子类
HashMap<K,V>:存储数据采用的哈希表结构,元素的存取顺序不能保证一致。由于要保证键的唯一、不重复,需要重写键的hashCode()方法、equals()方法
Map接口中的集合都有两个泛型变量<K,V>,在使用时,要为两个泛型变量赋予数据类型。两个泛型变量<K,V>的数据类型可以相同,也可以不同。
3.Map接口中的常用方法
Map接口中定义了很多方法,常用的如下:
public V put(K key, V value)
: 把指定的键与指定的值添加到Map集合中。public V remove(Object key)
: 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。public V get(Object key)
根据指定的键,在Map集合中获取对应的值。boolean containsKey(Object key)
判断集合中是否包含指定的键。public Set<K> keySet()
: 获取Map集合中所有的键,存储到Set集合中。public Set<Map.Entry<K,V>> entrySet()
: 获取到Map集合中所有的键值对对象的集合(Set集合)。
Map接口的方法演示:
public class MapDemo {
public static void main(String[] args) {
//创建 map对象
HashMap<String, String> map = new HashMap<String, String>();
//添加元素到集合
map.put("黄晓明", "杨颖");
map.put("文章", "马伊琍");
map.put("邓超", "孙俪");
System.out.println(map);
//String remove(String key)
System.out.println(map.remove("邓超"));
System.out.println(map);
// 想要查看 黄晓明的媳妇 是谁
System.out.println(map.get("黄晓明"));
System.out.println(map.get("邓超"));
}
}
使用put方法时,若指定的键(key)在集合中没有,则没有这个键对应的值,返回null,并把指定的键值添加到集合中;
若指定的键(key)在集合中存在,则返回值为集合中键对应的值(该值为替换前的值),并把指定键所对应的值,替换成指定的新值。
4.Map的遍历
1)第一种方式
将所有的key 获取出来放到set集合中,遍历set集合 , get(key)
public class Map03 {
public static void main(String[] args) {
//创建一个Map容器
Map<Integer,String> map = new HashMap<>();
//添加元素
map.put(1,"壹");
map.put(2,"贰");
map.put(3,"叁");
map.put(4,"肆");
map.put(5,"伍");
System.out.println("===============================");
// 第一种方式
// 将所有的key 获取出来放到set集合中,遍历set集合 , get(key)
// 1、获取所有的key
Set<Integer> keys = map.keySet();
//增强for
for(Integer key : keys){
String value = map.get(key);
System.out.println(key+"---"+value);
}
System.out.println("-------------------------");
//迭代器
Iterator<Integer> it = keys.iterator();
while(it.hasNext()){
Integer key = it.next();
String value = map.get(key);
System.out.println(key+"----"+value);
}
}
}
2)第二种方式
将Map集合中的键值对,利用Entry对象来进行包装,将所有的Entry对象放入一个set集合中
public class Map03 {
public static void main(String[] args) {
//创建一个Map容器
Map<Integer,String> map = new HashMap<>();
//添加元素
map.put(1,"壹");
map.put(2,"贰");
map.put(3,"叁");
map.put(4,"肆");
map.put(5,"伍");
//第二种方式
//将Map集合中的键值对,利用Entry对象来进行包装,将所有的Entry对象放入一个set集合中
Set<Map.Entry<Integer,String>> entries = map.entrySet();
//增强for循环
for(Map.Entry<Integer,String> e : entries){
Integer key = e.getKey();
String value = e.getValue();
System.out.println(key+"---"+value);
}
System.out.println("-----------------------------");
//迭代器
Iterator<Map.Entry<Integer,String>> its = entries.iterator();
while(its.hasNext()){
Map.Entry<Integer,String> e = its.next();
Integer key = e.getKey();
String value = e.getValue();
System.out.println(key+"---"+value);
}
}
}
3)第三种方式
只能获取value,利用values()方法
public class Map03 {
public static void main(String[] args) {
//创建一个Map容器
Map<Integer,String> map = new HashMap<>();
//添加元素
map.put(1,"壹");
map.put(2,"贰");
map.put(3,"叁");
map.put(4,"肆");
map.put(5,"伍");
//第三种方式,只能获取value
//利用values()方法
Collection<String> c = map.values();
for(String i : c){
System.out.println(i);
}
System.out.println("------------------");
Iterator<String> itss = c.iterator();
while(itss.hasNext()){
System.out.println(itss.next());
}
}
}
三、properties
Properties为Hashtable的子类,要求键与值只能为字符串,不能为null,长与配置文件(与外界交互的信息) 即内存与存储介质(文件、数据库、网络、服务器内存等)交互
public class properties001 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
// 加载指定的文件中的内容到 Properties容器中
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
//获取uname,upwd
System.out.println(properties.getProperty("uname"));//root
System.out.println(properties.getProperty("upwd"));//123456
}
}