集合框架02

Set

 


  1.1 特点:无序、对象不能重复

//无序的,不可重复的
		Set<Student> set = new HashSet<>();
		set.add(new Student(1, "zs", 80f));
		set.add(new Student(2, "ff", 54f));
		set.add(new Student(3, "rr", 42f));
		set.add(new Student(1, "zs", 120f));


  1.2 遍历
    1.2.1 foreach
    1.2.2 迭代器   

//foreach遍历1.8特性
		set.forEach(System.out::println);

		//foreach遍历
		for (Student student : set) {
			System.out.println(student);
		}
		System.out.println("------------------");
		
		//迭代器遍历
		Iterator<Student> it = set.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}


  1.3 常用实现类
      HashSet
      TreeSet:根据某种(规则)对里面的元素进行排序

        自然比较接口: java.lang.Comparable 
        比较器: java.util.Comparator
        String以AscII码进行比较,返回差值

	Set<String> set1 = new TreeSet<>();
		set1.add("zs");
		set1.add("fw");
		set1.add("jfd");
		set1.add("zs");

		Set<Student> set = new TreeSet<>();
		set.add(new Student(1, "zs", 80f));
		set.add(new Student(2, "fw", 54f));
		set.add(new Student(3, "jfd", 42f));
		set.add(new Student(1, "zs", 120f));

		Student stu = new Student();
		stu.equals(null);

		set.forEach(System.out::println);

 LinkedHashSet:


  1)元素是有顺序
  2)元素是不重复
  3)底层数据结构是按照链表的结构存储的 Linked

  hashCode

  队列 Queue:表示一个先入先出的数据结构(自行研究)

  堆栈 Stack:表示一个先进后出的数据结构
  压:push
  弹:pop

private LinkedList list = new LinkedList<>();

	//压
	private void push(Object o) {
		list.addFirst(o);
	}

	//弹
	public Object pop() {
		Object first = list.getFirst();
		list.removeFirst();
		return first;
	}

	public int size() {
		return list.size();
	}

	public static void main(String[] args) {
		MyStack stack = new MyStack();
		stack.push("a");
		stack.push("b");
		stack.push("c");
		stack.push("d");
		stack.push("e");

		while (stack.size() != 0) {
			System.out.println(stack.pop());
		}

	}
package com.zking.entity;

import java.io.Serializable;

public class Student implements Serializable, Comparable<Student> {

	private Integer sid;
	private String sname;
	private Float score;

	public Student() {
		// TODO Auto-generated constructor stub
	}

	public Student(Integer sid, String sname, Float score) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.score = score;
	}

	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public Float getScore() {
		return score;
	}

	public void setScore(Float score) {
		this.score = score;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((score == null) ? 0 : score.hashCode());
		result = prime * result + ((sid == null) ? 0 : sid.hashCode());
		result = prime * result + ((sname == null) ? 0 : sname.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 (score == null) {
			if (other.score != null)
				return false;
		} else if (!score.equals(other.score))
			return false;
		if (sid == null) {
			if (other.sid != null)
				return false;
		} else if (!sid.equals(other.sid))
			return false;
		if (sname == null) {
			if (other.sname != null)
				return false;
		} else if (!sname.equals(other.sname))
			return false;
		return true;
	}

	@Override
	public int compareTo(Student o) {
		if (this.getSid() > o.getSid()) {
			return 1;
		} else {
			if (this.getSid() == o.getSid()) {
				return 0;
			} else {
			}
			return -1;
			// return -(this.getSid()-o.getSid());
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值