彦舜原创,CSDN首发:The difference between TreeSet and HashSet in interface Set

2 篇文章 0 订阅
1 篇文章 0 订阅
/**
 * 
 */
package util.java;

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

/** @author Yanshun */
public class Collection_Set {

	/**
	 * 
	 */
	public Collection_Set() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/** Instantiate interface objects of collection:
		 *  TreeSet save self-definition class object:
		  
		  	Cannot be resolved to a type
		  	--Import 'Class.getName' (Class.getClass) 
		  
		  	The value of the local variable is not used
		  	--Add @SuppressWarnings 'unused' to 'field/method'
		  
			Runtime: Exception in thread "main" java.lang.NullPointerException
		    --TreeSet not can access null elements.
			Set<Integer> ts = new TreeSet<>();
			ts.add(null); 
			
		  	*/
		//Instantiate interface objects of collection
		Set<SelfDefine> hs = new HashSet<>();
		
		//Add elements into collection
		hs.add(null);
		hs.add(new SelfDefine(8000, "姚执永"));
		hs.add(new SelfDefine(12000, "彦舜"));
		hs.add(new SelfDefine(14000, "Jinyu"));
		
		/* Output directly elements of collection:
		 * [null, util.java.SelfDefine@79698539, util.java.SelfDefine@2ed94a8b, util.java.SelfDefine@73f792cf]
		 * Therefore, it is necessary to override/cover method toString(). */
		//The result show that elements are unordered in collection
		System.out.println(hs);
		
		System.out.println("-------------separate---------------------");
		
		//Therefore, it is needed to sort
		Set<SelfDefine> ts = new TreeSet<>();
		ts.add(new SelfDefine(8000, "姚执永"));
		ts.add(new SelfDefine(14000, "Jinyu"));
		ts.add(new SelfDefine(12000, "彦舜"));
		System.out.println(ts);
		
		System.out.println("------------------separate----------------");
		
		//On the base of string, from this we can see, Sort by initials
		ts.add(new SelfDefine(12000, "姚瑾瑜"));
		System.out.println(ts);
		
		System.out.println("-----------------separate-------------");
		
		//Repeated data don't save in this kind of collection
		ts.add(new SelfDefine(12000, "彦舜"));
		System.out.println(ts);
		
		System.out.println("----------------separate--------------");
		
		HashSet<Integer> hs2 = new HashSet<>();
		hs2.add(43);
		hs2.add(49);
		hs2.add(46);
		hs2.add(47);
		hs2.add(43);
		System.out.println(hs2);
		
		System.out.println("-------------------separate----------------");
		
		hs.add(new SelfDefine(14000, "Jinyu"));
		hs.add(new SelfDefine(8000, "姚执永"));
		System.out.println(hs);
	}
}

//TreeSet save self-definition class object
class SelfDefine extends Object implements java.lang.Comparable<SelfDefine> {
	/** Instantiate interface objects of collection:
	 *  TreeSet save self-definition class object:
		  
		Cannot be resolved to a type
		--Import 'Class.getName' (Class.getClass) 
		  
		The value of the local variable is not used
		--Add @SuppressWarnings 'unused' to 'field/method'
		  
		Runtime: Exception in thread "main" java.lang.NullPointerException
		--TreeSet not can access null elements.
		Set<Integer> ts = new TreeSet<>();
		ts.add(null); 
			
		Comparable is a raw type. References to generic type Comparable<T> should be parameterized
		--Infer Generic Type Arguments...
		--Add @SuppressWarnings 'rawtypes' to 'Class.getName'
		
		T cannot be resolved to a type
		--Add type parameter 'T' to 'Class.getName': class与type经常傻傻分不清
		----泛型类(型)参数、类(型)参数、T类(型)、泛型类型(generic type)or泛型类==泛型
		
		The type Class must implement the inherited abstract method Comparable.compareTo(Object)
		The type Class<T> must implement the inherited abstract method Comparable<T>.compareTo(T)
		The type Class must implement the inherited abstract method Comparable<Class>.compareTo(Class)
		--Add unimplemented methods
		--Make type 'Class.getName()' abstract: 经常,calss与type,傻傻分不清
		
		This method must return a result of type int
		--Add return statement
		--Change return type to 'void': class与type常分不清,此处又译为类型
		
		java.lang.Comparable<? super T>
		--The type Class cannot extend or implement Comparable<? super T>. A supertype may not specify any wildcard
		
		Set<Class> hs = new HashSet<>();
		Class is a raw type. References to generic type SelfDefine<T> should be parameterized
		--Infer Generic Type Arguments...
		--Add @SuppressWarnings 'rawtypes' to field/method
		
		The blank final field STRING may not have been initialized
		--Initialize final field 'FINAL' in constructor
		--Initialize final field 'FINAL' to new constructor parameter
		
		private Type Class.field;
		The value of the field Class.field is not used
		--Remove Class.field, keep assignments with side effects(副作用)
		删除字段,保留具有副作用的赋值
		----Fix 2 problems of same category in file
		--Create setter and getter for this field
		--Add @SuppressWarnings 'unused' to field/method
		
		SelfDefine this = new SelfDefine(),先于,SelfDefine sd = new SelfDefine(),
		而存在,而加载。*/
	//Auto generated constructor stub
	public SelfDefine(int i, String s) {
		this.integer = i;
		this.string = s;
//		System.out.println("SelfDefine创建成功,才有该构造方法,而该类创建成功的前提,是compareTo()方法创建成功");
	}
	
	private final String STRING = "JavaArchitect";
	private int integer;
	private String string;
	
	@Override
	public int compareTo(SelfDefine sd) {
		//Standard syntax: this.field
//		if(this. == o.) {
//			return 0;
//		}else if(this.t > o) {
//			return 1;
//		}else if(this.t < o) {
//			return -1;
//		}else {
//			
//		}
		/* SelfDefine this = new SelfDefine(),
		 * 先于,
		 * SelfDefine sd = new SelfDefine();
		 * 而存在,而加载。 */
//		System.out.println("compareTo()方法先于该类而存在、执行");
		
		if(this.integer > sd.integer)
			return 1;
		else if(integer < sd.integer)
			return -1;
		else
			return this.string.compareTo(sd.string);
	}
	
	/** Suggest all subclasses of object override/cover this method
	    It is suggested that all subclasses override this method */
	@Override
	public String toString() {
		//Return void string 
		//Standard grammar: this.field
		/*Escape character of newline is abbreviated to "\n"--
		The escape character for the new line is "\n"
		The escape character abbreviation for the new line is \n */
		return "Profession: " + this.STRING + ", Name: " + this.string + ", Salary: " + integer + "\n";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + integer;
		result = prime * result + ((string == null) ? 0 : string.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;
		SelfDefine other = (SelfDefine) obj;
		if (integer != other.integer)
			return false;
		if (string == null) {
			if (other.string != null)
				return false;
		} else if (!string.equals(other.string))
			return false;
		return true;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值