Java Set HashSet LinkedHashSet

		
		Set<String> set = new HashSet<String>();
		set.add("11");
		set.add("2");
		set.add("1");
		
		//output result: [11, 1, 2]
		//The order of elements has nothing to do with the order of placement, and is related to the HashCode of the element.
		System.out.println(set);
		
		Set<String> set2 = new LinkedHashSet<String>();
		set2.add("11");
		set2.add("2");
		set2.add("1");

		//output result: [11, 2, 1]
		// Consistent with the order in which they were placed.
		System.out.println(set2);
		
		Set<String> set3 = new LinkedHashSet<String>();
		set3.add("a");
		set3.add("a");
		
		//Output result: [a]
		//Whether it is HashSet or LinkedHashSet, no duplicate values ​​will be put into it.
		System.out.println(set3);
		
	

 

 

 

 

 

 

 

 

 

 

Guess you like

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