JAVA集合框架(一)

JAVA集合框架(一)

一. 集合的概念

  • 概念:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能。

  • 和数组的区别

      (1)数组长度固定,集合长度不固定
      (2)数组可以存储基本类型和引用类型,集合只能存储引用类型
    
  • 位置:java.util.*;

1. Collection接口

Collection体系
Interface Collection
Interface Set
Interface List
Interface SortedSet
Class HashSet
Class Vector
Class LinkedList
Class ArrayList
Class TreeSet

Interface Collection:该体系结构的根接口,代表一组对象,称为“集合”.
Interface List 接口的特点:有序、有下标、元素可重复
Interface Set接口的特点:无序、无下标、元素不能重复

2. Collection父接口

  • 特点:代表一组任意类型的对象,无序、无下标、不能重复。
集合遍历:
例一:
import javafx.print.Collation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Application {
    public static void main(String[] args) {
        //创建集合
        Collection collection = new ArrayList();
//      添加
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("葡萄");
        System.out.println(collection);
//      遍历
        //1,使用增强for循环
        for (Object o : collection) {
            System.out.println(o);
        }
        System.out.println("---------------------------");
        //2. 使用迭代器(专门用来遍历集合的一种方式)
        //hasNext()有没有下一个元素  next()获取下一个元素   remove()移除这一元素
        Iterator iterator = collection.iterator();
        while (iterator.hasNext()){
            //不能使用collection方法的
            System.out.println(iterator.next());
            iterator.remove();//移除当前
        }
        System.out.println(collection);
    }
}
---------------------结果---------------------
[苹果, 西瓜, 葡萄]
苹果
西瓜
葡萄
---------------------------
苹果
西瓜
葡萄
[]
例二:
public class Student {
    private String name;
    private int age;

    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 String toString() {
        return "Student [name="+name+", age="+age+"]";
    }
}
import javafx.print.Collation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Application {
    public static void main(String[] args) {

        Collection collection = new ArrayList();
        //添加数据
        Student s1 = new Student("张三",20);
        Student s2 = new Student("李四",20);
        Student s3 = new Student("王五",20);
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println(collection.size());
        System.out.println(collection);
        //删除
        collection.remove(s1);
        System.out.println("删除后"+collection);
        //遍历
        //增强for
        for (Object o : collection) {
            //Student stu=(Student) o;
            System.out.println(o);
        }
        //迭代器
        Iterator iter = collection.iterator();
        while (iter.hasNext()){
            System.out.println(iter.next());
            //iter.remove();
        }
        System.out.println(collection);
        //判断
        System.out.println(collection.contains(s2));
        System.out.println(collection.contains(new Student("李四",20)));
    }
}
---------------------结果---------------------
3
[Student [name=张三, age=20], Student [name=李四, age=20], Student [name=王五, age=20]]
删除后[Student [name=李四, age=20], Student [name=王五, age=20]]
Student [name=李四, age=20]
Student [name=王五, age=20]
Student [name=李四, age=20]
Student [name=王五, age=20]
[Student [name=李四, age=20], Student [name=王五, age=20]]
true
false
add()方法

boolean add(Object obj) //添加一个对象。

import javafx.print.Collation;
import java.util.ArrayList;
import java.util.Collection;
public class Application {
    public static void main(String[] args) {
// 		创建集合
        Collection collection = new ArrayList();
//      添加
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("葡萄");
        System.out.println("元素个数:"+collection.size());
        System.out.println(collection);
    }
}
---------------------结果---------------------
元素个数:3
[苹果, 西瓜, 葡萄]
addAll ()方法

boolean addAll (Collection c) //将一个集合中的所有对象添加到此集合中。

import javafx.print.Collation;
import java.util.ArrayList;
import java.util.Collection;
public class Application {
    public static void main(String[] args) {
// 		创建集合
        Collection collection = new ArrayList();
//      添加
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("葡萄");
        System.out.println(collection);
//      addAll
        collection1.addAll(collection);
        System.out.println(collection1);
    }
}
---------------------结果---------------------
[苹果, 西瓜, 葡萄]
[苹果, 西瓜, 葡萄]
remove()方法

boolean remove (Object o) //在此集合中移除o对象

import javafx.print.Collation;
import java.util.ArrayList;
import java.util.Collection;
public class Application {
    public static void main(String[] args) {
// 		创建集合
        Collection collection = new ArrayList();
//      添加
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("葡萄");
        System.out.println(collection);
// 		移除
        collection.remove("西瓜");
        System.out.println(collection);
    }
}
---------------------结果---------------------
[苹果, 西瓜, 葡萄]
[苹果, 葡萄]
clear()方法

void clear() //清空此集合中的所有对象。

import javafx.print.Collation;
import java.util.ArrayList;
import java.util.Collection;
public class Application {
    public static void main(String[] args) {
// 		创建集合
        Collection collection = new ArrayList();
//      添加
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("葡萄");
        System.out.println(collection);
//		清空
        collection.clear();
        System.out.println(collection);
    }
}
---------------------结果---------------------
[苹果, 西瓜, 葡萄]
[]
contains()方法

boolean contains (Object o) //检查此集合中是否包含o对象

import javafx.print.Collation;
import java.util.ArrayList;
import java.util.Collection;
public class Application {
    public static void main(String[] args) {
// 		创建集合
        Collection collection = new ArrayList();
//      添加
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("葡萄");
        System.out.println(collection);
//		检查
        System.out.println(collection.contains("苹果"));
        System.out.println(collection.contains("芒果"));
    }
}
---------------------结果---------------------
[苹果, 西瓜, 葡萄]
true
false
equals()方法

boolean equals (0bject o) //比较此集合是否与指定对象相等。

import javafx.print.Collation;
import java.util.ArrayList;
import java.util.Collection;
public class Application {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        Collection collection1 = new ArrayList();
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("葡萄");
        collection1.addAll(collection);
        System.out.println(collection1.equals(collection));
    }
}
---------------------结果---------------------
true
isEmpty()方法

boolean isEmpty() //判断此集合是否为空

import javafx.print.Collation;
import java.util.ArrayList;
import java.util.Collection;
public class Application {
    public static void main(String[] args) {
// 		创建集合
		Collection collection = new ArrayList();
        System.out.println(collection.isEmpty());
        collection.add("苹果");
        System.out.println(collection.isEmpty());
    }
}
---------------------结果---------------------
true
false
size()方法

int size() //返回此集合中的元素个数。

public class Application {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("葡萄");
        System.out.println("元素个数:"+collection.size());
    }
}
---------------------结果---------------------
元素个数:3
toArray()方法

Object [ ] toArray() //将此集合转换成数组。

public class Application {
    public static void main(String[] args) {
 		Collection<String> coll = new ArrayList<String>();
        String[] theStrings = new String[ coll.size() ];
        coll.toArray(theStrings);
    }
}

另几篇链接:
JAVA集合框架(二)链接
JAVA集合框架(三)链接
JAVA集合框架(四)链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值