๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/๐Ÿ’œํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

(JAVA) 99ํด๋Ÿฝ ์ฝ”ํ…Œ ์Šคํ„ฐ๋”” 1์ผ์ฐจ TIL [Stream().mapToInt์™€ toArray์˜ ์ฐจ์ด] / ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค [๋ฒ ์ŠคํŠธ์•จ๋ฒ”]

by eyes from es 2024. 5. 20.
728x90
๋ฐ˜์‘ํ˜•

#HashMap #ํด๋ž˜์Šค #Comparable #Stream #mapToInt

 

toArray() vs mapToInt(Integer::inValue).toArray() ์ฐจ์ด

 

toArray(): ๋ฐ˜ํ™˜๊ฐ’์ด Object๋ผ์„œ ํ˜•๋ณ€ํ™˜์‹œ int[]์ด๋Ÿฐ ์‹์œผ๋กœ ํŠน์ • ํ˜•๋ณ€ํ™˜์ด ๋ณด์žฅX

import java.util.*;

class Solution {
    
	static class playListManager implements Comparable<playListManager> {
        int count;
        List<playOfSong> playList;

        public playListManager() {
            this.count = 0;
            this.playList = new ArrayList<>();
        }

        public playListManager(String genre, playOfSong pl) {
            this();
            addPlayCount(pl.index, pl.playCount);
        }

        public void addPlayCount(int index, int playCount) {
            this.count += playCount;
            this.playList.add(new playOfSong(index, playCount));
        }

        public void sortPlayList() {
            Collections.sort(this.playList);
        }

        @Override
        public int compareTo(playListManager o) {
            return o.count - this.count;
        }
    }

    static class playOfSong implements Comparable<playOfSong> {
        int index;
        int playCount;

        public playOfSong(int index, int playCount) {
            this.index = index;
            this.playCount = playCount;
        }

        @Override
        public int compareTo(playOfSong o) {
            if (this.playCount != o.playCount) {
                return o.playCount - this.playCount; 
            } else {
                return this.index - o.index; 
            }
        }
    }
	
    public int[] solution(String[] genres, int[] plays) {        
        HashMap<String, playListManager> play = new HashMap<>();
    
        for (int i = 0; i < genres.length; i++) {
        	
        	String genre = genres[i];
        	
        	if(!play.containsKey(genre)) {
        		play.put(genre, new playListManager());
        	}
        	
        	play.get(genre).addPlayCount(i, plays[i]);
		}
        
        
        List<Integer> result = new ArrayList<>();
        List<playListManager> genreList = new ArrayList<>(play.values());
        Collections.sort(genreList);
        
        for(playListManager genre : genreList) {
        	genre.sortPlayList();
        	
			
            for(int i = 0; i<genre.playList.size(); i++){
                if(i==2) break;
                result.add(genre.playList.get(i).index);
            }
        	
        }

        return result.stream().mapToInt(Integer::intValue).toArray();
    }
}
 

ํšŒ๊ณ  ๐Ÿค”

  1. ๋‚˜๋Š” class๋ฅผ 2๊ฐœ๋ฅผ ํ–ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ๊นŒ
Class Music implements Comparable<Music>{
  String genre;
  int index;
  int play;
}

์ด๋Ÿฐ ์‹์œผ๋กœ ํ•˜๋‚˜์˜ class๋ฅผ ๋งŒ๋“ค๊ณ  ์ •๋ ฌ ์กฐ๊ฑด์€ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ๋„ฃ์—ˆ๋‹ค.

โ€‹

1. Map<String, Integer> ์žฅ๋ฅด, totalPlay

๊ทธ๋ฆฌ๊ณ  for๋ฌธ์„ ๋Œ๋ฉด์„œ ํ•ด๋‹น ์žฅ๋ฅด์˜ playํšŸ์ˆ˜๋ฅผ Map์— ์ €์žฅ

โ€‹

-> keySet์œผ๋กœ ๊ฐ€์ ธ์™€์„œ totalPlay ๊ฐฏ์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

โ€‹

2. List์— add๋กœ Music(genres[i], index, plays[i])

1. Map์— contain ์—ฌ๋ถ€ ํ™•์ธ

์žˆ์œผ๋ฉด List์— ์ถ”๊ฐ€

2. ์—†์œผ๋ฉด ์ƒˆ๋กœ์šด List์— ์ถ”๊ฐ€

โ€‹

3. ์ •๋ ฌ

โ€‹

4. ์ตœ๋Œ€ 2๊ฐœ์”ฉ

728x90
๋ฐ˜์‘ํ˜•