折纸的不归路(15)

一.集合

在Java中,如果要处理复杂的数据结构,就需要写很多复杂的算法
在Java中,java.util包下面提供了集合的概念

Collection

用来保存单值规范

List

提供一个数据可以重复的有序的()集合
ArrayList
LinkedList
Vector:线程安全的可拓展数组

Set

提供一个无序且数据不可重复的集合
HashSet:使用的hash算法进行存储

hashset的相关测试

package day13;

import java.util.*;

import com.briup.day06.Student;

/**
 * 测试set集合
 * 
 * @author Acer
 *
 */

public class SetTest {
	public static void main(String[] args) {
		Set set = new HashSet();
		// 增
//		 set.add(582);
//		 set.add(20.0);
//		 set.add(582);
//		 set.add(20.00000000000);
//		 System.out.println("====" + set.size());

		// Boolean i1 = false;
		// Boolean i2 = false;
		// Boolean i3 = new Boolean(false);
		// Boolean i4 = new Boolean(false);
		// System.out.println(i1.hashCode());
		// System.out.println(i2.hashCode());
		// System.out.println(i3.hashCode());
		// System.out.println(i4.hashCode());
		// 打印得出全部是1237

		// Integer i1 = 598;
		// Integer i2 = 598;
		// Integer i3 = new Integer(598);
		// Integer i4 = new Integer(598);
		// System.out.println(i1.hashCode());
		// System.out.println(i2.hashCode());
		// System.out.println(i3.hashCode());
		// System.out.println(i4.hashCode());
		// 打印得出全部是598

		Student stu1 = new Student("tom", 20, "男");
//		Student stu2 = new Student("tom", 20, "男");
//		Student stu3 = new Student("tom", 20, "男");
//		Student stu4 = new Student("tom", 20, "男");
//		Student stu5 = new Student("tom", 20, "男");
//		Student stu6 = new Student("tom", 20, "男");
		Student stu2 = new Student("harry", 22, "女");
		Student stu3 = new Student("larry", 23, "男");
		Student stu4 = new Student("terry", 24, "女");
		Student stu5 = new Student("tom", 19, "女");
		Student stu6 = new Student("tom", 22, "男");

		System.out.println(stu1 == stu2);
		System.out.println(stu1.equals(stu2));
		System.out.println(stu1.hashCode());
		System.out.println(stu2.hashCode());
		System.out.println("===========");

		 set.add(stu1);
		 set.add(stu2);
		 set.add(stu3);
		 set.add(stu4);
		 set.add(stu5);
		 set.add(stu6);
		 System.out.println("===="+set.size());

		// 删
		// 假如需要修改set集合中的某个元素,需要比对
		// 遍历
//		Student temp = new Student("tom", 20, "男");
//		int i = temp.hashCode();
//		for (Object obj : set) {
//			if (obj.hashCode() == i) {
//				if (obj instanceof Student) {
//					((Student) obj).setName("jerry");
//				}
//			}
//			System.out.println(obj);
//		}

		// 迭代器遍历
		 Iterator it = set.iterator();
		 while (it.hasNext()) {
		 System.out.println(it.next());
		 }
	}

SortedSet

提供了比较机制下的集合
TreeSet:在Set的基础上提供了比较规则

Map

用来以键值对的形式保存数据
HashMap:无序且key值不重复

SortedMap

TreeMap:在Map的基础上可以提供排序规则,key值不重复

二.Collection集合的遍历方式

List

索引

1、普通for循环遍历

List集合拥有获得长度的方法size();,同时也拥有根据索引获得元素值的方法get();

for(int i=0;i<size();i++){
	list.get(i);
}

2、使用迭代器遍历

Collection接口的父接口为Iterable,为每一个Colletion集合提供了迭代器。需要先获得迭代器对象,然后判断hasNext()方法有没有下一个元素,如果有的话就通过next()方法输出下一个元素。

Iterator it = list.iterator;
	while(it.hasNext()){
	it.next();
}

3、增强for循环。for-each循环

语法:
for(数据类型 临时变量 : 集合对象){
输出:临时变量
}

Set

因为Set没有索引,所以只提供两种遍历方式

HashSet是如何保证元素的唯一性的?

HashCode:通过Object的hashCode()方法可以获得一个对象的hashCode值

HashSet底层的Hsah算法是如何工作的?

对比两个对象的hashCode值来确定set集合中元素的唯一性
可以通过重写hashCode()方法,来制定判断规则

步骤:
1、先判断两个对象的hashCode值是否相同,如果不同,则认定为两个不同的元素
2、先判断两个对象的hashCode值是否相同,如果相同,在判断equals方法的返回值,如果为true,则是两个相同的元素,如果为false,则是两个不同的元素
如果要使用HashSet来验证唯一性:
需要重写hashCode()和equals方法();

TreeSet

保证了元素的唯一性,并且内部的对象必须具有比较的规则

1、自然排序

一个类实现了Comparable接口,并且实现了compareTo()方法。则此时该类具有自然排序

在之前的Student类中通过重写compareTo方法来对新添加进集合中的student对象进行排序

