Map
(1)存储键值对<key,value>
(2)key唯一,value可以重复
(3)无序
1、HashMap
(1)底层由哈希标(数组、链表、红黑树)实现
(2)查询、增删效率高数据类型的对象时,
(3)HashMap的key存储自定义,需要重写equals和hashCode方法
HashMap遍历
1、获取key集合,根据key获取对象value
Set<Integer> keys = map.keySet();
System.out.println(keys);
for(int k:keys) {
System.out.println(k+"---"+map.get(k));
}
2、取出所有values进行遍历
Collection<String> values = map.values();
System.out.println(values);
for(String v:values) {
System.out.println(v);
}
3、把map集合中的每一个映射封装成为Map.Entry类型的数据,放入一个set集合中返回
Set<Map.Entry<Integer,String>> set = map.entrySet();
for(Map.Entry<Integer,String> e:set) {
System.out.println(e.getKey()+"---"+e.getValue());
}
2、TreeMap
(1)底层红黑树
(2)默认升序存储数据
(3)去重是根据key
(4)TreeMap的key存储的是自定义引用数据类型,需要实现内部|外部比较器
public class MapDemo02 {
public static void main(String[] args) {
//定义容器HashMap
Map<Person,String> map = new HashMap<Person,String>();
map.put(new Person("张三",23),"上海");
map.put(new Person("张三",23),"北京");
map.put(new Person("张三",33),"上海");
map.put(new Person("张三",23),"广州");
System.out.println(map);
//定义容器TreeMap
Map<Person,String> tmap = new TreeMap<Person,String>((Person p1,Person p2)->{return p1.getAge()-p2.getAge();});
tmap.put(new Person("张三",23),"上海");
tmap.put(new Person("里斯",25),"北京");
tmap.put(new Person("李四",31),"上海");
tmap.put(new Person("王五",33),"广州");
System.out.println(tmap);
}
}
public class Person implements Serializable{
private String name;
private int 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;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
Properties
实现了Map接口的容器类,可保存在流中或在流中加载,常用来作为配置文件,实现软编码
public class Demo01 {
public static void main(String[] args) throws IOException {
Properties pro = new Properties();
pro.setProperty("上海", "东方明珠");
System.out.println(pro);
//load 从输入流中读取属性文件 key value
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
System.out.println(pro.getProperty("name"));
System.out.println(pro.getProperty("pwd"));
}
}
db.properties
name=alen
pwd=12345
Collections工具类
提供了对容器操作的工具方法
1、基础使用
public class CollectionsDemo01 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(10);
list.add(3);
list.add(4);
list.add(89);
list.add(13);
list.add(11);
list.add(0);
System.out.println(list);
//随机排序
Collections.shuffle(list);
System.out.println(list);
//反转
Collections.reverse(list);
System.out.println(list);
//升序排序
Collections.sort(list);
System.out.println(list);
System.out.println(Collections.binarySearch(list, 11));
// Collections.fill(list, 999);
// System.out.println(list);
}
}
2、自定义引用数据类型的数据,实现Comparable接口的comparaTo方法确定排序规则
Student类
@Override
public int compareTo(Student o) {
//根据学生成绩排序
if(this.score==o.score) {
return 0;
}else if(this.score>o.score) {
return 1;
}else {
return -1;
}
}
3、自定义引用数据类型的数据,实现Comparator接口的compara方法确定排序规则
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();
}});
Collections.sort(list, (s1,s2)->s1.getAge()-s2.getAge());
4、Stream
(1)stream数据渠道,用于操作数据源所生产的元素序列
(2)操作步骤:创建 中间操作 终止
(3)惰性求值
//1、顺序流 Stream
List<String> list = new ArrayList<String>();
Stream<String> stream1 = list.stream();
//2、并行流
List<String> list2 = new ArrayList<String>();
Stream<String> stream2 = list.parallelStream();
//3、通过Arrays的静态方法stream()获取数据流
Integer[] ints = new Integer[10];
Stream<Integer> stream3 = Arrays.stream(ints);
//4、通过stream类的静态方法of获取数据流
Stream<String> stream4 = Stream.of("on","off");
public static void test01(List<Student2> s) {
Stream<Student2> stream = s.stream()
.filter((s1)->{return s1.getAge()>21;})
.limit(4)//截流,限制元素不超过给定的数量
.skip(1)//跳过,返回扔掉前n个元素的流,若流中不足n个返回空流
.distinct();//去重
//一次性执行完全部内容,即惰性求值
stream.forEach(System.out::println);
}