黑马程序员--集合III--

来源:互联网 发布:h5小黄人接金币源码 编辑:程序博客网 时间:2024/05/23 14:32

------- <a  target="blank">android培训</a>、<a  target="blank">java培训</a>、期待与您交流! ----------

Set:

Set的特点是:

元素无序,唯一。


Set的子类特点:

HashSet:

如何保证元素的唯一性呢?

底层数据结构是哈希表。依赖于两个方法:hashCode()equals()

执行流程:

先判断hashCode()是否相同:

是:继续执行equals()方法,看其返回值:

           true:说明元素重复,不添加。

           false:说明元素不重复,添加到集合。

否:直接添加到集合。


LinkedHashSet:

1:底层数据结构是链表和哈希表。

2:由链表保证元素有序。

3:由哈希表保证元素唯一。

 

TreeSet:

1:这个集合是可以按照某种规则对元素进行排序。

2:底层数据结构是二叉树。

 

如何保证元素的唯一性的呢?

      根据比较的返回是否为0

如何保证元素的排序的呢?

A:元素具备比较性 (自然排序)

让元素所属的类实现Comparable接口。

B:集合具备比较性 (比较器排序)

在创建集合的时候,在构造参数中传递Comparator接口的子类对象。

 

案例:

//HashSet存储自定义对象并遍历:

import java.util.HashSet;

public class HashSetDemo{

          public static void main(String[] args) {

                     // 创建集合对象

                    HashSet<Student> hs = new HashSet<Student>();

 

                   // 创建元素对象

                   Student s1 = new Student("王强", 19);

                  Student s2 = new Student("肖芳", 20);

                  Student s3 = new Student("张良", 18);

                  Student s4 = new Student("杨梅", 19);

                  Student s5 = new Student("林敏", 17);

                 Student s6 = new Student("王蓝", 21;

                 Student s7 = new Student("王强", 19);

 

                // 添加元素

                hs.add(s1);

                hs.add(s1);

                hs.add(s2);

                hs.add(s3);

                hs.add(s4);

                hs.add(s5);

                hs.add(s6);

                hs.add(s7);

 

               // 遍历集合

              for (Student s : hs) {

                   System.out.println(s.getName() + "---" + s.getAge());

               }

      }

}

 

 

//TreeSet存储自定义对象并遍历:

import java.util.Comparator;

import java.util.TreeSet;

public class TreeSetDemo {

            public static void main(String[] args) {

                      TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() 

            public int compare(Student s1, Student s2) {

                       // 主要条件:年龄

                      int num = s1.getAge() - s2.getAge();

                      // 次要条件

                     int num2 = num == 0 ? s1.getName().compareTo(s2.getName()): num;

                         return num2;

            }

 

                       // 创建元素对象

                      Student s1 = new Student("周润发", 20);

                      Student s2 = new Student("周杰伦", 22);

                      Student s3 = new Student("周迅",22);

                      Student s4 = new Student("周星驰", 23);

                      Student s5 = new Student("周杰伦", 21);

                      Student s6 = new Student("周杰伦", 22);

                      Student s7 = new Student("周杰", 24);

 

                     // 把元素添加到集合中

                     ts.add(s1);

                    ts.add(s2);

                    ts.add(s3);

                    ts.add(s4);

                    ts.add(s5);

                    ts.add(s6);

                    ts.add(s7);

 

                   // 遍历集合

                    for (Student s : ts) {

                          System.out.println(s.getName() + "---" + s.getAge());

                  }

           }

}

 

 

 

 

 

Map:

存储的是键值对元素。键是唯一的,值可以重复。

         Map 

        |--HashMap

        |--Hashtable

        |--TreeMap

数据结构:Set的一致。

 

注意:Map集合的数据结构是针对键有效,和值无关。

 

CollectionMap的区别?

A:Collection,是单列集合,儿子List可重复,儿子Set唯一。

B:Map,是双列集合,键是唯一的,值是可重复的。

 

Map接口的功能概述:

A:添加功能

      put(K key,v value)

B:判断功能

     containsKey(Object key)

     containsValue(Object value)

C:删除功能

    remove(Object key)

D:获取功能

    get(K key)

    keySet()

    values()

E:长度功能

     size()

 

Map集合的遍历:

    根据键找值。

A:获取所有键的集合。keySet()

B:遍历键的集合,获取到每一个键。增强for

C:根据键到map集合中找值。get()

案例:

import java.util.HashMap;

import java.util.Set;

/*

 * HashMap集合存储数据并遍历。

 * 键:Student学生对象( Student:name,age )

 * 值:String学号("001",...)

 * 

 * 如果两个学生对象的成员变量值都相同,我们就认为是同一个学生对象。也就是键重复了。

 * Student s1 = new Student("张三",18);

 * Student s2 = new Student("张三",18;

 * 

 * map.put(s1,"001");

 * map.put(s2,"002");

 * 

 * 结果:

 * 张三-----18------001

 */

public class HashMapDemo {

           public static void main(String[] args) {

                      // 创建集合对象

                      HashMap<String, Student> hm = new HashMap<String, Student>();

 

                     // 创建元素

                   Student s1 = new Student("小明", 15);

                   Student s2 = new Student("小丽", 18);

                  Student s3 = new Student("小李", 18);

                  Student s4 = new Student("小张", 20);

                  Student s5 = new Student("小芳", 22);

                  Student s6 = new Student("小明", 15);

 

 

                // 添加元素

                hm.put("001", s1);

                hm.put("003", s2);

                hm.put("002", s3);

                hm.put("004", s4);

                hm.put("005", s5);

                hm.put("006", s6);

 

             // 遍历

            Set<String> set = hm.keySet();

           for (String key : set) {

                Student value = hm.get(key);

                         System.out.println(key + "---" + value.getName() + "----"+value.getAge());

             }

       }

}

 

 ------- <a  target="blank">android培训</a>、<a  target="blank">java培训</a>、期待与您交流! ----------

 详细请查看:http://edu.csdn.net


 

 ------- <a  target="blank">android培训</a>、<a  target="blank">java培训</a>、期待与您交流! ----------

 详细请查看:http://edu.csdn.net


0 0
原创粉丝点击