java高级开发 第十三章——泛型与集合框架

一、泛型 

1.泛型类   

                 

E是其中的泛型,代表一种数据类型,可以是类或接口,不能是基本数据类型。泛型可以作为类的成员变量的类型,方法的类型,局部变量的类型。

带有泛型的类称为泛型类。

使用泛型类声明对象

(1)用具体的类型代替E

(2)使用通配符?

Cone<? extends Geometry> cone=new Cone<Circle>(c);

//Geometry类或Geometry的子类或实现了Geometry接口的类

Cone<? super B> cone;   //B类或者B的任何父类

Cone<?> cone=new Cone<Circle>(c);

2.泛型接口   

    interface Computer<E,F>{

             void makeChorus(E x,F y);

    }

通过类实现这个接口时可以指定泛型的具体类型,也可以不指定。

3.练习
(1)泛型类、三棱锥

public class xx {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Circle c = new Circle(4);// 调用该类的构造方法
        Cone<Circle> cone = new Cone<Circle>(c, 10);
        System.out.println(cone);
        Triangle d=new Triangle(3.2,5.3,9.4);
        Cone<Triangle> de = new Cone<Triangle>(d,2);
        System.out.println(de);
     }

}

class Cone<E> {
    E bottom;
    double height;

    public Cone(E bottom, double height) {
        super();
        this.bottom = bottom;
        this.height = height; // 构造方法:生成一个全参构造方法
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "Cone [bottom=" + bottom + ", height=" + height + "]";
    }

}

class Circle {
    double r;

    public Circle(double r) {
        super();
        this.r = r;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }

    @Override
    public String toString() {
        return "Circle [r=" + r + "]";
    }

}
class Triangle{                //三棱锥
    double a,b,c;

    public Triangle(double a, double b, double c) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }

    public double getB() {
        return b;
    }

    public void setB(double b) {
        this.b = b;
    }

    public double getC() {
        return c;
    }

    public void setC(double c) {
        this.c = c;
    }

    @Override
    public String toString() {
        return "Triangle [a="+ a + ", b=" + b + ", c=" + c + ";]";
    }
}


(2)泛型接口

public class xx {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Printer p1=new Printer();//构造方法
        People people=new People("小明");
        p1.print(people);
            }

        }
            interface printable<E>{        //泛型接口
            public void print(E x);//抽象方法,无方法体--{}        
            }
        class Printer implements printable<People> {
            
            @Override
            public void print(People x) {
                // TODO Auto-generated method stub
                System.out.println(x.getName());
            }
            
        }
        class People{
            String name;

            public People(String name) {
                super();
                this.name = name;
            }

            public String getName() {
                return name;
            }

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

二、链表

  • 链表  

是由若干个结点组成的一种数据结构,每个结点含有一个数据和一个(两个)引用,指向上一结点或下一结点。

LinkedList<E>泛型类

该泛型类创建的对象以链表结构存储数据,常称为链表对象。

LinkedList<String> mylist=new LinkedList<String>();

public boolean add(E element) 

public void add(int index,E element) 

public void clear() 

public E remove(int index)  

public boolean remove(E element)   //删除首次出现的element

public E get(int index)  

public int indexOf(E element)  

public int lastIndexOf(E element)  

public E set(int index,E element)   //返回被替换的数据

public int size()  

public boolean contains(Object element)   

public void addFirst(E element)   

public void addLast(E element)   

public E getFirst()   

public E getLast()   

public E removeFirst()   

public E removeLast()   

public Object clone()   

链表的遍历

使用iterator()方法获得Iterator迭代器,用迭代器遍历。

Iterator<String> iter=mylist.iterator();

练习:链表、整数链表

import java.util.Iterator;
import java.util.LinkedList;
public class xx {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //链表数据是字符串
        LinkedList<String> list1=new LinkedList<String>();
        list1.add("小白");
        list1.add("小黑");
        list1.add("小粉");
        Iterator<String> iter=list1.iterator();//获得迭代器
        while(iter.hasNext()) {  //判断是否有下一个元素
            String s=iter.next();  //得到下一个原素
            System.out.println(s);
        }
        //链表数据是整数
        LinkedList<Integer> list2=new LinkedList<Integer>();
        list2.add(3);
        list2.add(4);
        list2.add(5);
        Iterator<Integer> itera=list2.iterator();//获得迭代器
        while(itera.hasNext()) {  //判断是否有下一个元素
            Integer s=itera.next();  //得到下一个原素
            System.out.println(s);
            }

        }
        }
         

