0

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.

4

2 回答 2

2

首先,为您的要求构建预期的字符串键是必需的吗?迭代地图并查看值是否大于数字不是更容易吗?

也许LambdaJ是您正在寻找的东西(直到 Java 获得本机 Lambda 表达式)。

于 2012-09-16T07:49:45.343 回答
0

参考: http ://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html#subSet (E, boolean, E, boolean)

您可以使用 TreeSet 中的子集()操作。

public NavigableSet<E> subSet(E fromElement,
                                      boolean fromInclusive,
                                      E toElement,
                                      boolean toInclusive)

    OR

public SortedSet<E> subSet(E fromElement,
                               E toElement)
于 2012-09-16T08:08:10.103 回答