Object and Set Summary

Object

1. Basic

Java中所有类的基类!!!
Java中所有的类都是间接或者直接继承Object类。
Object类的引用数据类型变量可以保存Java中任意数据类型空间的首   地址。

Some methods specified within Object class:
String toString ();

	当前对象建议String类型描述。默认情况是当前类所属包名.类名@十六进制内存地址
	如果对于数据类型展示有要求,可以重写toString方法,在展示的方法中会默认执行
	toString方法
int hashCode();
	内存中当前对象的唯一索引值,默认情况下是当前对象所处空间首地址的十进制展示。
boolean equals(Object obj);
	比较方法,判断两个对象是否一致,Object类内默认情况下比较的方式是地址比较。
	两个对象地址一致,表示肯定是相同对象。如果我们期望修改equals比较规则,可以
	在当前类内重写
	【注意】
    	Java中规定,如果两个对象的equals比较方法结果为true,要求hashCode值必须
    	一致

线程有关方法
void wait();
void notify();
void notifyAll();

反射有关方法

2.equals method

Compares two objects consistent default in the Object class whether an address is to compare two objects of the same.

There are some cases of code, you need to compare two objects that have been saved in the content, but using the equals method in the Object class inherited, it is unreasonable! ! !

Rewrite method

  1. Analyzing two objects are not the same object. If the class object and call the method of passing parameters to the class object
  2. The same address, and that is the same object, returns true, get! ! !
  3. equals method parameter is of type Object, it means that any type of data can be used as parameters.
  4. Two types of data are inconsistent, the need for comparison.
  5. Analyzing the data types are consistent
  6. Use the keyword instanceOf, continue to run the same type of data, unusual type, the end of the judge returns false
  •  格式:                                                          
     类对象 instanceOf 类名                                        
     判断对象中保存的数据                                                
     Student中我们比较id, name, age, gender就可以了                    
    

3.hashCode method

Returns the current contents of the first address space objects decimal display mode.

hashCode usage rewriting is not performed, the address will be used as data corresponding hashCode, after rewriting, the address is no longer used. After the rewrite hashCode does not correspond to the address where the current object.

Set collection

1. characterized
wherein:
random, non-repeatable

无序:添加顺序和存储顺序不一致,【不代表有排序效果】
不可重复: 在一个Set集合中不能出现相同元素
  • 2. hash table storage *
	HashSet<Person> hashSet = new HashSet<Person>();
		
		Person p1 = new Person(1, "宝哥", 10);
		Person p2 = new Person(2, "大熊", 15);
		Person p3 = new Person(3, "骚磊", 2);
		Person p4 = new Person(4, "骚杰", -5);
		Person p5 = new Person(5, "林妹妹", 11);
		
		/*
		 * 当前这里两个元素,ID一样 ==> hashCode值是一致,会通过底层哈希表运算
		 * 保存到同一个单元格位置。
		 * 这里会通过equals方法,比较两个对象是否一致,来决定是否能够保存。
		 * 如果两个对象一致,无法保存。
		 * 
		 * 期望每一个哈希表单元格内保存的数据是唯一
		 */
		Person p6 = new Person(6, "康爷", 8);
		Person p7 = new Person(6, "康牙", 10);
		
		
		hashSet.add(p4);
		hashSet.add(p6);
		hashSet.add(p3);
		hashSet.add(p5);
		hashSet.add(p2);
		hashSet.add(p1);
		hashSet.add(p7);

3.TreeSet tree storage

                没有比较方式无法存储
  1. Comparable interface
  interface Comparable<T> {
	int compareTo(T t);
}

Method type parameter T, the time to comply with the class that implements interface constraints,
the compareTo method returns an int type value, 0, negative, positive numbers
0 represents a consistent two elements, if the result of this comparison TreeSet, represent the same element, unable to store a second.

Comparable interface comply with a storage element corresponding to the class, the method is completed

  1. Comparator interface
  interface Comparator<T> {
	int compare(T o1, T o2);
}

A complete custom comparator class object,
int returns the value 0, negative, positive numbers
0 represents a consistent two elements, if the result of the comparison in TreeSet is 0, represent the same element, the second can not be stored.

Released four original articles · won praise 7 · views 679

Guess you like

Origin blog.csdn.net/Luyihx/article/details/104486513