java学习第12天


Collection 接口 经常使用的子接口
             List 接口 --》存放的元素有序且容许有重复的集合接口java

             Set 接口 --》存放的元素不包括重复的集合接口算法

 

Set接口的实现类
             HashSet   散列存放 -- 不保存元素的加入顺序ide

             TreeSet   有序存放this

             LinkeHashSetcode


HashSet --> 调用hashCode() 用固定的算法算出它的存储索引 而后把值放在一个
教散列表的相应位置(表元)中对象

             若是对应的位置没有其余元素,直接存入排序

             若是该位置有元素了,将新的值跟该位置的全部值进行比较
这时候 就调用 equals() 方法 看有没有该值 没有就存入 有就直接使用索引

******************************************************************
重点                                                             *
                                                                 *
    对于要存放到HashSet集合中的对象 对应的类必定要重写equals方法 *
和hashCode(Object obj) 方法 以实现对象相等规则                   *
                                                                 *
         *
******************************************************************接口


public class SetDemo {
 public static void main(String[] args) {
  ArrayList<String> list = new ArrayList<String>();
  list.add("0");
  list.add("1");
  list.add("2");
  list.add("3");
  list.add("4");
  list.add("5");
  list.add("5");
  list.add("5");
  System.out.println(list);
  
  //set 不保存元素的加入顺序
  //set 集合是一个不能够重复的集合
  //问题:为何能够不重复?
  //原理:它底层是一个Map   -- key -value 对, key 跟 value 一一对应,其中
  //key 是不能重复的
  HashSet<String> set = new HashSet<String>();
  set.add("1");
  set.add("1");
  set.add("1");
  set.add("1");
  set.add("1");
  set.add("1");
  set.add("1");
  set.add("1");
  System.out.println(set);
 }
}开发

结果 [0, 1, 2, 3, 4, 5, 5, 5]
     [1]

---------------------------------------------------------------

 

import java.util.HashSet;

//set 进行存储的时候,
//一、若是是添加同一个对象(即把同一个对象添加屡次),那么set只添加一次,无论hashcode
 //equals 方法的结果是啥
 //其结果是由它底层的map决定,由于key只能惟一

//二、若是添加的不是同个对象,那么先调用 hashcode方法,获取hashcode值 ,
//若是 hascode 同样,那么就调用equals方法,若是返回时true表示元素不能够存入,
//反之则能够

//得出的结果:
//实际开发中,若是须要将自定义的类添加入set中,同样重写hashcode  跟 equals
//怎么去重写 hascode 跟 equals
public class Test {
 public static void main(String[] args) {
  HashSet<Point> set = new HashSet<Point>();
  Point point = new Point(1, 1);
  Point point2 = new Point(1, 1);
  Point point3 = new Point(1, 1);
  Point point4 = new Point(1, 5);
  set.add(point);
  set.add(point2);
  set.add(point3);
  set.add(point4);
  System.out.println(set);
  
 }
}
------------

blic class Point{
 public int x;
 public int y;
 
 private String a;
 private String b;
 
 public Point(int x, int y) {
  super();
  this.x = x;
  this.y = y;
 }
 public int getX() {
  return x;
 }
 public void setX(int x) {
  this.x = x;
 }
 public int getY() {
  return y;
 }
 public void setY(int y) {
  this.y = y;
 }
 @Override
 public String toString() {
  return "Point [x=" + x + ", y=" + y + "]";
 }
 
 
 //重写hashcode 的缘由:为了提升效率
 //一、获取值的时候,是经过hascode进行判断的,若是hashcode同样,
 //那么必须比较内容,才能肯定具体获取那个值
 //二、添加的时候,若是hashcode值不同,就能够不须要调用equals判断是否一致
 
 //属性1的int形式+ C1*属性2的int形式+  C2*属性3的int形式+ …
 //系数:通常是一个大一点的质数   31  c2
 @Override
 public int hashCode() {
  //this.x + 31*this.y;
  String xS = this.x + "";
  String yS = this.y + "";
  return xS.hashCode() + this.x + 31 * yS.hashCode();
 }
 
 
 
 
 @Override
 public boolean equals(Object obj) {
  if(obj == null){
   return false;
  }
  if(this == obj){
   return true;
  }
  if(obj instanceof Point){
   Point point = (Point) obj;
   
   if(point.x == this.x && point.y == this.y){
    return true;
   }else{
    return false;
   }
  }
  return false;
 }
 
}

结果 [Point [x=1, y=1], Point [x=1, y=5]]


-------------------------------------------------------------------------


TreeSet -->一、能够排序 二、不能够重复

 

public class TreeSetDemo {
 public static void main(String[] args) {
  //set集合不能放重复的元素
  //TreeSet  
  //添加进去的元素,无论添加顺序怎样子的,输出结果,都是同样的
  //添加进去的元素,不能重复,若是重复,取其中一个 (由set的特性决定)
  TreeSet<String> set = new TreeSet<String>();
  set.add("0");
  set.add("1");
  set.add("7");
  set.add("4");
  set.add("3");
  set.add("5");
  set.add("6");
  set.add("2");
  System.out.println(set);
 }
}

