Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Want to see what's currently in public preview? We've pulled it all together in one place. Check it out here
cancel
Showing results for 
Search instead for 
Did you mean: 
JosephMansour
Partner - Contributor
Partner - Contributor

Using java.util.List and java.util.HashSet In Tjavaflex Component

// Begin Code

java.util.Map<String, java.util.Map<String, java.util.List<String>>> idYearlyValues = new java.util.HashMap<>();

java.util.Map<String, java.util.Map<String, java.util.List<String>>> assessmentHistoryMap = new java.util.HashMap<>();

java.util.Set<String> uniqueIDs = new java.util.HashSet<>();

 

 

// Main Code

String assessmentId = out2.ASSESSMENT_ID; // Unique ID

String date = out2.Month; // Month as a string

String value = out2.CA_AMT; // The value to be stored

String year = out2.Year; // Year as a string

String assessmentHistoryId = out2.ASSESSMENT_HISTORY_ID; // Get the ASSESSMENT_HISTORY_ID

 

// Add the ID to the set of unique IDs

uniqueIDs.add(assessmentId);

 

// Initialize the map for the ID if it doesn't exist

idYearlyValues.putIfAbsent(assessmentId, new java.util.HashMap<>());

assessmentHistoryMap.putIfAbsent(assessmentId, new java.util.HashMap<>()); // Initialize history map for the ID

 

// Initialize the list for the year if it doesn't exist

idYearlyValues.get(assessmentId).putIfAbsent(year, new java.util.ArrayList<>(java.util.Collections.nCopies(12, "")));

assessmentHistoryMap.get(assessmentId).putIfAbsent(year, new java.util.ArrayList<>(java.util.Collections.nCopies(12, ""))); // Initialize history for the year

 

// Get the month from the Date

int month = Integer.parseInt(date) - 1; // Convert month to 0-11 index

 

// Get the list of monthly values for this ID and year

java.util.List<String> monthlyValues = idYearlyValues.get(assessmentId).get(year);

java.util.List<String> historyValues = assessmentHistoryMap.get(assessmentId).get(year); // Get history values

 

// Set the current month value

monthlyValues.set(month, value);

 

// Store ASSESSMENT_HISTORY_ID for the current month

if (assessmentHistoryId == null || assessmentHistoryId.isEmpty()) {

// If the current month history ID is empty, use the previous month's ID

if (month > 0) { // Ensure there is a previous month

String previousId = historyValues.get(month - 1);

if (previousId != null && !previousId.isEmpty()) {

// Append the current month number (1-based index)

assessmentHistoryId = previousId + "-" + (month + 1);

} else {

// If the previous month ID is also empty, set it to a default value

assessmentHistoryId = "DefaultID-" + (month + 1); // Adjust as needed

}

} else {

// For the first month, if there's no previous month, set a default ID

assessmentHistoryId = "DefaultID-1"; // Or handle this case as needed

}

}

historyValues.set(month, assessmentHistoryId); // Store the ASSESSMENT_HISTORY_ID for the current month

 

// Filling in null months with last available month's value for history IDs

String lastHistoryId = null;

for (int i = 0; i < historyValues.size(); i++) {

if (!historyValues.get(i).isEmpty()) {

lastHistoryId = historyValues.get(i); // Update last known history ID

} else if (lastHistoryId != null) {

// Fill with last known history ID plus current month ID

historyValues.set(i, lastHistoryId + "-" + (i + 1)); // i + 1 to get the 1-based month index

}

}

 

// Filling in null months with last available month's value for values

String lastValue = null;

for (int i = 0; i < monthlyValues.size(); i++) {

if (!monthlyValues.get(i).isEmpty()) {

lastValue = monthlyValues.get(i); // Update last known value

} else if (lastValue != null) {

monthlyValues.set(i, lastValue); // Fill with last known value

}

}

 

// End Code

System.out.println("ID\tHistory ID\tYear\tMonth\tValue"); // Table Header

System.out.println("-------------------------------------------------"); // Separator

 

for (java.util.Map.Entry<String, java.util.Map<String, java.util.List<String>>> entry : idYearlyValues.entrySet()) {

String id = entry.getKey();

java.util.Map<String, List<String>> yearValues = entry.getValue();

 

for (java.util.Map.Entry<String, java.util.List<String>> yearEntry : yearValues.entrySet()) {

String yearOutput = yearEntry.getKey();

java.util.List<String> values = yearEntry.getValue();

java.util.List<String> historyValues = assessmentHistoryMap.get(id).get(yearOutput); // Get corresponding history IDs

 

for (int month = 0; month < values.size(); month++) {

// Get the month value and history ID for the current month

String monthValue = values.get(month);

String monthHistoryId = historyValues.get(month);

 

// Print the ID, History ID, Year, Month, and Value in the specified order

System.out.printf("%s\t%s\t%s\t%d\t%s%n", id, monthHistoryId, yearOutput, month + 1, monthValue);

}

}

}

The provided code is designed to process and transform data within a Java application, specifically to aggregate assessment values and their corresponding history IDs on a monthly basis. However, when executing this code in the TJavaFlex component, the expected output—containing all the transformed data ready for insertion into a database—is not being achieved. Instead, it is only displaying the initial values prior to any transformations.ScreenshotAXA.png

 

 

 

Labels (3)
0 Replies