Java 分组排序
我正在努力用Java解决这个问题 以下是所需的输入和输出:Java 分组排序,java,sorting,object,Java,Sorting,Object,我正在努力用Java解决这个问题 以下是所需的输入和输出: Input 1,9 1,12 1,7 3,3 2,4 3,2 2,2 output 1 -> 7,9,12 2 -> 2,4 3 -> 2,3 我只需使用Comparable和compareTo 1,7, 1,9, 1,12, 2,2, 2,4, 3,2, 3,3 但这不是我想要的答案。有人能帮我或给我一些建议吗? 这是我实现的代码,但这不是我想要的 使用地图: Map<Integer, List<In
Input
1,9
1,12
1,7
3,3
2,4
3,2
2,2
output
1 -> 7,9,12
2 -> 2,4
3 -> 2,3
我只需使用Comparable
和compareTo
1,7,
1,9,
1,12,
2,2,
2,4,
3,2,
3,3
但这不是我想要的答案。有人能帮我或给我一些建议吗?
这是我实现的代码,但这不是我想要的 使用地图:
Map<Integer, List<Integer>> groups = new TreeMap<Integer, List<Integer>>();
其中“group”是逗号前的数字,“member”是逗号后的数字。
查找组的映射条目,如果不存在则创建:
List<Integer> list = groups.get(group);
if (list==null) {
list = new ArrayList<Integer>();
groups.put(group, list);
}
输出留作练习:)假设您的字符串是
字符串[]
类似的东西应该可以工作:
public static void main(String[] args) {
final Scanner sc = new Scanner(System.in);
final List<String> list = new ArrayList<String>();
while (sc.hasNext()) {
final String next = sc.next();
if ("print".equals(next)) {
break;
}
list.add(next);
}
printGrouped(list);
}
public static void printGrouped(Collection<String> args) {
final TreeMap<Integer, Set<Integer>> map = new TreeMap<Integer, Set<Integer>>() {
@Override
public Set<Integer> get(Object key) {
Set<Integer> list = super.get(key);
if (list == null) {
list = new TreeSet<Integer>();
put((Integer) key, list);
}
return list;
}
};
for (final String string : args) {
final String[] split = string.split(",");
final Set<Integer> list = map.get(Integer.parseInt(split[0]));
list.add(Integer.parseInt(split[1]));
}
for (final Entry<Integer, Set<Integer>> entry : map.entrySet()) {
final String valueString = entry.getValue().toString();
System.out.println(entry.getKey() + " -> " + valueString.substring(1, valueString.length() - 1));
}
}
输出:
1 -> 7, 9, 12
2 -> 2, 4
3 -> 2, 3
您可以使用以下内容:
Map<Integer, Set<Integer>> result = new TreeMap<Integer, Set<Integer>>();
public void parseInput(int key, int value) {
if(result.get(key) == null) { // if you have not encountered the first integer yet, add it to your map.
result.put(key, new TreeSet<Integer>() {{ add(value); }});
} else { // otherwise, just add your value to your existing set. The set will take care of duplicates.
result.get(key).add(value);
}
}
Map result=newtreemap();
公共输入(int键,int值){
如果(result.get(key)==null){//如果还没有遇到第一个整数,请将其添加到映射中。
put(key,newtreeset(){{add(value);}});
}else{//否则,只需将您的值添加到现有集。该集将处理重复项。
结果。获取(键)。添加(值);
}
}
你可以使用整型映射,列表类型。使用树形映射,你只需添加元素,然后打印两个元素的内容。我想他也希望子列表排序。好的观点。在输出阶段,您可以在列表中使用Collections.sort(),然后再对其进行迭代。很抱歉你能修改我上面的代码吗?有人告诉我如何在地图上打印子列表(整数,列表)吗?看看我的答案-它是逐字拼写出来的!感谢bmoris,但是没有main方法,仍然不知道如何将字符串数组添加到上面的代码中。对不起,我是初学者。请完成它,这样我就可以在eclipse中运行了。非常简单!或者至少是Java。
1,9
1,12
1,7
3,3
2,4
3,2
2,2
print
1 -> 7, 9, 12
2 -> 2, 4
3 -> 2, 3
Map<Integer, Set<Integer>> result = new TreeMap<Integer, Set<Integer>>();
public void parseInput(int key, int value) {
if(result.get(key) == null) { // if you have not encountered the first integer yet, add it to your map.
result.put(key, new TreeSet<Integer>() {{ add(value); }});
} else { // otherwise, just add your value to your existing set. The set will take care of duplicates.
result.get(key).add(value);
}
}