Untitled
unknown
java
a year ago
3.5 kB
10
Indexable
package Atlassian;
import java.util.*;
public class FileUtil {
private int totalSize = 0;
public static void main(String[] args) {
FileUtil fileUtil = new FileUtil();
// Sample input data: file sizes and collection names
String[][] files = {
{"100", ""}, {"200", "collection1"},
{"200", "collection2"}, {"300", "collection3"}, {"10", ""}
};
int n = 3;
// Process files and generate report
fileUtil.processFiles(files, n);
fileUtil.printTotalSize();
}
// Processes files to calculate total size and collection sizes
void processFiles(String[][] files, int n) {
Map<String, Integer> collectionSizes = calculateCollectionSizes(files);
PriorityQueue<Map.Entry<String, Integer>> sortedCollections = sortCollectionsBySize(collectionSizes);
printTopNCollections(sortedCollections, n);
}
// Calculates the sizes of collections from the file data
Map<String, Integer> calculateCollectionSizes(String[][] files) {
Map<String, Integer> collectionSizes = new HashMap<>();
for (String[] file : files) {
int size = Integer.parseInt(file[0]); // Parse file size
totalSize += size; // Update total size
// If collection name is present, update collection size
if (file.length > 1 && !file[1].isEmpty()) {
String collection = file[1];
collectionSizes.put(collection, collectionSizes.getOrDefault(collection, 0) + size);
}
}
return collectionSizes;
}
// Sorts collections by size in descending order
PriorityQueue<Map.Entry<String, Integer>> sortCollectionsBySize(Map<String, Integer> collectionSizes) {
PriorityQueue<Map.Entry<String, Integer>> priorityQueue = new PriorityQueue<>(
Comparator.comparingInt(entry -> -entry.getValue())
);
priorityQueue.addAll(collectionSizes.entrySet());
return priorityQueue;
}
// Prints the top N collections by size
void printTopNCollections(PriorityQueue<Map.Entry<String, Integer>> sortedCollections, int n) {
for (int i = 0; i < n && !sortedCollections.isEmpty(); i++) {
Map.Entry<String, Integer> entry = sortedCollections.poll();
System.out.println((i + 1) + ": " + entry.getKey() + " -> " + entry.getValue());
}
}
// Prints the total size of all files
void printTotalSize() {
System.out.println("Total Size: " + getTotalSize());
}
// Returns the total size of all files
int getTotalSize() {
return this.totalSize;
}
}
/*
* Imagine we have a system that stores files, and these files can be grouped
* into collections. We are interested in knowing where our resources are being
* taken up. For this system we would like to generate a report that lists: The
* total size of all files stored; and The top N collections (by file size)
* where N can be a user-defined value An example input into your report
* generator might look like:
*
* file1.txt (size: 100) file2.txt (size: 200) in collection "collection1"
* file3.txt (size: 200) in collection "collection1" file4.txt (size: 300) in
* collection "collection2" file2.txt (size: 200) in collection "collection1"
* file5.txt (size: 10)
*
* Inputs: all the file names along with their size and collections. Output: Top
* N collections sorted by file size total size of all files
*
*
*
*/Editor is loading...
Leave a Comment