简体   繁体   English

将sortedSet转换为整数数组

[英]Converting a sortedSet to an Integer Array

Intro 介绍

First of all I want to start off by saying that I am learning java so if at any point I am doing something that is inefficient or could be done better, please let me know. 首先,我想首先说我正在学习java,所以如果我在做任何低效或可以做得更好的事情,请告诉我。

What I am trying to do is sort a HashMap alphabetically by key and then return a list of the values in that order. 我想要做的是按键按字母顺序排序HashMap,然后按顺序返回值列表。 After googling, I found that I could sort a HashMap easily using a SortedSet, but then I run into the problem of how do I get the tree set into an array? 在谷歌搜索之后,我发现我可以使用SortedSet轻松地对HashMap进行排序,但后来我遇到了如何将树设置为数组的问题?

Example

An input like this: 像这样的输入:

{"apple", "pear", "cherry", "apple", "cherry", "pear", "apple", "banana"} {“apple”,“pear”,“cherry”,“apple”,“cherry”,“pear”,“apple”,“banana”}

Should return this: 应该归还:

{3,1,2,2} {3,1,2,2}

My Code (so far) 我的代码 (到目前为止)

import java.util.*;

public class SortedFreqs {
    public int[] freqs(String[] data) {
        HashMap<String, Integer> myMap = new HashMap<String, Integer>();
        for (String s: data){
            if (!myMap.containsKey(s)){
                myMap.put(s, 0);
            }
        myMap.put(s, myMap.get(s)+1);
        }
    SortedSet<Integer> values = new TreeSet<Integer>(myMap.values());
    }
}

SortedSet extends Collection which defines the method toArray : SortedSet extends Collection定义方法toArray

Integer[] toArray = values.toArray(new Integer[values.size()]);

However, there is one problem which is that the collections use generics and your method is defined as returning a primitive array of int[] . 但是,有一个问题是集合使用泛型,并且您的方法被定义为返回int[]的原始数组。 There's no built in way to convert an int[] to an Integer[] so you either need to change your method signature or copy the array yourself: 没有内置的方法将int[]转换为Integer[]因此您需要更改方法签名或自己复制数组:

int[] primitives = new int[toArray.length];
for(int i = 0; i < toArray.length; i++) {

    primitives[i] = toArray[i].intValue();
    // or just    = toArray[i];
    // because intValue is called automagically by the compiler
}

Though, actually, it seems like here you should just be returning the Map. 虽然,实际上,你似乎应该回到地图。 Maybe use TreeMap instead of HashMap if you want to return it sorted alphabetically. 如果要按字母顺序返回它,可能使用TreeMap而不是HashMap。

If you really want to do it the way you described in the OP, you'd need to sort the values by their key. 如果您真的想按照OP中描述的方式进行操作,则需要按键对值进行排序。 The problem being that new TreeSet<Integer>(myMap.values()) sorts the integers numerically. 问题是new TreeSet<Integer>(myMap.values())以数字方式对整数进行排序。 So you'd need to do something like: 所以你需要做类似的事情:

Collection<Integer> values = new TreeMap<String, Integer>(myMap).values();
Integer[] toArray = values.toArray(new Integer[values.size()]);

But I'd recommend returning a Map. 但我建议你回一张地图。 Because otherwise it's difficult to tell which numbers correspond to which String. 因为否则很难分辨哪些数字对应于哪个字符串。

Solved it like this, thanks for the comments and responses. 解决这个问题,感谢您的评论和回复。 Really helped. 真的很有帮助。

import java.util.*;

public class SortedFreqs {
    public int[] freqs(String[] data) {
        int count = 0;
        TreeMap<String, Integer> myMap = new TreeMap<String, Integer>();
        for (String s: data){
            if (!myMap.containsKey(s)){
                myMap.put(s, 0);
                count++;
            }
        myMap.put(s, myMap.get(s)+1);
        }
    int [] ans = new int [count];
    int c = 0;
    for (String i: myMap.keySet()){
        ans[c] = myMap.get(i);
        c++;
    }
    return ans;
    }
}
public static Integer[] freqs(String[] data) {
    HashMap<String, Integer> myMap = new HashMap<String, Integer>();
    for (String s : data) {
        if (!myMap.containsKey(s)) {
            myMap.put(s, 0);
        }
        myMap.put(s, myMap.get(s) + 1);
    }
    return new TreeMap<String, Integer>(myMap).values().toArray(new Integer[myMap.size()]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM