java-- frame set (three HashSet)

hashset characteristics

  • Disorderly
  • No index
  • No repeat
  • 1.HashSet principle
    • We use the Set collection are needed to remove duplicate elements, one by one if equals () comparing the number of low efficiency, hash algorithm improves the deduplication efficiency and reduce the use of equals () method at the time of storage
    • When HashSet call add () method to store the object, call the object's first hashCode () method to obtain a hash value, then find whether the object has the same hash value in the collection
      • If you do not object to the same hash value directly into the collection
      • If the objects have the same hash value, hash value, and on the same object one by one equals () comparison, the comparison result is stored on false, true not exist
  • 2. The custom object class is stored in the deduplication HashSet
    • Class must override hashCode () and equals () method
    • hashCode (): returns the same object property must be the same, different attributes of different possible return values ​​(efficiency)
    • equals (): returns the same properties true, different property returns false, return false when stored

(LinkedHashSet overview and use)

  • A: LinkedHashSet features

    • How can guarantee deposit on how to take

    • Write a program 10 acquires a random number from 1 to 20, it requires a random number can not be repeated. And the final output of a random number to the console.

       HashSet<Integer> hs = new HashSet<>();		//创建集合对象
      	Random r = new Random();					//创建随机数对象
      	
      while(hs.size() < 10) {
      	int num = r.nextInt(20) + 1;			//生成1到20的随机数
      	hs.add(num);
      }
      
      for (Integer integer : hs) {				//遍历集合
      	System.out.println(integer);			//打印每一个元素
      }
      

      Scanner used to read a line of input from the keyboard, wherein the removing is repeated characters, printed characters that different

  • aaaabbbcccddd

    Scanner sc = new Scanner(System.in);			//创建键盘录入对象
    	System.out.println("请输入一行字符串:");
    	String line = sc.nextLine();					//将键盘录入的字符串存储在line中
    	char[] arr = line.toCharArray();				//将字符串转换成字符数组
    	HashSet<Character> hs = new HashSet<>();		//创建HashSet集合对象
    	
    for(char c : arr) {								//遍历字符数组
    	hs.add(c);									//将字符数组中的字符添加到集合中
    }
    
    for (Character ch : hs) {						//遍历集合
    	System.out.println(ch);
    }
    
    • The set of repeating elements removed
public static void main(String[] args) {
			ArrayList<String> list = new ArrayList<>();
			list.add("a");
			list.add("a");
			list.add("a");
			list.add("b");
			list.add("b");
			list.add("b");
			list.add("b");
			list.add("c");
			list.add("c");
			list.add("c");
			list.add("c");
		System.out.println(list);
		System.out.println("去除重复后:");
		getSingle(list);
		System.out.println(list);
	}
	
	/*
	 * 将集合中的重复元素去掉
	 * 1,void
	 * 2,List<String> list
	 */
	
	public static void getSingle(List<String> list) {
		LinkedHashSet<String> lhs = new LinkedHashSet<>();
		lhs.addAll(list);									//将list集合中的所有元素添加到lhs
		list.clear();										//清空原集合
		list.addAll(lhs);									//将去除重复的元素添回到list中
	}

TreeSet

  • 1. Features
  • TreeSet is used to sort, you can specify a sequence is sorted in the order specified after the object is stored
  • 2. use
    • a. natural ordering (the Comparable)
      • add TreeSet class () method will deposit objects to lift type Comparable
      • Call the object's compareTo () method compares objects and collections
      • Storing the results of the compareTo () method returns
    • b. comparator sequence (Comparator)
      • Create a TreeSet when you can develop a Comparator
      • If the subclass object Comparator passed, then the sort order will TreeSet comparators
      • Internal add () method is called automatically compare the Comparator Interface () method to sort
      • Calling object is the first argument to compare the method, the object set is the second argument of the compare method
    • Difference between c. Two ways
      • TreeSet constructor pass nothing, the default order of Comparable class (without being given on a ClassCastException)
      • TreeSet If the incoming Comparator, on priority in accordance with Comparator

Guess you like

Origin blog.csdn.net/lgy54321/article/details/94412293