我是靠谱客的博主 认真滑板,这篇文章主要介绍黑马程序员——集合之Set和MapJAVA集合之Set和Map,现在分享给大家,希望可以做个参考。

------- android培训、java培训、期待与您交流! ----------

JAVA集合之Set和Map

1、集合体系
----------丨Collecion
----------------丨List
----------------丨Set
----------------------丨HashSet
----------------------丨TreeSet


2、Set集合的特点
Set集合的功能和Collection是一致的。元素不可重复、无序。

3、Set集合的子类
    HashSet :底层数据结构是哈希表。线程不同步。保证元素唯一性的原理:判断元素的 hashCode 值是否相同。如果相同,还 会继续判断元素的 equals 方法,是否为 true

 HashSet保证元素唯一性原码判断过程:

1、调用HashMap的hash()方法,hash()方法通过处理+调用hashCode()方法来获取hash值。

2、在HashTable中查表,如果没有相同hashCode的元素直接添加。

3、如果存在,就和该元素比较hash值&&(地址值||equals值)

4、如果返回false就添加,返回true,就设置Value值,(因为这是在调用hashMap中的方法),这对set没有影响,相当于不添加

            T|reeSet:可以对Set集合中的元素进行排序。默认按照字母的自然排序。底层数据结构是二叉树。保证元素唯一性的依据:比较器方法的返回值。

比较器对象有2个接口:Comparator------->compare()方法

    Comparable-------->compareTo()方法

Comparator和Comparable用法的区别。

Comparator是一个比较器类,用于外部实现,并创建比较器对象传入集合。

Comparable是元素类区实现的,重现CompareTo()方法直接传入集合就可以排序了。


4、Set集合的代码练习

1、HashSet的练习

添加元素,并包含相同的元素,观察输出顺序与结果。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package it.heima.set; import java.util.HashSet; public class HashSetDemo { public static void main(String[] args) { HashSet<String> hashSet=new HashSet<>(); hashSet.add("hello"); hashSet.add("java"); hashSet.add("world"); hashSet.add("world"); for(String item:hashSet){ System.out.println(item); } } }

添加自定义对象:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package it.heima.set; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.HashSet; public class HashSetDemo3 { public static void main(String[] args) throws Exception { Student s1=new Student(10,"张三"); Student s2=new Student(10,"张三"); Student s3=new Student(11,"李四"); Student s5=new Student(11,"张三"); HashSet<Student> hashSet=new HashSet<>(); hashSet.add(s1); hashSet.add(s2); hashSet.add(s3); hashSet.add(s5); System.out.println(hashSet); /*Class c=HashSet.class; //Constructor con=c.getDeclaredConstructor(); //Object obj=con.newInstance(); Method method=c.getDeclaredMethod("add", Object.class); method.invoke(hashSet, "String"); System.out.println(hashSet);*/ } } class Student{ @Override public int hashCode() { return this.name.hashCode()<<2+this.age*15; } @Override public String toString() { return "Student [age=" + age + ", name=" + name + "]"; } int age; String name; public Student() { super(); // TODO Auto-generated constructor stub } public Student(int age, String name) { super(); this.age = age; this.name = name; } @Override public boolean equals(Object obj) { Student s=(Student)obj; return (this.age==s.age)&&(this.name.equals(s.name)); } }
自定义类要重写hashCode()和equals()方法。

Set集合的toString方法:底层是继承自AbstractCollection的toString(),AbstractCollection的toString()重写了Object的toString(),是通过迭代器迭代元素,并通过StringBuilder拼接实现的。


2、TreeSet的代码练习

添加方法的源代码解析:

1、通过调用TreeMap里的put()实现

2、判断Comparator是否为空,否则调用Comparator的compare()

3、是则将key的值向上转型Comparable,调用CompareTo()

TreeSet的代码练习:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package it.heima.set; import java.util.Comparator; import java.util.TreeSet; public class TreeSetDemo1 { public static void main(String[] args) { /*IdComparator id=new IdComparator(); TreeSet<Person> tree=new TreeSet<>(id);*/ TreeSet<Person> tree=new TreeSet<>(); Person s1=new Person(16,"张三", 80); Person s2=new Person(15,"李四", 81); Person s3=new Person(14,"王五", 82); Person s4=new Person(13,"赵六", 83); Person s5=new Person(12,"张三", 80); Person s6=new Person(11,"刘德华", 80); tree.add(s1); tree.add(s2); tree.add(s3); tree.add(s4); tree.add(s5); tree.add(s6); System.out.println(tree); } } class Person implements Comparable<Person>{ @Override public String toString() { return "学生[学号"+id+" 姓名"+name+" 成绩"+score+"]"; } @Override public int compareTo(Person s) { //Student s=(Student)o; int num=this.score-s.score; int num2=num==0?this.name.compareTo(s.name):num; return num2; } int id; String name; int score; public Person(int id, String name, int score) { super(); this.id = id; this.name = name; this.score = score; } } class IdComparator implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { return o1.id-o2.id; } }
注意:优先调用Comparator的compare()方法,如果不存在比较器,再调用自身实现的Comparable的compareTo()方法


5、Map集合

1、Map概述

  Map是双列集合的根接口,存储了Ket和Value,组成了键值对的集合。

Map集合的子类

            Hashtable:底层是哈希表数据结构,不可以存入nullnull值。该集合是线程同步的。JDK1.0,效率低。

            HashMap:底层是哈希表数据结构。允许使用nullnull值,该集合是不同步的。JDK1.2,效率高。

            TreeMap:底层是二叉树数据结构。线程不同步。可以用于给Map集合中的键进行排序。

           MapSet很像。其实Set底层就是使用了Map集合,只不过Set只操作了Map中方法的键,不去管值。

2、Map的使用

主要掌握Map集合的3种遍历方式。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package it.heima.map; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; import java.util.Set; public class MapDemo { public static void main(String[] args) { HashMap<Student, String> hashMap=new HashMap<>(); Student s1=new Student("张三", 10); Student s2=new Student("李四", 11); Student s3=new Student("张三", 10); Student s4=new Student("李四", 10); hashMap.put(s1, "一班"); hashMap.put(s2, "一班"); hashMap.put(s3, "一班"); hashMap.put(s4, "二班"); //Map的三种遍历方式 //第一种keySet() /*Set<Student> key=hashMap.keySet(); for(Student s:key){ System.out.println(s+hashMap.get(s)); }*/ //第二种values() /*Collection<String> value=hashMap.values(); Iterator<String> it= value.iterator(); while(it.hasNext()){ System.out.println(it.next()); }*/ //第三种EntrySet Set<Entry<Student, String>> entry=hashMap.entrySet(); for(Entry<Student, String> s:entry){ System.out.println(s.getKey()+s.getValue()); } } } class Student{ @Override public int hashCode() { return this.name.hashCode()+this.id*12; } @Override public boolean equals(Object obj) { Student s=(Student)obj; return this.name.equals(s.name)&&this.id==s.id; } String name; int id; @Override public String toString() { return "Student [name=" + name + ", id=" + id + "]"; } public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int id) { super(); this.name = name; this.id = id; } }


最后

以上就是认真滑板最近收集整理的关于黑马程序员——集合之Set和MapJAVA集合之Set和Map的全部内容,更多相关黑马程序员——集合之Set和MapJAVA集合之Set和Map内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部