	@Override
	public int compareTo(Object o) {
		if(this == o) return 0;
		if(o instanceof Student) {
			Student stu = (Student)o;
			//如果this和stu性别相同
			if(this.name.equals(stu.name)) {
				return this.age-stu.age;
			}
			return this.name.compareTo(stu.name);
		}
		else {
			return 0;
		}
	}
应用实例
		1,2,3,4,5,6,7,8,9,10
	要求:
		奇数在前,偶数在后,奇数升序,偶数降序,输出:
		1,3,5,7,9,10,8,6,4,2
package day13;

import java.util.*;

public class TreeSet{
	public static void main(String[] args) {
		Set<Integer> set = new TreeSet<Integer>(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				if (o1 % 2 != 0 && o2 % 2 != 0) {
					return o1 - o2;
				} else if (o1 % 2 == 0 && o2 % 2 == 0) {
					return o2 - o1;
				} else if (o1 % 2 != 0 && o2 % 2 == 0) {
					return -10;
				} else if (o1 % 2 == 0 && o2 % 2 != 0) {
					return 10;
				} else {
					return 0;
				}
				// 前-后:升序
				// 后-前:降序

			}
		});

		for (int i = 1; i < 11; i++) {
			set.add(i);
		}
		System.out.println(set);
	}

}

2、客户化排序(自定义排序、覆盖排序。。)

java.util.Comparator

使用外部类、成员内部类、匿名内部类实现Comparator接口,将其比较器的对象传入TreeSet中作为参数,为没有实现自然排序的对象提供排序方法。

如果一个对象既有自然排序,又有客户化排序,则优先进行客户化排序,原先的自然排序会被覆盖掉。客户化排序又叫覆盖排序。

用外部类来实现客户化排序
package day13;

import java.util.*;


import com.briup.day06.Student;

/**
 * 测试set集合
 * 
 * @author Acer
 *
 */

public class SetTest {
	public static void main(String[] args) {
		 // 实例化一个比较器
		 MyComparator mc1 = new MyComparator();

		Set set = new TreeSet(mc1);
		


		Student stu1 = new Student("tom", 20, "男");

		Student stu2 = new Student("harry", 22, "女");
		Student stu3 = new Student("larry", 23, "男");
		Student stu4 = new Student("terry", 24, "女");
		Student stu5 = new Student("tom", 19, "女");
		Student stu6 = new Student("tom", 22, "男");

		System.out.println(stu1 == stu2);
		System.out.println(stu1.equals(stu2));
		System.out.println(stu1.hashCode());
		System.out.println(stu2.hashCode());
		System.out.println("===========");

		 set.add(stu1);
		 set.add(stu2);
		 set.add(stu3);
		 set.add(stu4);
		 set.add(stu5);
		 set.add(stu6);
		 System.out.println("===="+set.size());


// 使用外部类来实现比较器
class MyComparator implements Comparator {

	@Override
	public int compare(Object o1, Object o2) {
		// 比较姓名和年龄
		String name1 = "";
		String name2 = "";
		int age1 = 0;
		int age2 = 0;
		if (o1 instanceof Student) {
			Student s1 = (Student) o1;
			name1 = s1.getName();
			age1 = s1.getAge();
		}
		if (o1 instanceof Student) {
			Student s2 = (Student) o2;
			name2 = s2.getName();
			age2 = s2.getAge();
		}
		// 制定比较规则
		if (name1.equals(name2)) {
			return age1 - age2;
		} else {
			return name1.compareTo(name2);
		}

	}

}

用内部类来实现客户化排序
package day13;

import java.util.*;


import com.briup.day06.Student;

/**
 * 测试set集合
 * 
 * @author Acer
 *
 */

public class SetTest {
	public static void main(String[] args) {
		 // 实例化一个比较器
		 MyComparator mc1 = new MyComparator();
		 // 实例化一个内部的比较器
		 SetTest.MC mc2 = new SetTest().new MC();
		  Set set = new TreeSet(mc2);

		Student stu1 = new Student("tom", 20, "男");

		Student stu2 = new Student("harry", 22, "女");
		Student stu3 = new Student("larry", 23, "男");
		Student stu4 = new Student("terry", 24, "女");
		Student stu5 = new Student("tom", 19, "女");
		Student stu6 = new Student("tom", 22, "男");

		System.out.println(stu1 == stu2);
		System.out.println(stu1.equals(stu2));
		System.out.println(stu1.hashCode());
		System.out.println(stu2.hashCode());
		System.out.println("===========");

		 set.add(stu1);
		 set.add(stu2);
		 set.add(stu3);
		 set.add(stu4);
		 set.add(stu5);
		 set.add(stu6);
		 System.out.println("===="+set.size());

	// 使用一下内部类
	private class MC implements Comparator<Student> {

		@Override
		public int compare(Student o1, Student o2) {
			if (o1.getName().equals(o2.getName()))
				return o1.getAge() - o2.getAge();
			return o1.getName().compareTo(o2.getName());
		}

	}

}

三.心得

今天学的内容前半部分接受程度还可以,后面再treeset的部分有一些不是很明白,明天早上得抓紧弄明白.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值