HashMap에 저장된 요소 세트가 있습니다. 그래서 값을 비교해야하고 검색된 값이 특정 값보다 큰 경우 Group-n (n은 n 번째 그룹을 나타냄)으로 그룹화해야합니다.HashMap의 요소를 특정 그룹으로 그룹화하기
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]
기본적으로 요소 또는 데이터 집합을 그룹화/저장하는 방법이 필요합니다.
편집 : 조건을 게시했습니다. 나는 단지 일부 요소들을 그룹으로 묶기 원했기 때문에 조건을 그대로 두는 것이 더 쉬울 것이라고 생각했다.
요소를 그룹화하는 방법을 잘 모르겠습니다. 어쩌면 당신이 그것을 분명히 할 수 있습니다. –
아마도 나는 여기에있는 것들을 복잡하게 만들고 있습니다. 간단히 말해 보도록하겠습니다. HashMap에 저장된 요소 목록이 있습니다. 필요한 것은 특정 조건을 기반으로 특정 요소를 그룹화하는 것입니다. 예 : 요소 0, 5, 11, 12는 그룹 0으로 그룹화해야합니다. 요소 2, 4, 8, 14는 그룹 -1로 이동합니다. 요소 3, 6, 13, 15는 그룹 2로 이동하고 요소 1, 7, 9, 14는 그룹 3으로 이동합니다. – Cryssie
그래,하지만 조건에 대한 예제가있는 것이 좋을 것이다. 당신의 본보기는 그 상태가 정말로 무엇인지 말해주지 않습니다. –