Java泛型、集合

java泛型

01-泛型的引入

定义:是用泛型可以指代任意对象类型

引用泛型的作用:简化向上转型和向下转型

//泛型类要+<>,<>内的字母可以随意,但是一般都是用T(type)
public class test<T>
{
    private T ob;
    public T getob()
    {
        return ob;
    }
    public void setob(T ob)
    {
        this.ob=ob;
    }
    
    public static void main(String[] args)
    {
        //定义一个整形类型
        CC<Integer> cc=new CC<Integer>();
        CC<String> cc=new CC<String>();
    }
}
02-限制泛型

有限制的,必须是继承自某个类的子类,不允许其他类型传进去,类似于一种安全机制

//父类Animal
public class Animal
{
    public void print()
    {
        System.out.println("动物");
    }
}
//子类Cat
public class Cat extends Animal
{
    //改写父类的构造方法
    public void print()
    {
        System.out.println("猫");
    }
}
//限制泛型类,继承自Animal
public class Demo <T extends Animal>
{
    private T ob;
    //get set方法
    public void setob(T ob)
    {
        this.ob=ob;
    }
    public T getob()
    {
        return ob;
    }
    //构造方法
    public Demo(T ob)
    {
        super();
        this.ob=ob;
    }
}
//测试类
public class test()
{
    public static void main(String[] args)
    {
        //这里只能是Animal类以及Animal的子类
        Demo<Cat> demo=new Demo<Cat> (new Cat());
        Cat cat=demo.getob();
        cat.print();
        //结果:猫
        
        /*这样就会报错
        Demo<Integer> demo=new Demo<Integer> (2);
        */
    }
}
03-通配符泛型

特殊的场景下用到,具体看例子

import Cat;
import Demo;
import Animal;

public class Test
{
    //传入的参数不确定,可以用通配符泛型“?”
    private static void take(Demo<?> a)
    {
        a.print();
    }
    public static void main(String[] args)
    {
        Demo<Cat> demo=new Demo<Cat> (new Cat());
        take(cat);
    }
}
03-泛型方法
public class test
{
    //泛型方法
    public static <T> void function(T t)
    {
        System.out.println("T的类型是"+t.getClass().getName());
    }
    public static void main(String[] args)
    {
        function(1);
        function("str");
    }
}

java集合

01-集合的引入

用数组实现集合

public class Student
{
    private Sting name;
    public void setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return this.name;
    }
    public void Student(String name)
    {
        this.name=name;
    }
    
    public static void main(String[] args)
    {
        //可以用数组定义多个元素
        //定义三个长度的数组对象
        Student student[]=new Student[3];
        Student s1=new Student("张三");
        Student s2=new Student("李四");
        Student s3=new Student("王五");
        //缺点:长度固定死了,不方便
    }
}

链表来实现集合

//用链表来实现集合
 public static void main(String[] args)
    {
        Student s1=new Student("张三");
        Student s2=new Student("李四");
        Student s3=new Student("王五");
     	LinkedList<Student> list=new LinkedList<Student> ();
     	list.add(s1);
        list.add(s2);
        list.add(s3);
       
     //遍历链表
     for(int i=0;i<list.size();i++)
     {
         Student s=list.get(i);
         System.out.pritln(s.getName());
     }
    }
02-List集合

List集合是Collection接口的子接口,也是最常用的接口。此接口对Collection接口进行了大量的扩展,List集合里的元素是允许重复的

ArrayList实现类(实现了List接口)

public class test
{
    public static void main(String[] args)
    {
        //ArraryList有很多方法,具体看文档
        ArraryList<String> al=new ArrayList<String>();
        al.add("张三");
        al.add("李四");
         for(int i=0;i<ArrayList.size();i++)
     {
         Student s=list.get(i);
         System.out.pritln(s.getName());
     }
    }
}

linkedList实现类

public class test
{
    public static void main(String[] args)
    {
        
        linkedList<String> ll=new linkedList<String>();
        ll.add("张三");
        ll.add("李四");
        printLinkedList(linkList);
    }
}
03-集合的遍历

Iterator方法遍历集合

import Student;
    public class test
    {
        public static void main(String[] args)
        {
            LinkedList<Student> list=new LinkedList<Student>();
            //添加匿名Student类
            list.add(new Student("张三"));
            list.add(new Student("王五"));
            Iterator<Student> it=list.iterator();	//返回的是一个迭代器、
            while(it.hasNext())	//当迭代器类有元素
            {
                Student s=it.next();	//返回迭代的下一个元素
                System.out.println(s.getName());
            }
        }
    }

foreach方法遍历集合

import Student;
    public class test
    {
        public static void main(String[] args)
        {
            LinkedList<Student> list=new LinkedList<Student>();
            //添加匿名Student类
            list.add(new Student("张三"));
            list.add(new Student("王五"));
            //list:集合元素
            //Student s:每次循环的时候都指向这个变量
            for(Student s:list)
            {
                System.out.println(s.getName());
            }
        }
    }
04-set集合

set集合是Collection接口的子接口,没有对Collection接口进行扩展,里面不允许存在重复内容

HashSet类

public static void main(String[] args)
{
    //HashSet是无序的(现在是有序的了)
    //不循环重的值
    HashSet<String> hs=new HashSet<String>();
    hs.add("1");
    hs.add("2");
    hs.add("3");
    hs.add("1");
    hs.add("4");
    Iterator<String> it=hs.iterator();
    while(it.hasNext)
    {
        String next=it.next();
        System.out.println(next);
        //打印出来的结果是无序的!
        //3124...
    }
}
05-Map集合

是存放一对值的最大接口,即接口中的每一个元素都是一对,以key->value键值对的形式保存

HashMap类

import Student;
public class test
{
    HashMap<String,Student> hh=new HashMap<String,Student>();
    hh.put("1号",new Student("张三"));
    hh.put("2号",new Student("李四"));
    //通过key获取value
    Student s=hh.get("1号");
    //获取key集合,再获取迭代器
    Interator<String> it=hh.keySet().interator();
    while(it.hasNext())
    {
        String key=it.next();
        Student s=hh.get(key);
    }
    
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值