三、堆栈、散列映射、树集

  • 堆栈 

是一种“后进先出”的数据结构。只能从一端输入输出数据。使用Stack<E>泛型类可以创建堆栈对象。

创建堆栈对象:

Stack<Integer> stack=new Stack<Integer>();

方法:

public E push(E item);    //入栈

public E pop();   //出栈

public boolean empty();   //判断堆栈是否为空

public E peek();  //获取堆栈顶端的数据,但不删除该数据。

public int search(Object data);  //获取数据在堆栈中的位置,最顶端的位置是1,向下依次增加,如果堆栈不含此数据,则返回-1。

  • 散列映射    

映射表(map)依照“键/值”对(key,value)来存储元素。映射表将键和值一起保存,一个键和它对应的值构成一个条目保存在映射表中。键不能重复,可以是任意类型的对象,每个键对应一个值。键又称搜索键,用来查找相应的值。

散列一般指哈希(hash),散列函数(哈希函数)是一种将键映射到表(map)中索引上的函数。散列函数从一个键获得索引,并使用索引来获得该键的值。散列就是一种无须执行搜索,即可通过键得到索引来获取值的技术。

HashMap<K,V> 泛型类创建的对象称为散列映射。

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

常用方法:

public V put(K key,V value);   //添加键值对

public boolean containsKey(Object key)  

public boolean containsValue(Object value) 

public V get(Object key)  

public int size() 

遍历:

Collection<Student> collection=hashmap.values();
        Iterator<Student> iter=collection.iterator();
        while(iter.hasNext()) {
            Student stu=iter.next();
            System.out.println(stu.getName()+stu.getNumber());
        }

  • 树集 

树集采用树结构存储数据,树结点中的数据会按存放数据的“大小"顺序依次排列,在同一层中的结点从左到右从小到大的顺序递增排列,下一层比上一层小。

TreeSet<E>泛型类创建的对象称为树集。

TreeSet<String> tree=new TreeSet<String>();

常用方法:

public boolean add(E o) 

public void clear()

public boolean contains(Object o)

public E first()

public E last()

注意:树集中的结点要能比较大小。

练习
1.栈

import java.util.Stack;

public class xx {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Stack<Integer> s1 = new Stack<Integer>();
        s1.push(1);
        s1.push(1);
//循环十次
        int n = 1;
        while (n <= 10) {

            int f2 = s1.pop();
            int f1 = s1.pop();
            int next = f1 + f2;
            s1.push(f2);
            s1.push(next);
//打印
            System.out.print(next + " ");
            n++;

        }
    }
}

2.散列映射

(1)Student类的定义

public class Student implements Comparable{
    //属性:学号,姓名
        int num;
        String name;
        int englishfen;
        //全参构造方法
        public Student(int num, String name) {
            super();
            this.num = num;
            this.name = name;
        }
        //
        public Student(int num, String name, int englishfen) {
            super();
            this.num = num;
            this.name = name;
            this.englishfen = englishfen;
        }
        public Student(String name, int englishfen) {
            super();
            this.name = name;
            this.englishfen = englishfen;
        }
        public Student(int num, int englishfen) {
            super();
            this.num = num;
            this.englishfen = englishfen;
        }
        //get.set
        public int getNum() {
            return num;
        }
        public void setNum(int num) {
            this.num = num;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    @Override
        public String toString() {
            return "Student [num=" + num + ", name=" + name + ", englishfen=" + englishfen + "]";
        }
        
        
    @Override
    public int compareTo(Object arg0) {
        // TODO Auto-generated method stub
        Student stu=(Student) arg0; //强制类型转换
        
        return this.englishfen-stu.englishfen;    //    用英语课程成绩排序
    }
    }

(2)main方法

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

public class xx {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashMap<String, Student> hashmap = new HashMap<String, Student>();
        hashmap.put("201", new Student(01,"小白"));
        //仿照,再加2条目
        hashmap.put("202", new Student (02,"小黑"));
        hashmap.put("203", new Student(03,"小粉"));

