프로그래밍언어/JAVA

<JAVA>Collection Set,Map

컴퓨터과학 2020. 12. 7. 20:44

 

stackoverflow.com/questions/5689517/java-hashset-vs-hashmap

 

Java HashSet vs HashMap

I understand that HashSet is based on HashMap implementation but is used when you need unique set of elements. So why in the next code when putting same objects into the map and set we have size of...

stackoverflow.com

  • in a Map you store key-value pairs
  • in a Set you store only the keys

set과 map의 차이 

먼저 set과 map의 차이는 

key값을 연결해서 사용하는게 Map이고

Set은 오직 key값만 set하는것 입니다. 

 

stackoverflow.com/questions/1463284/hashset-vs-treeset

 

Hashset vs Treeset

I've always loved trees, that nice O(n*log(n)) and the tidiness of them. However, every software engineer I've ever known has asked me pointedly why I would use a TreeSet. From a CS background, I d...

stackoverflow.com

hash set과 tree set의 차이 

베스트 글을 빌리자면 

hash set은 tree set 보다 빠릅니다(추가,삭제와 같은것들)

그러나 tree set 처럼 정렬되어 있지 않다고 합니다. 

  Add Contains Next
Hash Set O(1) O(1) O(h/n)
Tree Set O(log n) O(log n) O(log n)

Hash Set: 

package Test01;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.HashMap;
import java.util.TreeMap;

public class Main {

  
	public static void main(String [] args)  {
		System.out.println("-----------------Set----------------");
		System.out.println("-----------------Hash Set----------------");
				HashSet<String> hashSet=new HashSet<>();
				
				hashSet.add("뚜비");
				hashSet.add("나나");
				hashSet.add("뽀");
				hashSet.add("보라돌이");
				hashSet.add("홍길동");
				hashSet.add("김유신");
				hashSet.add("이순신");
				
				
				System.out.println(hashSet);
				
				
		
	}
	
}

 

 

 

Tree Set:

 

package Test01;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.HashMap;
import java.util.TreeMap;

public class Main {

  
	public static void main(String [] args)  {
		System.out.println("-----------------Set----------------");
		
				//Tree Set
				System.out.println("-----------------Tree Set----------------");
		         TreeSet<String> treeSet=new TreeSet<>();
				
		         treeSet.add("뚜비");
		         treeSet.add("나나");
		         treeSet.add("뽀");
		         treeSet.add("보라돌이");
		         treeSet.add("홍길동");
		         treeSet.add("김유신");
		         treeSet.add("이순신");
				
				
				System.out.println(treeSet);
				
				
		
	}
	
}

 

 

 

 

stackoverflow.com/questions/2444359/what-is-the-difference-between-a-hashmap-and-a-treemap/2444370

 

What is the difference between a HashMap and a TreeMap?

I started learning Java. When would I use a HashMap over a TreeMap?

stackoverflow.com

Hash Map과 Tree Map의 차이 

Tree Map  키값 기준으로 정렬된 map입니다. 

반면 Hash Map 키값으로 정렬해주는걸 보장하지 않습니다.

  Get ContainsKey Next
HashMap O(1) O(1) O(1)
TreeMap O(log n) O(log n) O(log n)

 

Hash Map:

 

package Test01;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.HashMap;
import java.util.TreeMap;

public class Main {

  
	public static void main(String [] args)  {
	
				System.out.println("-----------------Map----------------");
				//Hash Map
				
				
				System.out.println("-----------------Hash Map----------------");
		         HashMap<Integer,String> hashMap=new HashMap<>();
				
		         hashMap.put(1,"뚜비");
		         hashMap.put(2,"나나");
		         hashMap.put(3,"뽀");
		         hashMap.put(4,"보라돌이");
		         hashMap.put(5,"홍길동");
		         hashMap.put(6,"김유신");
		         hashMap.put(7,"이순신");
				
				
				System.out.println(hashMap);
			
		
	}
	
}

 

 

 

Tree Map:

package Test01;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.HashMap;
import java.util.TreeMap;

public class Main {

  
	public static void main(String [] args)  {
	
				
				System.out.println("-----------------Map----------------");
			
				
				

				System.out.println("-----------------Tree Map----------------");
				
				TreeMap<Integer,String> treeMap=new TreeMap<>();
				
				treeMap.put(1,"뚜비");
				treeMap.put(2,"나나");
				treeMap.put(3,"뽀");
				treeMap.put(4,"보라돌이");
				treeMap.put(5,"홍길동");
				treeMap.put(6,"김유신");
				treeMap.put(7,"이순신");
				System.out.println(treeMap);
		
	}
	
}

 

 

infotechgems.blogspot.com/2011/11/java-collections-performance-time.html

 

Java Collections – Performance (Time Complexity)

Many developers I came across in my career as a software developer are only familiar with the most basic data structures, typically, Array,...

infotechgems.blogspot.com