java 集合

collection

  • collection 集合的创建
    collection coll=new arraylist();

  • 接口的常用方法

  1. add() 添加元素
  2. addall() 添加另外一个collection集合
  3. isempty(); 判断集合是否为空
  4. size() 获取集合长度
  5. clear() 清除集合所有元素
  6. contains() 是否包含某个元素
  7. containsall() 判断一个集合是否包含另外一个集合元素
  8. removeall() 清楚集合内和另外一个集合的共同元素
  9. retainsall() 获取一个集合和另外一个集合交集
  10. equals() 只需判断另外一个集合元素值是否一样
  11. hascode() 求集合的哈希值
  12. toarray() 将集合转化为数组
  13. iterator() 创建集合迭代器
  • collection集合和数组之间的转换
    集合到数组 toarray()
    数组到集合 list list=arrays.aslist(new int[] {123,456});

  1. 会用
  2. 熟悉源码

遍历collection 的两种遍历

Collection collection=new ArrayList();
		collection.add(123);
		collection.add(124);
		collection.add(788);
		collection.forEach(System.out::println);
  1. 使用iterator迭代器
Collection collection=new ArrayList();
		collection.add(123);
		collection.add(124);
		collection.add(788);
		//collection.forEach(System.out::println);
		Iterator iterator=collection.iterator();
		while(iterator.hasNext()) {
			System.out.println(iterator.next());
		}

迭代器remove的使用

set

数据存储特点

|----collection 接口:单例集合,用来存储一个一个对象
|----list接口:存储有序的可重复的数据–》动态数组
|----arraylist:作为list的主要实现类,线程不安全效率高
|----linkedlist 对于频繁的插入删除操作,使用效率比arraylist高,使用双向列表存储
|----vector作为list接口的老实现类,线程是安全的,执行效率低

常用方法

  1. add
  2. remove
  3. set
  4. get
  5. add(index,object )
  6. size()
  7. 遍历 iterator

存储元素的要求

添加的对象所在的类要重写equals方法

面试题

arraylist lickedlist vector异同
同:三个类都实现了list接口 存储数据的特点相同 存储有序的可重复数据
不同:略

set 接口

数据存储特点

存储无序的 不可重复的 具体的数据-

  • 无序 不可重复

hashset添加过程

在这里插入图片描述

set常见方法

  • set中没有提供的额外方法 都是collection继承的

常用实现类

arraylist| linkedlist vector

treeset

  1. 自然排序中,比较两个对象是否相同的标准为compareto() 返回值0不再是equals
  2. 定制排序中 比较两个对象是否相同的标准为compareto() 返回值0不再是equals

使用说明1

  1. 自然排序
package treeset练习;

import java.util.Iterator;
import java.util.TreeSet;

public class start {
	public static void main(String[] args) {
		TreeSet treeSet=new TreeSet();
		Employee e1=new Employee("liudehua", 56,new Mydate(1987, 2, 21));
		Employee e2=new Employee("ehoujielun", 46,new Mydate(1947, 2, 21));
		Employee e3=new Employee("phangxueyou", 52,new Mydate(1937, 2, 21));
		Employee e4=new Employee("guofucheng", 54,new Mydate(1987, 2, 25));
		Employee e5=new Employee("niangchaowei", 50,new Mydate(1985, 2, 21));
		treeSet.add(e1);
		treeSet.add(e2);
		treeSet.add(e3);
		treeSet.add(e4);
		treeSet.add(e5);
		
		Iterator iterator =treeSet.iterator();
		while(iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}

}
// 存放日期
class Mydate {
	private int year;
	private int month;
	private int day;

	public Mydate(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}

//	public Mydate() {
//		super();
//	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

}

class Employee implements Comparable{
	@Override
	public int compareTo(Object o) {
//		// TODO Auto-generated method stub
//		return 0;
		if(o instanceof Employee) {
			Employee employee=(Employee)o;
			return this.name.compareTo(employee.name);
		}
		else {
			return 0;
		}
	}
	private String name;
	private int age;
	private Mydate birthday;
	public Employee(String name, int age, Mydate birthday) {
		super();
		this.name = name;
		this.age = age;
		this.birthday = birthday;
	}
//	public Employee() {
//		super();
//	}
	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 Mydate getBirthday() {
		return birthday;
	}
	public void setBirthday(Mydate birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "Employee [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
	}
	
}
  1. 定制排序
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值