Set frame (ii) set

A, HashSet memory hash table, repeating the underlying storage element Research

First we need to know,
set the collection element can be repeated ; (only for strings, the eight basic data types)
collection set element is disordered (stored and retrieved sequentially do not necessarily match)

1. As the following code:

package com.huangzhiyao.set;

import java.util.HashSet;
import java.util.Set;

public class Text2 {
		public static void main(String[] args) {
			Set set=new HashSet<>();
			set.add("lm");
			set.add("qb");
			set.add("lm");
			set.add("xf");
			System.out.println(set.size());
		}
		
		
  }

Run Results:
Here Insert Picture Description
the results of operation that can set a set of elements may not be repeated .

2. Another example is the following code:

package com.huangzhiyao.se't;

import java.util.HashSet;
import java.util.Set;

public class Text2 {
	public static void main(String[] args) {
		Set set=new HashSet<>();
		set.add(new Person("laomo", 18, 1500));
		set.add(new Person("dabai", 23, 500));
		set.add(new Person("xiaoxi", 19, 1200));
		set.add(new Person("goudan", 22, 2500));
		set.add(new Person("laomo", 18, 1500));//这里数据与第一条数据相同
		
		System.out.println(set.size());
		
	}
	}
	//定义Person类
	class Person{
	private String name;
	private int age;
	private int money;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", money=" + money + "]";
	}
	public Person(String name, int age, int money) {
		super();
		this.name = name;
		this.age = age;
		this.money = money;
	}
	public Person() {
		super();
	}
	}

After the printing operation whereby the result is 5 , so the set is limited only to the repeated string, eight basic data types .

3. Another example is the following codes:

package com.huangzhiyao.set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Text2 {
public static void main(String[] args) {
	Set set=new HashSet<>();
	set.add(new Person("laomo", 18, 1500));
	set.add(new Person("dabai", 23, 500));
	set.add(new Person("xiaoxi", 19, 1200));
	set.add(new Person("goudan", 22, 2500));
	set.add(new Person("laomo", 18, 1500));//这里数据与第一条数据相同
	
	
	Iterator it=set.iterator();//定义一个迭代器
	while(it.hasNext()) {
		System.out.println(it.next());
	}
	
}
}
//定义Person类
class Person{
private String name;
private int age;
private int money;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public int getMoney() {
	return money;
}
public void setMoney(int money) {
	this.money = money;
}
@Override
public String toString() {
	return "Person [name=" + name + ", age=" + age + ", money=" + money + "]";
}
public Person(String name, int age, int money) {
	super();
	this.name = name;
	this.age = age;
	this.money = money;
}
public Person() {
	super();
}
}

After printing run more than a few you will find a different order , then we can know the set collection elements are unordered (into and out of order do not necessarily match).

So HashSet is how to ensure the uniqueness of the elements?

Elements in two ways: hashCode and equals method to complete;
if hashCode value of the same , will determine equals whether to true ;
if hashCode values differ , so do not call equals .
When it is set on a custom storage class is determined whether the need to repeat the demand override hashCode () and equals (Object obj) method.

As the following code:

package com.huangzhiyao.set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Text2 {
public static void main(String[] args) {
	Set set=new HashSet<>();
	set.add(new Person("laomo", 18, 1500));
	set.add(new Person("dabai", 23, 500));
	set.add(new Person("xiaoxi", 19, 1200));
	set.add(new Person("goudan", 22, 2500));
	set.add(new Person("laomo", 18, 1500));//这里数据与第一条数据相同
	
	//System.out.println(set.size());
	
	Iterator it=set.iterator();//定义一个迭代器
	while(it.hasNext()) {
		System.out.println(it.next());
	}
	
}
}
//定义Person类
class Person{
private String name;
private int age;
private int money;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public int getMoney() {
	return money;
}
public void setMoney(int money) {
	this.money = money;
}
@Override
public String toString() {
	return "Person [name=" + name + ", age=" + age + ", money=" + money + "]";
}
public Person(String name, int age, int money) {
	super();
	this.name = name;
	this.age = age;
	this.money = money;
}
public Person() {
	super();
}
@Override
public int hashCode() {
	System.out.println("hashCode---------"+this.name);
	int code=this.name.hashCode()+this.age;//算一个name和age的哈希码值的和
	System.out.println(code);
	return code;
}
@Override
public boolean equals(Object obj) {
	System.out.println("equals---");
	Person p=(Person)obj;
	return this.name.equals(p.name) && this.age==p.age;
}
}

