I have a set of elements stored in HashMap. So, I have to compare the values and if the values retrieved are bigger than a certain value, it should be grouped into Group-n (where n = represent the n-th group).
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class GroupTag{
public static void main(String[] args)
{
Map<String, Integer> myMap = new HashMap<String, Integer>();
myMap.put("0-1", 33);
myMap.put("0-2", 29);
myMap.put("0-3", 14);
myMap.put("0-4", 8);
myMap.put("1-2", 41);
myMap.put("1-3", 15);
myMap.put("1-4", 17);
myMap.put("2-3", 1);
myMap.put("2-4", 16);
myMap.put("3-4", 18);
for(int i = 0; i < 5; i++)
{
for(int j = i+1; j < 4; j++)
{
String testLine = i+"-"+j;
int itemA = myMap.get(testLine);
boolean greaterThanAll = true;
for(int k = j+1; k < 5; k++)
{
String newLine = j+"-"+k;
int itemB = myMap.get(newLine);
if(itemA <= itemB)
{
//Condition: e.g IF and ONLY IF all myMap.get(0-1)>than myMap.get(1-2),
//myMap.get(1-3),myMap.get(1-n)
//THEN trigger an event to group ALL of myMap.get(1-n) to myMap.get(0-1)
//THEN remove all the values that satisfied the condition from the HashMap list
greaterThanAll = false;
break;
}
}
if (greaterThanAll)
{
for(int m = j+1; m < 5; m++)
{
String removeLine = j+"-"+m;
//Group myMap.get(removeLine) to myMap.get(testLine)
//myMap.remove(removeLine);
System.out.println("Index " + removeLine + " : " + myMap.get(removeLine));
}
//myMap.remove(testLine);
System.out.println("Main Index " + testLine + " : " + myMap.get(testLine));
}
}
}
}
}
Example of how the element are compared:
IF myMap.get("0-1")>myMap.get("1-n"): Grouped to Group 0 and REMOVE both values from list
IF myMap.get("0-2")>myMap.get("2-n"): Grouped to Group 1 and REMOVE both values from list
IF myMap.get("0-3")>myMap.get("3-n"): Grouped to Group 2 and REMOVE both values from list
THEN the loop goes on to compare myMap.get("1-2")>myMap.get("2-n") and so on..
Desired outcome:
Retrieve number of Groups: 2
Retrieve size of Group 0: 3
Retrieve elements in Group 1: [1, 16]
Basically, I just need a way to group/store a set of elements or data together?
EDIT: I posted the conditions. I thought it would be easier leaving the conditions out as I just wanted to group some elements together.