set the framework of the collection collection

set collection framework

1. collection set frame set frame Set (HashSet memory hash table, repeating the underlying storage element Research)
1.set basics
set not store duplicate set of elements (for string only, eight basic data types) is set in the set of elements disorder (stored and retrieved sequentially do not necessarily match)
tests are as follows:

package com.wangcong.set;

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

public class SetTest {
public static void main(String[] args) {
	Set set=new HashSet<>();
	set.add("ni");
	set.add("wo");
	set.add("ta");
	set.add("ni");//此次与第一条add数据一致
	System.out.println(set.size());
}
}

Here Insert Picture Description
Operating results for the 3. This can be seen not store duplicate element set collection

Run the following code:

package com.wangcong.set;

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

public class SetTest {
public static void main(String[] args) {
	Set set=new HashSet<>();
	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("wanting", 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();
}
}

Console Print 5 results. Description set only for limited repeated string, eight basic data types

Test the following code:

package com.wangcong.set;

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

public class SetTest {
public static void main(String[] args) {
	Set set=new HashSet<>();
	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("wanting", 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();
}
}

Printing results are shown:
Here Insert Picture Description
it can be seen: SET in the set of elements is disordered (stored and retrieved sequentially do not necessarily match)

1.2 Repeat storage elements to explore the underlying
HashSet is how to ensure the uniqueness of the elements?
Ideas:
a method by two elements: to complete the hashCode and equals method; if the values are the same hashCode, equals will determine whether the true; if hashCode values are different, it does not call equals.

package com.wangcong.set;

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

public class SetTest {
public static void main(String[] args) {
	Set set=new HashSet<>();
	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("wanting", 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;
}
}

2, the frame set TreeSet (natural order, binary tree data structure, the comparator sorting)
2.1 Natural sorting
run the following code:

package com.wangcong.set;

import java.util.TreeSet;

public class TreeSetTest {
public static void main(String[] args) {
	TreeSet set=new TreeSet<>();
	set.add(25);
	set.add(23);
	set.add(31);
	set.add(28);
	System.out.println(set);
}
}

Results are as follows:
Here Insert Picture Description
2.1.2
Run the following code:

public class TreeSetTest {
public static void main(String[] args) {
	TreeSet set=new TreeSet<>();
//	set.add(25);
//	set.add(23);
//	set.add(31);
//	set.add(28);
	set.add("abc");
	set.add("be");
	set.add("dc");
	set.add("geg");
	set.add("ge");
	System.out.println(set);
}
}

Enter the results:
Here Insert Picture Description

2.2 binary tree data structure
what is binary: treeSet container is a natural Sort the elements in the container based on the collation of the binary tree ( the element itself with comparative )

package com.wangcong.set;

import java.util.Iterator;
import java.util.TreeSet;
/**
 * 2.集合框架TreeSet(自然排序.数据结构二叉数。比较器排序)
 * 1.treeSet容器是根据二叉数的排序规则对容器中元素进行排序的 自然排序(元素自身具有比较性)
 * @author wangcong
 *
 */
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;
}
}

2.3 Sorting comparator (for coping with some code change frequently inconvenient situation brought about)
by implementing accomplished Comparator (different methods to complete the call) does not need to modify the code every time, a method may be practiced rollback

package com.wangcong.set;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
 * 2.集合框架TreeSet(自然排序.数据结构二叉数。比较器排序)
 * 1.treeSet容器是根据二叉数的排序规则对容器中元素进行排序的 自然排序(元素自身具有比较性)
 * @author wangcong
 *
 */
public class TreeSetTest {
public static void main(String[] args) {
//	TreeSet set=new TreeSet<>();
//	TreeSet<Person> set=new TreeSet<>(new Personba());
	TreeSet<Person> set=new TreeSet<>(new Personjiu());
	set.add(new Person("wanting", 18, 1500));
	set.add(new Person("zhuangyuan", 18, 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());
	}
}
}
/**
 * 年小钱多
 * @author wangcong
 *
 */
class Personba implements Comparator<Person>{
	public int compare(Person o1, Person o2) {
	int num=o1.getAge()-o2.getAge();//主要条件年少
	if(num==0) {
		return o2.getMoney()-o1.getMoney();//次要钱多
	}
		return num;
	}
}
/**
 * 多钱年轻
 * @author wangcong
 *
 */
class Personjiu implements Comparator<Person>{
	public int compare(Person o1, Person o2) {
	int num=o2.getMoney()-o1.getMoney();//主要条件钱多
	if(num==0) {
		return o1.getAge()-o2.getAge();//次要年少
	}
		return num;
	}
}

//定义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, generics
what generics are 3.1? what is the benefit?
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
generic benefits:
1. run abnormal converted into compile errors, programmers found earlier, so as to solve the code hidden
2. to improve the robustness of the code

3.2

package com.wangcong.set;

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

/**
 * 
 * @author wangcong
 *
 */

public class FanXinTest {
public static void main(String[] args) {
	List c=new ArrayList<>();
	c.add(23);
	c.add(25);
	c.add(26);
	c.add(28);
	c.add(55);
	
	Iterator it = c.iterator();
	while(it.hasNext()) {
		Object obj=it.next();
		if(obj instanceof Integer) {
			int num=(int) obj;
			if(num %2 == 0) {
				System.out.println(num);
			}
		}
	}
	
}
}
/*
 * --------不使用泛型的情况-----------------
 * 购物车项目
 * 订单模块,用户模块、商品模块
 * Class OrderDao{
 * 	public List<Order> list(Order order){
 * 
 * 	}
 * 
 * 	public int add(Order order){}
 *  public int edit(Order order){}
 *  public int del(Order order){}
 * 
 * }
 * 
 * Class UserDao{
 * 	public List<User> list(User User){}
 * 	public int add(User User){}
 *  public int edit(User User){}
 *  public int del(User User){}
 * 
 * }
 * 
 * Class ProductDao{
 * 	
 * }
 * 
 * --------使用泛型的情况-----------------
 * Class BaseDao<T>{
 *  public List<T> list(T t){}
 * 	public int add(T t){}
 *  public int edit(T t){}
 *  public int del(T t){}
 * }
 * 
 * Class OrderDao extends BaseDao<Order>{}
 * Class UserDao extends BaseDao<User>{}
 * Class ProductDao extends BaseDao<Product>{}
 */

Overall, generics on the amount of code a bit to reduce duplication of code, reducing the duplication of development time used, do not repeat write code that only need to pass different types can simplify the code

Guess you like

Origin blog.csdn.net/weixin_44255950/article/details/94637639