栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java-知识3

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java-知识3

集合类体系结构

集合只能存储引用数据类型

集合特点

提供一种存储空间可变的数据模型,存储的数据容量可以随时发生改变。

Collection集合概述和使用

是单列集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素
JDK不提供此接口的任何直接实现,它提供更具体的子接口(如:set和List)

创建Collection集合的对象

· 多态的方式
· 具体的实现类ArrayList

Collection中常用的方法

Collection结合的遍历

iterator:迭代器,集合的专用遍历方式

Iterator中的常用方法

集合 的使用步骤

List集合

List常用的方法(之前ArrayList中有介绍ArrayList继承List)

并发修改异常(结合源码分析)

ListIterator

增强for语句

List集合子类的特点

linkedList集合特有的功能 set集合 特点

HashSet

HashSet对集合的迭代顺序不做保证

哈希值

对象的哈希值的特点

HashSet集合 HashSet集合的特点


HashSet集合保证元素唯一性源码分析

哈希表


linkedHashSet集合

TreeSet集合

自然排序Comparable的使用

例:存储学生对象并遍历,创建TreeSet集合使用无参构造方法
要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母排序。

package IT07.TreeSet;

import java.util.TreeSet;

public class TreeSetDemo02 {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet ts = new TreeSet();
          //创建学生对象
        Student s1=new Student("xishi",29);
        Student s2=new Student("diaochan",28);
        Student s3=new Student("diaochan",30);
        Student s4=new Student("yangyuhuan",33);

        Student s5 = new Student("huitailang",33);
        Student s6 = new Student("zwyx",33);
        Student s7 = new Student("zwyx",33);
        //把学生对象添加到集合
        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());
        }

    }
}
package IT07.TreeSet;

public class Student implements Comparable{
    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 int compareTo(Student s) {
        //return 0;返回0相当于集合添加一个元素后进行比较后不会再添加新元素
        //return 1; 返回值为正数,每次都会存储,会按照存储顺序输出
        //return -1;  返回值是负数,每次都会存储,会按照存储的倒序输出
        int num = this.age-s.age;// this是传进来的元素,s是已经在集合里的元素
        //int num = s.age-this.age; 按年龄降序排列
        //年龄相同时按照字母顺序排序
        int num2=num==0?this.name.compareTo(s.name):num;
        return num2;
    }
}
比较器排序Comparator的使用

package IT07.TreeSet;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo03 {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet ts = new TreeSet(new Comparator() {
            @Override
            public int compare(Student01 s1, Student01 s2) {
                int num = s1.getAge() - s2.getAge();
                int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
                return num2;
            }
        });
        Student01 s1=new Student01("xishi",29);
        Student01 s2=new Student01("diaochan",28);
        Student01 s3=new Student01("diaochan",30);
        Student01 s4=new Student01("yangyuhuan",33);

        Student01 s5 = new Student01("huitailang",33);
        Student01 s6 = new Student01("zwyx",33);
        Student01 s7 = new Student01("zwyx",33);
        //把学生对象添加到集合
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);
        ts.add(s7);
        for(Student01 s : ts){
            System.out.println(s.getName()+","+s.getAge());
        }

    }
}
package IT07.TreeSet;

public class Student01 {
    private String name;
    private int age;

    public Student01() {
    }

    public Student01(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;
    }
}
泛型

泛型的好处

泛型类

泛型方法

泛型接口

类型通配符

可变参数

可变参数使用

Map

Map集合的基本功能

Map集合的获取功能

package IT09.Map;

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

public class MapDemo03 {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("张无忌","赵敏");
        map.put("郭靖","黄蓉");
        map.put("杨过","小龙女");
        //根据键获取值
//        System.out.println(map.get("张无忌"));
//        System.out.println(map.get("张三"));//没有键或者键对应值为空,返回null
        //获取所有键的集合的方法
        Set keySet = map.keySet();
        for(String k : keySet){
            System.out.println(k);
        }
        //获取所有值的集合的方法
        Collection values = map.values();
        for(String value:values){
            System.out.println(value);
        }
    }
}
Map集合的遍历(方式1、2)


Collections

练习(斗地主升级版)
package IT09.模拟斗地主升级版;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;

public class PokerDemo {
    public static void main(String[] args) {
        //创建HashMap集合,键是编号,值是牌
        HashMap hm = new HashMap();
        //创建ArrayList集合,存储编号
        ArrayList arr= new ArrayList();

        //创建花色数组
        String []colors= {"♥","♦","♣","♠"};
        //创建点数数组
        String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2"};

        //从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList中存储编号
        int id = 0;
        for(String number:nums){
            for(String color:colors){
                hm.put(id,color+number);
                arr.add(id);
                id++;
            }
        }
        hm.put(id,"小王");
        arr.add(id);
        id++;
        hm.put(id,"大王");
        arr.add(id);
        //洗牌
        Collections.shuffle(arr);
//        System.out.println(arr);
        TreeSet xiaoguo = new TreeSet();
        TreeSet daguo = new TreeSet();
        TreeSet wyx = new TreeSet();
        TreeSet dipai = new TreeSet();

        for (int i = 0; i < arr.size(); i++) {
            Integer poker = arr.get(i);
            if (i >= arr.size() - 3) {
                dipai.add(poker);
            } else if (i % 3 == 0) {
                daguo.add(poker);
            } else if (i % 3 == 1) {
                xiaoguo.add(poker);
            } else if (i % 3 == 2) {
                wyx.add(poker);
            }
        }
        lookpoker("xiaoguo",xiaoguo,hm);
        lookpoker("daguo",daguo,hm);
        lookpoker("wyx",wyx,hm);
        lookpoker("dipai",dipai,hm);
    }
    public static void lookpoker(String name,TreeSet ts,HashMaphm){
        System.out.print(name+"的牌是:");
        for(Integer key:ts){
            String poker = hm.get(key);
            System.out.print(poker+" ");
        }
        System.out.println();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/343638.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号