回忆集合笔记

集合和数组的区别

元素类型

                集合:引用类型(存储基本类型时会自动装箱)

                数组:基本类型、引用类型

元素个数

              集合:不固定,可以任意扩容

             数组:固定

集合优点

              不受容器大小限制,可以随时添加、删除元素、提供了大量操作元素方法

 

 

List集合

特点:可重复,有序(存取顺序相同)

应用:List list = new ArrayList();

List listDemo = new ArrayList();
		Student s1 = new Student("谢广坤",44);
		Student s2 = new Student("谢永强",20);
		Student s3 = new Student("谢广坤",44);
		Student s4 = new Student("谢大脚",43);
		listDemo.add(s1);
		listDemo.add(s2);
		listDemo.add(s3);
		System.out.println(listDemo.get(2));
		System.out.println(listDemo.size());
		for (int i = 0; i < listDemo.size(); i++) {
			System.out.println(listDemo.get(i));
		}
				

迭代器

对过程的重复叫迭代

迭代器是遍历collection集合的通用方式,可以在对集合遍历的同时进行添加、删除等操作。

迭代器的常用方法

next():返回迭代的一个元素对象

hasNext():如果有元素可以迭代返回true

注意:列表迭代器是List体系独有的遍历方法,可以在对集合遍历的同时进行添加和删除等操作。

但是必须通过 调用列表迭代器的方法来实现。

 

//创建集合对象
    	List list = new ArrayList();
    	//创建元素对象
    	//将元素添加到
    	list.add("a");
    	list.add("b");
    	list.add("c");
    	//遍历集合
    	Iterator iterator = list.iterator();
    	while(iterator.hasNext()) {
//        没有添加泛型,需要类型转换
    		String string = (String) iterator.next();
    		System.out.println(string);
    	}

	//列表迭代器
   
    	//根据集合对象获取列表迭代器对象
    	ListIterator listIterator = list.listIterator();
    	//判断迭代器中是否有元素
    	while(listIterator.hasNext()) {
    		String string = (String)listIterator.next();
    		if ("b".equals(string)) {
				listIterator.add("java");
			}
    	}
    	System.out.println(list);
    	

泛型(1.5开始)

泛指任意类型,幼教参数化类型,对具体类型的使用起到辅助作用,类似方法参数

好处:类型安全,避免类型转换

泛型一般只和集合类 相结合使用

	List<String> list = new ArrayList<String>();
    	list.add("a");
    	list.add("b");
    	list.add("c");
    	for (String string : list) {
    		System.out.println(string);		
		}

Collections工具类

max(Clloection<T>)

sort(List<T>);

reverse(List<T>);

shuffle(List<T>);   使用默认的随机源置换指定列表

//打印集合
		System.out.println(Collections.max(list));
//		Collections.sort(list);
//		System.out.println(list);
		Collections.sort(list);
		Collections.reverse(list);
		System.out.println(list);
		Collections.shuffle(list);
		System.out.println(list);

 

Set集合

特点:不可重复,无序

 //创建集合对象
		Set<Student>  set = new HashSet<Student>();
//		创建元素对象
		Student s1 = new Student("谢广坤",41);
		Student s2 = new Student("刘能",44);
		Student s3 = new Student("赵四",39);
		Student s4 = new Student("王老七",45);
		Student s5 = new Student("宋富贵",43);
		Student s6 = new Student("谢广坤",41);
//		将集合对象添加到元素对象中
		set.add(s1);
		set.add(s2);
		set.add(s3);
		set.add(s4);
		set.add(s5);
		set.add(s6);
//		遍历集合,必须在Student中重写equal()和hashCode()
		System.out.println(set);
//		通过迭代器遍历Set集合
		Iterator<Student> iterator = set.iterator();
//		判断迭代器中是否有元素
		while(iterator.hasNext()) {
//		如果有,就获取元素
			Student student = iterator.next();
			System.out.println(student);
		}
		System.out.println("=============");
//		通过增强for
		for (Student student : set) {
			System.out.println(student);
		}

Student:
@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + age;
	result = prime * result + ((name == null) ? 0 : name.hashCode());
	return result;
}
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Student other = (Student) obj;
	if (age != other.age)
		return false;
	if (name == null) {
		if (other.name != null)
			return false;
	} else if (!name.equals(other.name))
		return false;
	return true;
}
   
  

Map集合

特点:键不可以重复,值可以重复

成员方法

V  put(K key,V value);添加元素(键值对),元素的第一次添加,返回null,重复添加会用新值覆盖旧值,

并返回旧值 

V get(Object key);根据键值获取对应的值

Set<K> keySet(); 获取所有键的集合;

遍历步骤:

1、获取所有键的集合          keySet()

2、遍历所有的键,获取到每一个键  迭代器,增强for

3、根据键,获取指定的值             get();

//		创建集合对象
    	  Map<Integer, Student>  map = new HashMap<Integer, Student>();
//		创建元素的对象
    	  Student s1 = new Student("广坤",23);
       	  Student s2 = new Student("刘能",23);
       	  Student s3 = new Student("赵四",23);
       	  Student s4 = new Student("赵四",23);
       	  
//		将元素对象添加到集合中
       	  Student stu1 = map.put(1, s1);
       	  System.out.println(stu1);
       	  Student stu2 = map.put(1,s2);
       	  System.out.println(stu2);
       	  map.put(2,s2);
       	  map.put(3,s3);
       	  System.out.println(map.get(3));
//		              遍历集合
//       	  获取所有键的集合
       	     Set<Integer> keys = map.keySet();
       	     //获取迭代器对象
       	     Iterator<Integer> iterator = keys.iterator();
       	     while(iterator.hasNext()) {
//       	  如果迭代器中有数据,就获取
                Integer key = iterator.next();
//       	  根据键获取值
                Student value = map.get(key);
                System.out.println(key+"    "+value);
       	     }
       	     System.out.println("======================");
       	     //通过增强for
       	     for (Integer key : keys) {
				System.out.println(key+"    "+ map.get(key));
			}
       	     

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值