Second, set the frame TreeSet (natural order, binary tree data structure, the comparator sorting)

1.TreeSet natural ordering

That the element includes a comparative achieve Comparable interface class object, the rewriting compareTo method
first embodiment TreeSet sorted, so the element itself with comparative ;
elements need to implement Comparable interface, covering compareTo method;
this manner also known an element of the natural order , or called a default order

For example, run the following code:

package com.huangzhiyao.set;

import java.util.TreeSet;

public class TreeSetTest {
		public static void main(String[] args) {
			TreeSet set=new TreeSet<>();
			set.add(74);   //存放int类型排序
			set.add(14);
			set.add(68);
			set.add(23);
			System.out.println(set);
		}

}

Results are as follows:
Here Insert Picture Description

Another example:

package com.huangzhiyao.set;

import java.util.TreeSet;

public class TreeSetTest {
		public static void main(String[] args) {
			TreeSet set=new TreeSet<>();
//			set.add(74);  //存放int类型排序
//			set.add(14);
//			set.add(68);
//			set.add(23);

			set.add("q");   //存放String类型排序
			set.add("x");
			set.add("a");
			set.add("qw");
			set.add("c");
			
			System.out.println(set);
		}
		}

Results are as follows:
Here Insert Picture Description
we can store is found chaos, when taken out automatically sorted.

2. binary tree data structure

Set may be ordered set, the underlying data structure is a binary tree

package com.huangzhiyao.set;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetTest {
		public static void main(String[] args) {
			TreeSet set=new TreeSet<>();
			set.add(new Person("wanting", 18, 1500));
			set.add(new Person("zhuangyuan", 23, 500));
			set.add(new Person("runchen", 19, 1200));
			set.add(new Person("xiang", 22, 2500));
			set.add(new Person("wantings", 88, 1500));
//			System.out.println(set);
			Iterator it = set.iterator();
			while(it.hasNext()) {
				System.out.println(it.next());
			}
		}
		}
		//定义Person类
		class Person implements Comparable<Person>{
		private String name;
		private int age;
		private int money;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
		public int getMoney() {
			return money;
		}
		public void setMoney(int money) {
			this.money = money;
		}
		@Override
		public String toString() {
			return "Person [name=" + name + ", age=" + age + ", money=" + money + "]";
		}
		public Person(String name, int age, int money) {
			super();
			this.name = name;
			this.age = age;
			this.money = money;
		}
		public Person() {
			super();
		}
		@Override
		public int hashCode() {
			System.out.println("hashCode---------"+this.name);
			int code=this.name.hashCode()+this.age; //算一个name和age的哈希码值的和
			System.out.println(code);
			return code;
		}
		@Override
		public boolean equals(Object obj) {
			System.out.println("equals---");
			Person p=(Person)obj;
			return this.name.equals(p.name) && this.age==p.age;
		}
		/**
		 * 
		 * 让元素具有比较性
		 * 
		 * 注意:在做自然排序方法重写的时候,一定要先判断主要条件,还要判断次要条件
		 * 
		 */
		@Override
		public int compareTo(Person o){
			int num=o.money-this.money;
			if(num==0) {
				return o.age-this.age;
			}
			return num;
		}
		}

3.TreeSet sort comparator

When the element itself does not have a comparative time, or when the comparative is not required to have
at this time need to have their own set of comparative , when a collection is initialized, there is a comparison mode ;
for example, to be young and more gold examples
such as the following Code:

package com.huangzhiyao.set;

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

public class TreeSetTest {
	public static void main(String[] args) {
		TreeSet<Person> set = new TreeSet<>(new PersonMoneyAgeComp());// 调用比较器
		set.add(new Person("王五", 12, 1000));
		set.add(new Person("王五", 12, 1800));
		set.add(new Person("张三", 18, 1900));
		set.add(new Person("张三丰", 22, 5000));
		set.add(new Person("李四", 90, 1000));

		Iterator it = set.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}

/**
 * 比较器排序 需求:money多 age小
 */
class PersonMoneyAgeComp implements Comparator<Person> {

