JavaScript로 데이터를 시각적으로 표현하는 법: 다양한 출력 방법 마스터하기

JavaScript에서 데이터 출력하기 JavaScript를 사용하면 웹 페이지에 동적으로 내용을 추가하거나 변경할 수 있습니다. 이 글에서는 다양한 방법으로 사용자에게 정보를 제공하는 방법을 소개합니다. 1. document.write() 사용하기 document.write() 메서드는 HTML 페이지가 로드될 때 직접 문서에 문자열을 쓰는 데 사용됩니다. 예제: document.write("Hello, World!"); 2. innerHTML 사용하기 innerHTML 속성을 사용하면 특정 HTML 요소의 내부 HTML을 가져오거나 설정할 수 있습니다. 예제: document.getElementById("myElement").innerHTML = "안녕하세요!"; 3. DOM 조작하기 HTML 문서의..

30. Map에서 요소의 평균 구하기

// 값의 평균 구하기 double average = map.values().stream().mapToInt(Integer::intValue).average().orElse(Double.NaN);

29. Map에서 요소의 총합 구하기

// 값의 총합 구하기 int sum = map.values().stream().mapToInt(Integer::intValue).sum();

28. Map에서 키/값의 빈도수 구하기

// 키의 빈도수 구하기 Map keyFrequencyMap = map.keySet().stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); // 값의 빈도수 구하기 Map valueFrequencyMap = map.values().stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

27. Map에서 조건에 맞는 요소 찾기

// 값이 2 이상인 첫 번째 요소 찾기 Optional firstMatch = map.entrySet().stream() .filter(entry -> entry.getValue() >= 2) .findFirst(); // 값이 2 이상인 모든 요소 찾기 List allMatches = map.entrySet().stream() .filter(entry -> entry.getValue() >= 2) .collect(Collectors.toList());

26. Map의 요소를 Stream으로 변환하기

// EntrySet을 Stream으로 변환하기 Stream entryStream = map.entrySet().stream(); // KeySet을 Stream으로 변환하기 Stream keyStream = map.keySet().stream(); // ValueSet을 Stream으로 변환하기 Stream valueStream = map.values().stream();

25. Map의 요소를 Set으로 변환하기

// 키와 값 모두 포함한 EntrySet을 Set으로 변환하기 Set entrySet = map.entrySet(); // 키만 포함한 KeySet을 Set으로 변환하기 Set keySet = map.keySet(); // 값만 포함한 ValueSet을 Set으로 변환하기 Set valueSet = new HashSet(map.values());

24. Map에서 키/값 모두 이용하여 필터링하기

// 키/값 모두 이용하여 값이 2 이상인 요소만 필터링 Map filteredMap = map.entrySet().stream() .filter(entry -> entry.getKey().startsWith("a") && entry.getValue() >= 2) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));