• 三.java集合的应用


    目录

    需求一:存放学生信息

    需求二:要求学生信息不能重复存放

    需求三:在不允许出现相同对象的情况下,如何进行按照年龄排序(也可以年龄为第一要素,成绩为第二要素排序)


    需求一:存放学生信息

    定义学生类,按年龄升序,将学生信息存放在集合中,遍历学生的信息(Student注释部分为后续的HashSet使用,此处可忽略)

    思路:①定义一个学生类,声明name,age,grade...等属性,并自动生成toString,和构造方法

               ②向集合中加入学生信息

               ③对学生按年龄排序     思路:调用sort,重写Comparator接口进行排序

               ④遍历集合中的学生信息

    1.定义一个学生类,声明name,age,grade...等属性,并自动生成toString,和构造方法

    package com.bdsw.wxl.day5;
    
    public class Student {//implements Comparable{
    	String name;
    	int age;
    	int grade;
    	
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + age;
    		result = prime * result + grade;
    		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 (grade != other.grade)
    			return false;
    		if (name == null) {
    			if (other.name != null)
    				return false;
    		} else if (!name.equals(other.name))
    			return false;
    		return true;
    	}
    
    	
    
    	@Override
    	public String toString() {
    		return "Student [name=" + name + ", age=" + age + ", grade=" + grade + "]";
    	}
    
    	public Student(String name, int age, int grade) {
    		super();
    		this.name = name;
    		this.age = age;
    		this.grade = grade;
    	}
    
    //	@Override
    //	public int compareTo(Object o) {
    //		if(o instanceof Student) {
    //			if(this.age-((Student) o).age>0) return 1;
    //			else return -1;
    //		}
    //		return 0;
    //	}
    
    	
    	
    }
    

    2.向集合中加入学生信息,对学生进行排序

    package com.bdsw.wxl.day5;
    
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.List;
    import java.util.ListIterator;
    
    
    /**
     * 
     * @author 王雪亮
     * @date 2018年7月20日
     * @Description
     *    ①向集合中加入学生信息
     *    ②遍历集合中的学生
     *    ③怎样使List中不能添加重复元素  -->此时引入了Set集合,可使用HashSet实现
     *    ④怎么处理排序问题                         -->使用TreeSet
     */
    public class ListTest {
    	public static void main(String[] args) {
    		// 1.创建4个学生对象
    		Student s1 = new Student("a", 15, 4);
    		Student s2 = new Student("a", 15, 4);
    		Student s3 = new Student("c", 18, 24);
    		Student s4 = new Student("d", 20, 24);
    
    		// 2.构造List集合,并将学生对象加进List集合中
    		List al = new ArrayList();
    		al.add(s1);
    		al.add(s2);
    		al.add(s3);
    		al.add(s4);
    
    		// 3.迭代器遍历排序前的List
    		System.out.println("排序前的List为:");
    		ListIterator it = al.listIterator();
    		while (it.hasNext()) {
    			System.out.println(it.next());
    		}
    
    		// 4.List集合处理排序问题
    		// ①调用sort
    		// ②重写Comparator接口
    		Comparator c=new Comparator() {
    
    			@Override
    			public int compare(Object o1, Object o2) {
    				if (o1 instanceof Student && o2 instanceof Student) {
    					Student s1 = (Student) o1;
    					Student s2 = (Student) o2;
    					if (s1.age > s2.age)
    						return 1;
    					else
    						return -1;
    				}
    				return 0;
    			}
    		};
    		al.sort(c);
    
    		// 5.加强for循环遍历排序后的List
    		System.out.println("排序后的List为:");
    		for (Object object : al) {
    			System.out.println(object + " ");
    		}
    
    	}
    
    }
    

    需求二:要求学生信息不能重复存放

    思路:此时引入了Set集合,可使用HashSet实现(无序列),也可使用LinkedHashSet实现(

        注:①实现Set时,需要重写equals和hashCode方法

               ②HashSet底层是Hash表,LinkedHashSet底层是Hash表和链表

    package com.bdsw.wxl.day5;
    
    import java.util.LinkedHashSet;
    import java.util.Set;
    
    public class SetTest {
    	public static void main(String[] args) {
    		// 1.创建2个学生对象
    		Student s1 = new Student("a", 15, 4);
    		Student s2 = new Student("b", 15, 4);
    
    		// 2.构造List集合,并将学生对象加进List集合中
    		//若使用HashSet,则无序(HashSet底层时哈希表)
    		Set<Student> al = new LinkedHashSet<Student>();
    		al.add(s1);
    		al.add(s2);
    		al.add(new Student("c", 18, 24));
    		al.add(new Student("f", 18, 24));
    		al.add(new Student("e", 18, 24));
    		al.add(new Student("f", 18, 24));
    		al.add(new Student("g", 18, 24));
    		
    		
    		
    		//3.加强for循环遍历Set集合
    		System.out.println("Set集合为:");
    		for (Object object : al) {
    			System.out.println(object+" ");
    		}
    	}
    }
    

    需求三:在不允许出现相同对象的情况下,如何进行按照年龄排序(也可以年龄为第一要素,成绩为第二要素排序)

    思路:使用TreeSet

          TreeSet两种排序方式:

                 1.自然排序:①Student类中实现 Comparable<T>接口   ②重写Comparable接口中的Compareto方法

                 2.比较排序   ①构造TreeSet对象,并且要让其实现Comparator接口   ②重写Comparator接口中的Compare方法

    自然排序在Student类中已经注释掉了,此处省略,以下展示比较排序

    package com.bdsw.wxl.day5;
    
    import java.util.Comparator;
    import java.util.Set;
    import java.util.TreeSet;
    
    public class TreeSetTest {
    	public static void main(String[] args) {
    		// 1.创建4个学生对象
    		Student s1 = new Student("a", 15, 90);
    		Student s2 = new Student("b", 15, 80);
    
    		// 2.构造List集合,并将学生对象加进List集合中
    		Set<Student> al = new TreeSet<Student>(new Comparator() {
    
    			@Override
    			public int compare(Object o1, Object o2) {
    				if(o1 instanceof Student&&o2 instanceof Student) {
    					Student s1=(Student)o1;
    					Student s2=(Student)o2;
    					if(s1.age-s2.age>0) return 1;
    					else if(s1.age-s2.age<0) return -1;
    					else {
    						if(s1.grade-s2.grade>0) return 1;
    						else return -1;
    					}
    				}
    				return 0; 
    				
    			}
    		});
    		al.add(s1);
    		al.add(s2);
    		al.add(new Student("c", 18, 24));
    		al.add(new Student("f", 20, 98));
    		al.add(new Student("e", 20, 89));
    		al.add(new Student("f", 5, 34));
    		al.add(new Student("g", 5, 20));
    
    		//3.排序
    		/**
    		 * 1.自然排序
    		 *    ①Student类中实现 Comparable<T>接口
    		 *    ②重写Comparable接口中的Compareto方法
    		 * 2.比较排序
    		 *    ①单独创建一个比较类,并且要让其继承Comparator接口
    		 *    ②重写Comparator接口中的Compare方法
    		 */
    
    		
    		// 4.加强for循环遍历Set集合
    		System.out.println("Set集合为:");
    		for (Object object : al) {
    			System.out.println(object + " ");
    		}
    	}
    }
    
  • 相关阅读:
    CentOS 网络配置
    BUUCTF-PWN爬坑-04-pwn1_sctf_2016
    BUUCTF-PWN爬坑-03-warmup_csaw_2016
    BUUCTF-PWN爬坑-02-rip
    此博客早八百年已停止维护
    重&长剖
    FHQ Treap
    NOIP2020游记
    CSP2020 游记
    线段树套平衡树
  • 原文地址:https://www.cnblogs.com/wangxueliang/p/9346454.html
Copyright © 2020-2023  润新知