韩顺平Java31——集合

集合

学习注意点:具体应用场景和源代码的阅读

 

  •  集合框架体系图(背下来!!!)

单列集合:

 双列集合:

Collection接口

  • 常用方法

  • collection接口遍历方法

1.Iterator迭代器

 

package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author 紫英
 * @version 1.0
 * @discription
 */
public class Col01 {
    public static void main(String[] args) {
        Collection coll = new ArrayList();
        coll.add(new Book("《三国演义》", "罗贯中", 56.9));
        coll.add(new Book("《水浒传》", "施耐庵", 46.9));
        coll.add(new Book("《西游记》", "吴承恩", 66.9));
        Iterator iterable = coll.iterator();

        while (iterable.hasNext()) {
            //快捷键itit
            Object next = iterable.next();
            System.out.println(next);
        }
//        退出while循环后迭代器指向的是集合的最后一个元素
//        这时如果再使用next会抛出异常
//        iterable.next();//NoSuchElementException
//        想要再次遍历需要重置迭代器
        iterable=coll.iterator();
        System.out.println("第二次遍历:");
        while (iterable.hasNext()) {
            Object next = iterable.next();
            System.out.println(next);
        }
    }
}

class Book {
    private String name;
    private String author;
    private double price;

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }
}

2.增强for循环

 

 List接口

 

 

  •  常用方法

6.返回值是该元素

 7.因为是替换所以不能替换不存在的会抛出越界异常

8.[fromIndex,toIndex)前闭后开

  • 三种遍历方式

package collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 紫英
 * @version 1.0
 * @discription list接口三种遍历方式
 */
public class Col02 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
//        1.迭代器
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println(next);
        }
//        2.增强for
        for (Object o :list) {
            System.out.println(o);
        }
//        3.普通for
        for (int i = 0; i < list.size(); i++) {
            Object o= list.get(i);
            System.out.println(o);

        }
    }
}

Set接口

  • 基本介绍

(1)无序——添加和取出的顺序不一致,虽然取出顺序和存放顺序不一致,但是也是有固定的顺序

(2)不允许元素重复,所以最多包含一个null

(3)所有已知实现类:

AbstractSetConcurrentHashMap.KeySetViewConcurrentSkipListSetCopyOnWriteArraySetEnumSet HashSetJobStateReasonsLinkedHashSet TreeSet

 

package collection.set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author 紫英
 * @version 1.0
 * @discription
 */
public class SetMethod {
    public static void main(String[] args) {
        //以Set接口的实现类HashSet来演示Set接口的方法
        Set hashset = new HashSet();
        hashset.add("111");
        hashset.add("222");
        hashset.add("222");
        hashset.add("333");
        hashset.add(null);
        hashset.add(null);
        System.out.println("hashset = "+hashset);

        //遍历
        // 1.迭代器遍历
        System.out.println("===迭代器遍历===");
        Iterator iterator = hashset.iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println(next);

        }

        //2.增强for遍历
        System.out.println("===增强for遍历===");
        for (Object o : hashset) {
            System.out.println(o);
        }


    }
}

 Map接口

  • 基本介绍

3)说明:当有相同的key时会将原来的value替换掉

8)以HashMap为例,其中有许多内部类。实际上的kv值是用HashMap$Node类型的数组来存储,但是为了方便遍历,用Entry类型的数据指向每一个Node(Node类型实现了Map.Entry接口),之后再将Entry存放在HashMap$EntrySet类型的Set之中,其中有getKey和getValue()方法

 可以看到还有HashMap$KeySet类型的keyset和HashMap$Values类型的values

  • 常用方法

  • 遍历方式

package collection.map;

import java.util.*;

/**
 * @author 紫英
 * @version 1.0
 * @discription map遍历方式
 */
@SuppressWarnings("all")
public class Map01 {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("01","jack");
        map.put("02","marry");
        //遍历方式
        //第一种:先取出所有的key,通过key取出对应的value
        //(1)增强for
        System.out.println("====第一种.(1)====");
        Set keyset = map.keySet();
        for (Object key : keyset) {
            System.out.println(key + "-" + map.get(key));
        }
        //(2)迭代器
        System.out.println("====第一种.(2)====");
        Iterator iterator = keyset.iterator();
        while (iterator.hasNext()) {
            Object key =  iterator.next();
            System.out.println(key + "-" + map.get(key));

        }
        //第二种:先取出所有的value,但是这种不能反向找key
        //因为类型是Collection,所以可以使用对应的遍历方式(因为没有索引值所以不能普通for)
        Collection values = map.values();
        //(1)增强for
        System.out.println("====第二种.(1)====");
        for (Object value : values) {
            System.out.println(value);
        }
        //(2)迭代器
        System.out.println("====第二种.(2)====");
        Iterator iterator2 = values.iterator();
        while (iterator2.hasNext()) {
            Object next =  iterator2.next();
            System.out.println(next);

        }
        //第三种:通过EntrySet来获取kv值
        //(1)增强for
        System.out.println("====第三种.(1)====");
        Set entrySet = map.entrySet();
        for (Object o : entrySet) {
            Map.Entry m = (Map.Entry) o;
            System.out.println(m.getKey()+ "-" + m.getValue());
        }
        //(2)迭代器
        System.out.println("====第三种.(2)====");
        Iterator iterator3 = entrySet.iterator();
        while (iterator3.hasNext()) {
            Object o =  iterator3.next();
            Map.Entry m = (Map.Entry) o;
            System.out.println(m.getKey()+ "-" + m.getValue());
        }

    }

}

  •  map练习

package collection.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author 紫英
 * @version 1.0
 * @discription
 */
public class Homework01 {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("01",new Employee("jack","01",19000));
        map.put("02",new Employee("marry","02",14000));
        map.put("03",new Employee("jack","03",17000));
        System.out.println(map);
        Set set = map.entrySet();
        System.out.println("工资>18000的员工(第一种方式——entrySet)");
        for (Object o : set) {
            Map.Entry m = (Map.Entry) o;
            Employee e1 = (Employee)m.getValue();
            if (e1.salary>18000){
                System.out.println(m.getKey() + "-" + m.getValue());
            }
        }
        System.out.println("工资>18000的员工(第一种方式——entrySet.迭代器)");
        Iterator iterator1 = set.iterator();
        while (iterator1.hasNext()) {
            Object o = iterator1.next();
            Map.Entry m = (Map.Entry) o;
            Employee e = (Employee)m.getValue();
            if (e.salary>18000){
              System.out.println(m.getKey() + "-" + m.getValue());
            }
        }

        System.out.println("工资>18000的员工(第二种方式——keyset)");
        Set keyset = map.keySet();
        Iterator iterator = keyset.iterator();
        while (iterator.hasNext()) {
            Object key =  iterator.next();
            Employee e = (Employee)map.get(key);
            if (e.salary>18000){
                System.out.println(key + "-" +map.get(key) );
            }

        }

    }
}

class Employee{
    String name;
    String id;
    public double salary;

    public Employee(String name, String id, double salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                ", salary=" + salary +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
 
posted @ 2021-12-29 15:00  紫英626  阅读(82)  评论(0编辑  收藏  举报

紫英