How to optimize searching through json array from file

I’m trying to look through and find the most played songs from my spotify listening history download, and my code below uses 99% cpu for 20 seconds and then fails with “Exception in thread “main” java.lang.OutOfMemoryError: Java heap space”.

I wanted it to give me the song i most listened to based on total ms listened. I used arraylists and made a class named “Song” with its own constructor to make it easier to contain all the info about a song I want from the json file. The json file structure and my 2 classes are below. I use eclipse 23-09 if it helps

Main Class:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class MainTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JSONParser jsonParser = new JSONParser();
        ArrayList<Song> songs = new ArrayList<>();
        Song mostPlayed = null;
        Song mostTimesPlayed = null;
        //ArrayList<Song> highlights = new ArrayList<>();
        long maxLong = 0;
        int maxPlays = 0;
        
        try {
            JSONArray jsonArray = (JSONArray) jsonParser.parse(new FileReader("C:\Users\Username\eclipse-workspace\csh\csh\history.json"));
            Iterator<Object> iterator = jsonArray.iterator();
            while (iterator.hasNext()) {
                JSONObject jsonObject = (JSONObject) iterator.next();
                String tempTrack = (String) jsonObject.get("master_metadata_track_name");
                String tempAlbum = (String) jsonObject.get("master_metadata_album_album_name");
                String tempArtist = (String) jsonObject.get("master_metadata_album_artist_name");
                long tempLong = (long) jsonObject.get("ms_played");
                if (songs.isEmpty() /*||  tempLong >=30000*/) {
                    songs.add(new Song(tempTrack, tempAlbum, tempArtist, tempLong));
                }
                else{
                    for (int i = 0; i < songs.size(); i++) {
                        if (( songs.get(i)).getTrackName() == tempTrack /*&& tempLong >=30000*/) {
                            (songs.get(i)).updateSong(tempLong);
                        }
                        else {
                            songs.add(new Song(tempTrack, tempAlbum, tempArtist, tempLong));
                        }
                    }
                }
                
                
            }
            for (int i = 0; i < songs.size(); i++) {
                if ((songs.get(i)).getTotalPlayedFor()> maxLong) {
                    mostPlayed = songs.get(i);
                }
                if ((songs.get(i)).getTotalPlayedFor()> maxPlays) {
                    mostTimesPlayed = songs.get(i);
                }
            }
            System.out.println("――――――――――― Highlights ―――――――――――");
            double maxTimeSec = mostPlayed.getTotalPlayedFor()/1000.0;
            double maxTimeMin = maxTimeSec/60.0;
            double maxTimeHr = maxTimeMin/60.0;
            System.out.println("Most listened to song by time: ");
            System.out.println("Song Name: " + mostPlayed.getTrackName());
            System.out.println("By: " + mostPlayed.getArtist());
            System.out.println("Playtime(ms): " + mostPlayed.getTotalPlayedFor());
            System.out.println("Playtime(s): " + maxTimeSec);
            System.out.println("Playtime(m): " + maxTimeMin);
            System.out.println("Playtime(hr): " + maxTimeHr);
            
        }
        
        catch (FileNotFoundException e) {
            e.printStackTrace();
            }
        catch (IOException e) {
            e.printStackTrace();
            }
        catch (ParseException e) {
            e.printStackTrace();
            }
        
    }

}

Song Class:

public class Song {
    
    private String trackName;
    private String album;
    private String artist;
    private int timesPlayed;
    private long totalPlayedFor;

    /**
     * @return the album
     */
    public String getAlbum() {
        return album;
    }
    public String getTrackName() {
        return trackName;
    }
    public long getTotalPlayedFor() {
        return totalPlayedFor;
    }
    public String getArtist() {
        return artist;
    }
    public int getTimesPlayed() {
        return timesPlayed;
    }
    public Song(String trackName, String album, String artist, long playedFor) {
        this.trackName = trackName;
        this.album = album;
        this.artist = artist;
        this.timesPlayed = 1;
        totalPlayedFor = playedFor;
    }
    public void updateSong(long playedFor) {
        timesPlayed = getTimesPlayed() + 1;
        totalPlayedFor = getTotalPlayedFor() + playedFor;
    }
    
}

Example json object from file:

{
    "ts": "2021-04-21T07:22:32Z",
    "username": "USERNAME",
    "platform": "Windows 10 (10.0.19042; x64)",
    "ms_played": 0,
    "conn_country": "COUNTRYCODE",
    "ip_addr_decrypted": "IPADDRESS",
    "user_agent_decrypted": "unknown",
    "master_metadata_track_name": "Little Dark Age",
    "master_metadata_album_artist_name": "MGMT",
    "master_metadata_album_album_name": "Little Dark Age",
    "spotify_track_uri": "spotify:track:2Y0iGXY6m6immVb2ktbseM",
    "episode_name": null,
    "episode_show_name": null,
    "spotify_episode_uri": null,
    "reason_start": "playbtn",
    "reason_end": "endplay",
    "shuffle": false,
    "skipped": null,
    "offline": false,
    "offline_timestamp": 1618989748702,
    "incognito_mode": false
}

any help would be appreciated!!

New contributor

gungurbuz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật