当前位置:网站首页>Hash table, generic

Hash table, generic

2022-06-25 16:19:00 weixin_ forty-eight million six hundred and forty-four thousand

  1.  Set
    1.  HashSet Use
    2. public static void main(String[] args) {
      		HashSet set = new HashSet();
      		set.add(1);
      		set.add("asd");
      		set.remove("asd");
      		System.out.println(set.size());
      		set.isEmpty();
      		for (Object object : set) {
      
      		}
      	}

  1.   Hash table
    1.   summary

    1.  HashSet

The above two screenshots can be seen , When we use HashSet When , In fact, it is equal to using again HashMap

When adding data , Even though it's called HashSet Of add Method , But the essence is to call map Of put Method

Ps : stay map in ,put Is an add operation

and map in What needs to be saved is k and v The mapping relationship , So in set There is a variable in that holds value Value

So let's go on set When adding , It's just the operation map Medium key,value We no longer care about

  1.  Map

    1.  HashMap
 //  establish map

                  HashMap map = new HashMap();

                  //  add to  K-V

                  map.put("A", "one");

                  map.put("B", "two");

                  map.put("C", "three");

                  map.put(65,100);

                  map.put('A', "2222");

                  // key repeat , Don't add ,value Replace 

                  map.put("A", "2222");

                  //  Support K and V all null, But it doesn't make sense 

                  map.put(null,null);

                  //  Number 

                  System.out.println(map.size());

                  // get :  according to K  obtain  V Value 

                  System.out.println(map.get("A"));

                  //  To determine whether or not to include a key

                  System.out.println(map.containsKey(65));

                  //  To determine whether or not to include a value

                  System.out.println(map.containsValue("one"));

                  //  according to key Delete   The mapping relationship , Return the corresponding value value 

                  map.remove(65);

                  //  Get all value, And put it in the collection to return 

                  Collection values = map.values();

                  for (Object object : values) {

                          System.out.println(object);

                  }

                  System.out.println("======");

                  // keySet :  Get all key, Package to set Object and return 

                  Set keys = map.keySet();

                  for (Object object : keys) {

                          System.out.println(object +":"+map.get(object));

                  }

                  //  hold map Convert to set

                  // Entry Class , Save the K and V Two variables , hold map Medium k and v Convert to entry Class 

                  //  So we just need to save entry object , It's like saving k and v

                  Set set  = map.entrySet();

                  for (Object object : set) {

                          // C=three

                          System.out.println(object);

                          //  Convert to entry type 

                          Entry entry = (Entry) object;

                          //  obtain k and v

                          System.out.println(entry.getKey()+" : "+entry.getValue());

                  }
    1.      TreeMap

  1.   Generic
    1.   summary

    1.   Usage mode
public static void main(String[] args) {
		//  Add custom type , also  Product in   Realized Comparable Interface 
		TreeMap map = new TreeMap();
		map.put(new Product(" Apple ", 5.3), 10);
		map.put(new Product(" Banana ", 5.6), 10);
		System.out.println(map.size());

		//  Add an existing type , And this type implements Comparable Interface ,Integer Default ascending order 
		//  If   The type does not implement Comparable Interface , perhaps   We want to be in descending order , You need to use Comparator
		// map = new TreeMap();
		map = new TreeMap(new Comparator() {
			@Override
			public int compare(Object o1, Object o2) {
				Integer i1 = (Integer) o1;
				Integer i2 = (Integer) o2;
				return i2 - i1;
			}
		});
		map.put(11, "a");
		map.put(2, "a");
		map.put(23, "a");
		map.put(3, "a");
		System.out.println(map.size());
		System.out.println(map);
	}
}

class Product implements Comparable {
	private String name;
	private double price;

	public Product(String name, double price) {
		super();
		this.name = name;
		this.price = price;
	}

	@Override
	public int compareTo(Object o) {
		if (o instanceof Product) {
			Product p = (Product) o;
			if (this.price > p.price) {
				return 1;
			} else if (this.price < p.price) {
				return -1;
			}
		}
		return 0;
	}

}

    1.   Be careful

    1.   Custom generics

  1.   topic
    public static void main(String[] args) {
    		//  Generics are not used 
    		List list = new ArrayList();
    		//  Anything can be put 
    		list.add(new A());
    		list.add(1);
    		//  When I pick it up , Get is Object
    		for (Object object : list) {
    			//  Use requires judgment and downward transformation 
    			if (object instanceof A) {
    				A a = (A) object;
    				a.m1();
    			}
    		}
    		
    		//  Use generics   Regulations   In this collection   Save only A type 
    		List<A> list2 = new ArrayList<A>();
    		list2.add(new A());
    		//  Add other   Report errors , And it is a compilation error 
    		// list2.add(2);
    		
    		//  When you use it , What you get is A type , There will be no type conversion exception 
    		//  Because as long as it can run , There must be A
    		for (A a : list2) {
    			a.m1();
    		}
    		
    		//  Generic   Cannot write basic type , Only reference types can be written 
    		//  If you want to save basic types , You need to write the corresponding packing class type 
    		// List<int> list3 = new ArrayList<int>();
    		List<Integer> list4 = new ArrayList<Integer>();
    		
    		//  If   Need to save a variety of data , You don't need to use generics 
    		//  But if you need to save the same type of data , Be sure to use generics , It's easy to operate 
    		
    		// set and map Use generics 
    		Set<String> set = new HashSet<String>();
    		Map<String, Integer> map = new HashMap<String, Integer>();
    	}
    }
    class A {
    	public void m1(){
    		System.out.println("m1 Yes ");
    	}
    }

原网站

版权声明
本文为[weixin_ forty-eight million six hundred and forty-four thousand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190534174140.html