-------------------------------------------------------------------


Comparable 接口  —— 全部可排序的类均可以经过该接口实现

          该接口惟一方法
                      public int compareTo(Object obj)

                      返回 0        表示 this == obj 输出里面的
                      返回正数      表示 this > obj  小 -- 大排
                      返回负数      表示 this <obj   大 -- 小排

 


public class Test {
 public static void main(String[] args) {
  
  TreeSet<Student> set = new TreeSet<Student>();
  for(int i = 0; i < 5; i++){
   set.add(new Student(i, "name_" + i));
  }
  
  for(Student student : set){
   System.out.println(student);
  }
 }
}

---------

public class Student implements Comparable<Student>{
 int id;
 String name;
 public Student(int id, String name) {
  super();
  this.id = id;
  this.name = name;
 }
 @Override
 public String toString() {
  return "Student [id=" + id + ", name=" + name + "]";
 }
 @Override
 public int compareTo(Student o) { //返回值 只能int 类型
  
  //id小的排在前面
  //this.id  加进去的元素
  //o.id  里面的元素
  //return 0; return 1; return -1;
                //return this.id; return o.id;

  return  this.id - o.id ;
 }
}
结果 Student [id=4, name=name_4]
     Student [id=3, name=name_3]
     Student [id=2, name=name_2]
     Student [id=1, name=name_1]
     Student [id=0, name=name_0]

----------------------------------------------------------------------

Comparator 接口
            由于使用Comparable 接口定义排序顺序有局限性(实现此接口的类
只能按compareTo() 定义的这一种方式排序)


Comparator 接口中的比较方法
              public int compare(Object o1,Object o2)

             返回 0        表示 o1 == o2 输出里面的
             返回正数      表示 o1 > o2  小 -- 大排
             返回负数      表示 o1 <o2   大 -- 小排

 

public class Student{
 int id;
 String name;
 double score;
 public Student(int id, String name, double score) {
  super();
  this.id = id;
  this.name = name;
  this.score = score;
 }
 @Override
 public String toString() {
  return "Student [id=" + id + ", name=" + name + ", score=" + score + "]";
 }
}
-----------

import java.util.Comparator;
import java.util.TreeSet;

public class Test {
 public static void main(String[] args) {
  //Comparator 比较器    
  TreeSet<Student> set = new TreeSet<Student>(
    new Comparator<Student>() {
   @Override
   public int compare(Student o1, Student o2) {
    //o1   外面的
    //o2  是里面的
    return o1.id - o2.id;
   }
  });
  
  TreeSet<Student> set2 = new TreeSet<Student>(
    new Comparator<Student>() {
   @Override
   public int compare(Student o1, Student o2) {
    //o1   外面的
    //o2  是里面的
    if(o1.score == o2.score){
     return 0;
    }
    return o1.score > o2.score ? -1 : 1;
   }
  });
  
  for(int i = 0; i < 5; i++){
   set.add(new Student(i, "name_" + i, 40 * i));
  }
  
  for(Student student : set){
   System.out.println(student);
  }
  
  System.out.println("---------------------------");
  for(int i = 0; i < 5; i++){
   set2.add(new Student(i, "name_" + i, 40 * i));
  }
  
  for(Student student : set2){
   System.out.println(student);
  }
  
 }
}
结果
Student [id=0, name=name_0, score=0.0]
Student [id=1, name=name_1, score=40.0]
Student [id=2, name=name_2, score=80.0]
Student [id=3, name=name_3, score=120.0]
Student [id=4, name=name_4, score=160.0]
---------------------------
Student [id=4, name=name_4, score=160.0]
Student [id=3, name=name_3, score=120.0]
Student [id=2, name=name_2, score=80.0]
Student [id=1, name=name_1, score=40.0]
Student [id=0, name=name_0, score=0.0]


----------------------------------------------------------------------

LinkedHashSet


import java.util.HashSet;
import java.util.LinkedHashSet;

public class LinkedHSetDemo { public static void main(String[] args) {    //元素最终打印的顺序跟添加的顺序无关,跟具体的hashcode值  HashSet<String> set = new HashSet<String>();    set.add("0");  set.add("5");  set.add("2");  set.add("3");  set.add("7");  set.add("4");  set.add("1");  set.add("6");    for(String string : set){   System.out.println(string);  }    System.out.println("-----------------");    //元素最终打印的顺序跟添加的顺序同样  LinkedHashSet<String> linkedHashset = new LinkedHashSet<String>();  linkedHashset.add("0");  linkedHashset.add("7");  linkedHashset.add("6");  linkedHashset.add("2");  linkedHashset.add("3");  linkedHashset.add("5");  linkedHashset.add("1");  linkedHashset.add("4");    for(String string : linkedHashset){   System.out.println(string);  } }}

相关文章
相关标签/搜索