        Collection<Student> collection=hashmap.values();//获得所有values,放在集合collection中
        Iterator<Student> iter=collection.iterator();//通过集合获得迭代器
        while(iter.hasNext()) {    // 使用迭代器打印信息
            Student stu=iter.next();
            System.out.println(stu);
        }
    }

}

3.树集

(1)Student类的定义

public class Student implements Comparable{
    //属性:学号,姓名
        int num;
        String name;
        int englishfen;
        //全参构造方法
        public Student(int num, String name) {
            super();
            this.num = num;
            this.name = name;
        }
        //
        public Student(int num, String name, int englishfen) {
            super();
            this.num = num;
            this.name = name;
            this.englishfen = englishfen;
        }
        public Student(String name, int englishfen) {
            super();
            this.name = name;
            this.englishfen = englishfen;
        }
        public Student(int num, int englishfen) {
            super();
            this.num = num;
            this.englishfen = englishfen;
        }
        //get.set
        public int getNum() {
            return num;
        }
        public void setNum(int num) {
            this.num = num;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    @Override
        public String toString() {
            return "Student [num=" + num + ", name=" + name + ", englishfen=" + englishfen + "]";
        }
        
        
    @Override
    public int compareTo(Object arg0) {
        // TODO Auto-generated method stub
        Student stu=(Student) arg0; //强制类型转换
        
        return this.englishfen-stu.englishfen;    //    用英语课程成绩排序
    }
    }

(2)main方法

import java.util.Iterator;
import java.util.TreeSet;
public class xx {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TreeSet <String> t=new TreeSet<> ();
        t.add("aa");
        t.add("ee");
        t.add("cc");
        Iterator<String> i=t.iterator();
        
        while(i.hasNext()) {
            String te=i.next();
            System.out.println(te);
        }
        
        TreeSet<Integer> tr=new TreeSet<>();
        tr.add(11);
        tr.add(99);
        tr.add(25);
        Iterator<Integer> it=tr.iterator();
        while(it.hasNext()) {
            Integer in=it.next();
            System.out.println(in);
        }
        
        TreeSet<Student> st=new TreeSet<>();

        st.add(new Student(11,"小夜",65));
        st.add(new Student(60,"小多",60));
        st.add(new Student(30,"小云",95));
        Iterator<Student> ite=st.iterator();
        while(ite.hasNext()) {
            Student s=ite.next();
            System.out.println(s);
        }
        }
    }
 

四、树映射、集合

  • 树映射 

称TreeMap<K,V>对象为树映射。如:

TreeMap<Key,Student> treemap=new TreeMap<Key,Student>();

常用方法:public V put(K key,V value);

按key升序排列。

遍历:

Collection<Student> collection=treemap.values();
        Iterator<Student> iter=collection.iterator();
        while(iter.hasNext()) {
            Student stu=iter.next();
            System.out.println(stu.getName()+stu.getEnglish());
        }

  • 集合 

HashSet<E>泛型类创建的对象称为集合。如:

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

添加到集合中的数据称做集合的元素。集合不允许有相同的元素。

集合的遍历

集合的运算

boolean retainAll(HashSet set)当前集合成为两个集合的交集。

boolean addAll(HashSet set)当前集合成为两个集合的并集。

boolean removeAll(HashSet set)当前集合成为两个集合的差集。

  • 30
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值