	@Override
	public int compare(Person o1, Person o2) {
		// TODO Auto-generated method stub
		int num = o2.getMoney() - o1.getMoney();// 比较money
		if (num == 0) {// 当money相等时
			return o1.getAge() - o2.getAge();// 比较age
		}
		return num;
	}

}

/**
 * age比较
 * 
 * @author 10570
 *
 */
class Person implements Comparable<Person> {
	private String name;
	private int age;
	private int money;

	public Person(String name, int age, int money) {
		super();
		this.name = name;
		this.age = age;
		this.money = money;
	}

	public Person() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getMoney() {
		return money;
	}

	public void setMoney(int money) {
		this.money = money;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", money=" + money + "]";
	}

	// 重写hashcode方法
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		int code = this.name.hashCode() + this.age;// 计算名字和年龄的值
		System.out.println(this.toString() + "-----code:" + code);// 查看code值
		return code;
	}

	// 重写equals
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub

		Person p = (Person) obj;
		System.out.println("比较名字:" + this.name + "---" + p.name);
		System.out.println("比较年龄:" + this.age + "---" + p.age);
		return this.name.equals(p.name) && this.age == p.age;// 当值code相等时,就比较名字和年龄是否相等
	}

	/**
	 * 让元素具备比较性 注意:在做自然排序方法重写的时候,一定先判断主要条件、还要判断次要条件
	 */
	@Override
	public int compareTo(Person o) {
		// TODO Auto-generated method stub
		return this.age - o.age;
	}

}

The result:
Here Insert Picture Description
the above we can see in front of the multiple money, when money is equal, in front of the small Age.

We then conditions are changed: more money older

/**
 * 比较器排序 需求:age大 money少
 */
class PersonAgeMoneyComp implements Comparator<Person>{

	@Override
	public int compare(Person o1, Person o2) {
		// TODO Auto-generated method stub
		int num = o2.getAge() - o1.getAge();
		if(num == 0) {//当money相等时
			return o1.getMoney() - o2.getMoney();
		}
		return num;
	}
	
}

After running:
Here Insert Picture Description

Third, Generics

1. What is a generic?

Without the use of generics, it will be unknown error performance at runtime.
If the code to deal with this mistake might be found, then the runtime errors will not be exposed.
E.g:

package com.huangzhiyao.set;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class FanXinTest {
	public static void main(String[] args) {
		List c = new ArrayList();
		c.add(22);
		c.add(23);
		c.add(26);
		c.add(28);
		c.add(55);
		c.add("错误");
		
		Iterator it = c.iterator();
		while(it.hasNext()) {
			Object obj = it.next();
			int num = (int) obj;
			if(num % 2 == 0) {
				System.out.println(num);
			}
				
		}
		
	}
	}

The result:
Here Insert Picture Description
add the generic:
Here Insert Picture Description

2. Generic benefits

(1) converting the abnormal run into compile errors, programmers found earlier, so as to solve the code hidden.
(2) improved robustness of the code.

3. Simple generic application

/*
 * 购物车项目
 * 订单模块、用户模块、商品模块
 * Class OrderDao{
 *  public List<Order> list(Order o){}
 *  public int add(Order o){}
 *  public int edit(String id,Order o){}
 *  public int del(String id){}
 *  
 * }
 * Class UserDao{
 *  public List<User> list(User u){}
 *  public int add(User u){}
 *  public int edit(String id,User u){}
 *  public int del(String id){}
 *  
 * }
 *  * Class ProductDao{
 *  public List<Product> list(Product p){}
 *  public int add(Product p){}
 *  public int edit(String id,Product p){}
 *  public int del(String id){}
 *  
 * }
 * 
 * ----不使用泛型的情况---
 * ----使用泛型的情况---
 * Class BaseDao<T>{
 *  public List<T> list(T t){}
 *  public int add(T t){}
 *  public int edit(String id,T t){}
 *  public int del(String id){}
 * }
 *Class OrderDao extends BaseDao<Order>{}
 *Class UserDao extends BaseDao<User>{}
 *Class ProductDao extends BaseDao<Product>{}
 * 
 */

Guess you like

Origin blog.csdn.net/weixin_44234514/article/details/94748329
set
set