集合
集合是对象的容器,定义了对多个对象常用的操作,实现了数组的功能
和数组的区别:
1.数组长度固定,集合长度不固定
2.数组可以存储基本类型和引用类型,集合只能存储引用类型
collection接口
特点:代表一组任意类型的对象
package com.chapter.chapter6;
//学生类
public class Student {
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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* collection接口的使用
* 添加元素
* 删除元素
* 遍历元素
* 判断
*
*/
public class Demo01 {
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);
/** //删除元素
collection.remove("榴莲");
collection.clear();
System.out.println("删除之后"+collection.size());
*/
// 遍历元素
//使用增强for
for (Object object:collection){
System.out.println(object);
}
System.out.println("================================");
//使用迭代器
//hasnext() next() remove()
Iterator it=collection.iterator();
while (it.hasNext()){
String s1=(String)it.next();
System.out.println(s1);
// collection.remove();这方法不能使用
it.remove();
}
System.out.println("元素个数"+collection.size());
// 判断
System.out.println(collection.contains("西瓜"));
System.out.println(collection.isEmpty());
}
}
另一个例子:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo02{
public static void main(String[] args) {
//新建collection对象
Collection collection = new ArrayList();
Student s1 = new Student("张三", 20);
Student s2 = new Student("李四", 21);
Student s3 = new Student("王五", 22);
//添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数" + collection.size());
System.out.println(collection.toString());
//删除
/** collection.remove(s1);
collection.clear();//清除
System.out.println("删除之后"+collection.size());
*/
//遍历
//增强for
for (Object object:collection){
Student s=(Student) object;
System.out.println(s.toString());
}
//迭代器
//迭代过程中不能使用collection.删除的方法
Iterator it=collection.iterator();
while (it.hasNext()){
Student s=(Student)it.next();
System.out.println(s.toString());
}
//判断
System.out.println(collection.contains("张三"));
System.out.println(collection.isEmpty());
}
}
list接口
特点;有序,有下标,元素可以重复
package com.chapter.chapter6;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Demo03 {
public static void main(String[] args) {
//创建集合对象
List list=new ArrayList<>();
//添加元素
list.add("a");
list.add("b");
list.add("c");
list.add("d");
System.out.println("元素个数"+list.size());
System.out.println(list.toString());
//删除元素
//list.remove("a");
list.remove(0);
System.out.println("删除之后"+list.size());
System.out.println(list.toString());
//遍历
//使用for来进行遍历
System.out.println("==========================");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//使用增强for
for (Object object:list){
System.out.println(list);
}
//使用迭代器
Iterator it=list.iterator();
while (it.hasNext()){
System.out.println( it.next());
}
//列表迭代器
ListIterator lit = list.listIterator();
while (lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}
//判断
System.out.println(list.contains("a"));
System.out.println(list.isEmpty());
//获取位置
System.out.println(list.indexOf("b"));
}
}
import java.util.ArrayList;
import java.util.List;
public class Demo04 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.add(60);
System.out.println("元素个数"+list.size());
System.out.println(list.toString());
//删除
//方法一:list.remove(0);
//方法二
list.remove((Object)20);
System.out.println("删除元素"+list.size());
System.out.println(list.toString());
//补充方法
SubList sublist=list.subList(1,3);
System.out.println(sublist.toString());
}
}
list实现类
Arraylist重点:数组结构实现,查询快,增删慢,线程不安全
LinkedList:链表结构实现,查询快,增删慢
import java.util.ArrayList;
import java.util.Iterator;
public class Demo05 {
public static void main(String[] args) {
//创建集合
ArrayList arrayList = new ArrayList<>();
//添加元素
Student s1 = new Student("chengle",22);
Student s2 = new Student("lege",23);
Student s3 = new Student("李四",200);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println("元素个数"+arrayList.size());
System.out.println(arrayList.toString());
//删除元素
arrayList.remove(s1);
System.out.println("删除之后"+arrayList.size());
System.out.println(arrayList.toString());
//遍历元素
//使用迭代器
Iterator it = arrayList.iterator();
while (it.hasNext()){
Student s=(Student)it.next();
System.out.println(s.toString());
//列表迭代器
Iterator lit= arrayList.iterator();
while (lit.hasNext()){
Student s5=(Student)lit.next();
System.out.println(s5.toString());
}
}
//判断
System.out.println(arrayList.contains(new Student("chengle",22)));
System.out.println(arrayList.isEmpty());
//查找
System.out.println(arrayList.indexOf(s1));
}
}
vector集合的使用(用得很少,了解即可)
import java.sql.SQLOutput;
import java.util.Enumeration;
import java.util.Vector;
public class Demo01 {
public static void main(String[] args) {
//创建集合
Vector vector = new Vector<>();
//添加元素
vector.add("草莓");
vector.add("芒果");
vector.add("西瓜");
System.out.println("元素个数"+vector.size());
//删除
vector.remove(0);
vector.remove("西瓜");
vector.clear();
//遍历(之前的方法就可以用)
//另外的方法:使用枚举器
Enumeration en=vector.elements();
while (en.hasMoreElements()){
String o=(String)en.nextElement();
System.out.println(o);
}
//判断
System.out.println(vector.contains("西瓜"));
System.out.println(vector.isEmpty());
}
}
LinkedList
import com.chapter.chapter6.Student;
import java.util.Iterator;
import java.util.LinkedList;
public class Demo02 {
public static void main(String[] args) {
//创建集合
LinkedList linkedList = new LinkedList<>();
Student s1 = new Student("chengle",22);
Student s2 = new Student("lege",23);
Student s3 = new Student("李四",200);
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
System.out.println("元素个数"+linkedList.size());
System.out.println(linkedList.toString());
//遍历
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
//增强for
for (Object object:linkedList){
Student s=(Student)object;
System.out.println(s.toString());
//使用迭代器
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()){
Student ss=(Student) iterator.next();
System.out.println(ss.toString());
}
}
}
}
泛型本质是参数化类型,,把类型作为参数传递
常用形式:泛型类,泛型接口,泛型方法
好处:提高代码的重用性,防止类型转换异常
泛型类案例:
public class MyGeneric<T> {
//使用泛型T
// 创建变量
T t;
//作为方法的参数
public void show (T t){
System.out.println(t);
}
//泛型作为方法的返回值
public T getT(){
return t;
}
}
public class TestGeneric {
public static void main(String[] args) {
//使用泛型类创建对象
//泛型只能使用引用类型,不同泛型对象之间不能相互复制
MyGeneric<String> myGeneric = new MyGeneric<String>();
myGeneric.t="hello";
myGeneric.show("大家好");
String string=myGeneric.getT();
MyGeneric<Integer> myGeneric2 = new MyGeneric<Integer>();
myGeneric2.t=100;
myGeneric2.show(233);
Integer integer=myGeneric2.getT();
}
}
另一个例子
public interface MyInterface<T> {
String name="张三";
T server(T t);
}
public class MyInterfaceImpl implements MyInterface<String>{
@Override
public String server(String t) {
System.out.println(t);
return null;
}
}
在main方法下写下面的代码
MyInterfaceImpl impl = new MyInterfaceImpl();
impl.server("xxxxxxxxxxxxxxx");
泛型方法
public class MyGenericMethod {
public <T> T show(T t){
System.out.println("泛型方法"+t);
return t;
}
}
在main方法下调用
MyGenericMethod myGenericMethod = new MyGenericMethod();
myGenericMethod.show("中国加油");
myGenericMethod.show(12131);
!!!!!遍历快捷键foreach
set集合
全部继承collection方法
set接口简单使用
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
//测试set接口的使用
public class Demo01 {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
//添加数据
set.add("苹果");
set.add("华为");
set.add("小米");
System.out.println("数据个数"+set.size());
System.out.println(set.toString());
//遍历
for (String string:set
) {
System.out.println(string);
}
//使用迭代器
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
String s=iterator.next();
System.out.println(s);
}
}
}
HashSet集合的使用
哈希表相当于数组+链表+红黑树、
基于hashcode计算元素存放位置
遍历:增强for或者用迭代器
import java.util.HashSet;
import java.util.Iterator;
public class Demo02 {
public static void main(String[] args) {
//新建集合
HashSet<String> hashSet = new HashSet<>();
//添加元素
hashSet.add("程乐1");
hashSet.add("程乐2");
hashSet.add("程乐3");
hashSet.add("程乐4");
System.out.println("元素个数"+hashSet.size());
System.out.println(hashSet.toString());
//遍历:增强for或者用迭代器
//增强for
for (String string:hashSet
) {
System.out.println(string);
}
//使用迭代器
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()){
String s=iterator.next();
System.out.println(s.toString());
}
}
}
import java.util.HashSet;
import java.util.Iterator;
```java
public class Dem03 {
public static void main(String[] args) {
HashSet<Person> persons = new HashSet<>();
Person p1 = new Person("程乐1",22);
Person p2 = new Person("程乐2",23);
Person p3 = new Person("程乐3",24);
Person p4 = new Person("程乐4",25);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(p4);
System.out.println("元素个数"+persons.size());
System.out.println(persons.toString());
Iterator<Person> ite = persons.iterator();
while (ite.hasNext()){
Person s=ite.next();
System.out.println(s.toString());
}
}
}
TreeSet
基于排列顺序实现元素不重复
实现了SortedSet接口,对集合元素自动排序
元素对象类型必须实现Comparable接口,指定排序规则
通过CompareTo方法是否为重复元素
//先按姓名比,再按年龄比
public class Person implements Comparable<Person>{
@Override
public int compareTo(Person o) {
int i1=this.getName().compareTo(o.getName());
int i2=this.getAge()-o.getAge();
return i1==0?i2:i1;
import java.util.Iterator;
import java.util.TreeSet;
//TreeSet的使用,存储结构:红黑树
//必须实现compareto接口,如果compareTo()方法返回值为0,认为是重复元素
public class Demo04 {
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("xyz");
treeSet.add("bbb");
treeSet.add("hello");
System.out.println("元素个数:"+treeSet.size());
System.out.println(treeSet.toString());
//遍历
//增强for
for (String string:treeSet
) {
System.out.println(string);
}
//使用迭代器
Iterator<String> ite = treeSet.iterator();
while (ite.hasNext()){
System.out.println(ite.next());
}
}
}
import java.util.TreeSet;
public class Demo05 {
public static void main(String[] args) {
TreeSet<Person> persons = new TreeSet<>();
//添加元素
Person p1 = new Person("程乐1",22);
Person p2 = new Person("程乐2",23);
Person p3 = new Person("程乐3",24);
Person p4 = new Person("程乐4",25);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(p4);
System.out.println("元素个数"+persons.size());
System.out.println(persons.toString());
}
}
Comparator接口
实现定制比较
Map集合
Map接口(不能实例化接口,就new出接口的实现类)
用于存储任意键值对(一对数据)
map.put()添加数据
HashMap使用
允许用null作为key或者value
Key.Set进行遍历
entrySet进行遍历
Hashtable和Properties
Hashtable很少用了,但是它的子类Properties(属性集合)在流中用的很多
TreeMap的使用
必须实现comparable接口或者定制比较
treemap.put()
Collection工具类
import java.util.Collection
Collection.sort()排序
reverse()反转
shuffle()打乱
集合是一个受限集合,不能添加或者删除元素
总结