java custom hashSet

 

 

set: The data in the set cannot be repeated,

 

Features: Use HashMap<Added object, Object> Use the key of the map to store the added data, so as to achieve the purpose of not repeating the data storage, and the value is a new object object.

 

script:

 

public class SxtHashSet {

	HashMap map;
	private static final Object PRESENT = new Object();

	public SxtHashSet(){
		map = new HashMap();
	}
	
	public int size(){
		return map.size();
	}
	
	public void add(Object o){
		map.put(o, PRESENT); //The non-repeatability of set is to use the non-repeatability of the key object in the map!
	}
	
	public static void main(String[] args) {
		SxtHashSet s = new SxtHashSet();
		s.add("aaa");
		s.add(new String("aaa"));
		System.out.println(s.size());
	}

}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326614119&siteId=291194637