集合

来源:互联网 发布:qq飞车冲锋战神数据 编辑:程序博客网 时间:2024/05/17 08:19

  • 集合
    • Collection接口以及其中的常用方法
    • List集合不唯一有序
    • ArrayList
      • 对集合的各种操作用到Collections类
    • Set集合唯一无序
      • HashSet的程序
      • 技巧
  • Map用mapputkv 和 get v api-HashMap

集合

Collection接口以及其中的常用方法

这里写图片描述
Collection是Set和List的父类
1.set是HashSet和TreeSet的父类
2.List是ArrayList和LinkedList的父类
Map是HashMap和TreeMap的父类

方法 功能描述 add(E e) 将指定的对象添加到该集合中 remove(Object o) 将指定的对象从该集合中移除 isEmpty() 返回booolean值,用与判断当前集合是否为空 iterator() 返回在此Collection的元素上进行迭代,遍历集合 size() 获取集合中元素的个数 hasNext() 如果仍有元素可以迭代,则返回 true

示例:

//创建一个集合,注意导入包,Collection<String> list=new ArrayList<>();        list.add("a");        list.add("b");        //创建迭代器,然后遍历集合        Iterator<String> it=list.iterator();        while(it.hasNext()){        //强制类型转换,恢复原来的特性            String str=(String)it.next();//next()方法返回的是Object            System.out.println(str);        }

List集合(不唯一,有序)

List集合包括List接口以及List接口的所有实现类,List中允许重复,各元素的顺序就是对象插入的顺序。

ArrayList内存连续的遍历块,实现了可变的数组,允许保存所有元素,包括Null,并可以根据索引的位置对集合进行快速的随机访问;缺点是向指定的位置插入对象或删除对象的速度较慢。

LinkedList类采用链表结构保存对象,这种结构的优点是便于向集合中插入和删除对象时效率较高。

//set(int index,Object obj):将集合中指定索引位置的对象修改为指定对象。//get(int index):得到指定索引位置的对象//创建实例化的List集合List<E> list=new ArrayList<>();List<E> list2=new LinkedList<>();List<String> list=new ArrayList<String>();        list.add("a");        list.add("b");        list.add("d");        public static void main(String[] args) {    ArrayList<String> list=new ArrayList<String>();    list.add("a");    list.add("b");    list.add("d");    int i=(int)(Math.random()*(list.size()-1));//random()返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。    int j=list.size();//size比数组数大一,就是实际的长度。    System.out.println(i);    System.out.println(j);    System.out.println(Math.random());    list.set(1,"q");    Random rd=new Random();    System.out.println(rd.nextInt(10));    list.remove(2);    for(int m=0;m<list.size();m++){        System.out.println(list.get(m));}}

ArrayList

对集合的各种操作,用到Collections类

"*********对集合的各种操作,用到Collections类**********"//学生类public class Student {public String name;public int 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;}public Student(String name,int age){    this.age=age;    this.name=name;}}//集合类ArrayList<Student> list=new ArrayList<>();        list.add(new Student("李四",11));        list.add(new Student("王刚",12));        list.add(new Student("小强",9));        list.add(new Student("小红",10));        list.add(new Student("小明",32));        list.add(new Student("地方",15));        list.add(new Student("李的",26));        //必须创建一个类,在类中设置比较的方法,查看api文档        Collections.sort( list,new StuComparator());//集合的排序,new一个新对象        Collections.reverse(list);//集合的反转        for(Student stu:list){//for each语句            System.out.println(stu.getName()+"\t"+stu.getAge());        }        //实现接口类,另一个类public class StuComparator implements Comparator<Student> {    @Override    public int compare(Student stu1, Student stu2) {        //从小到大排序        return stu1.getAge()-stu2.getAge();        //从大到小排序        //return stu2.getAge()-stu1.getAge();    }}       

Set集合(唯一,无序)

Set集合不是按特定的顺序排序,只是简单的加入,但Set中不能包含重复对象
1.HashSet类实现Set接口,由哈希表支持,它不保证Set的迭代顺序,特别是它不保证该顺序恒久不变,此类允许使用null元素
2.HashSet使用迭代器Iterator
2.TreeSet类实现的Set集合在遍历集合时按照自然顺序递增,也可以按照指定的比较器递增排序,即可以通过比较器对用TreeSet类实现的Set集合中的对象进行排序。

HashSet的程序

public class HashSet1 {    public static void main(String[] args) {        HashSet<Integer> set = new HashSet<>();        Random random = new Random();        int count = 0;        while (set.size() < 10) {            // 产生0~到100的随机数            int i = random.nextInt(90) + 10;            System.out.println("第" + (++count) + "次" + "得到的数" + i);            set.add(i);        }        System.out.println(set.size());        Iterator<Integer> it = set.iterator();        while (it.hasNext()) {            int i = it.next();            System.out.println(i);//迭代器打印出来的数是无序的        }    }}

技巧

headSet()、subSet()、tailSet()方法截取对象生成新集合时是否包含指定的参数,可通过如下方法判断:

如果指定参数位于新集合的起始位置,则包含该对象,如subSet()方法的第一个参数和tailSet()方法的参数;如果指定的参数是新集合的终止位置,则不包含参数,如headSet()方法的入口参数和subSet()方法的第二个入口参数。

Map(用map.put(k,v) 和 get( v) api-HashMap

可以通过HashMap类穿件Map集合,当需要顺序输出时,再创建一个完全相同映射关系的TreeMap类实例
HashMap允许null值null键
HashTable不允许null键

java
//TreeMap(Map<? extends K,? extends V> m)
构造一个与给定映射具有相同映射关系的新的树映射,该映射根据其键的自然顺序 进行排序。

1.Map接口实现类有HashMap和TreeMap,建议使用HashMap类实现Map集合,因为这样集合的添加和删除映射效率更高;如果希望Map集合中的对象也存在一定的顺序,应该用TreeMap来实现。

    public static void main(String[] args) {        HashMap<String, Student> map=new HashMap<>();        map.put(null,new Student("张三",17));        map.put("wangwu", new Student("王五",19));        map.put("lisi", new Student("李四",21));//用put进行赋值        System.out.println(map.size());        Set<String> set=map.keySet();//先生成set表        Iterator<String> it=set.iterator();//用iterator遍历set表        while(it.hasNext()){//hasNext()返回boolean值            String key=it.next();            System.out.println(key);//得到String的值            Student stu=map.get(key);//通过key得到value            System.out.println(stu.getName()+stu.getAge());//调用value的方法        }    }
0 0
原创粉丝点击