简体   繁体   中英

How to put List elements to String Array in Java

Is there a way to put list elements to a String array in java? I sorted a Map by value using a custom Comparator and now trying to put the key(String) elements into an array. The only way I found is looping through the array and the sorted list at the same time and fill up the array that way but it only puts the last element into it.

Example: My map without sorting: {a=5, b=2, c=8, d=1}

After sorted with custom Comparator: [c=8, a=5, b=2, d=1]

Now I simply need to put the key values ( c,a etc.) to the tuple final String[] lettersWithBigValues = new String[n] where n is the length of the tuple.

However, after:

for (int i = 0; i < Integer.parseInt(args[1]); i++) { System.out.println(lettersWithBigValues[i]+","); }

The console gives back: d,d,d,d given that the console line argument is 4

Here is the full function:

public String[] getTopLettersWithValues(final int n){
        final String[] lettersWithBigValues = new String[n];

        final Map<String, Integer> myMap = new HashMap<>();

        int counter = 0;

        for (final List<String> record : records) {

            if (!myMap.containsKey(record.get(1))) {
                myMap.put(record.get(1), counter += Integer.parseInt(record.get(4)));
            } else {
                myMap.computeIfPresent(record.get(1), (k,v) -> v + Integer.parseInt(record.get(4)));
            }
        }

        System.out.println(myMap);

        List<Map.Entry<String, Integer>> sorted = new LinkedList<>(myMap.entrySet());

        // Sort list with list.sort(), using a custom Comparator
        sorted.sort(valueComparator);

        System.out.println(sorted);

        for (int i = 0; i < lettersWithBigValues.length; i++) {
            for (Map.Entry<String, Integer> values: sorted) {
                lettersWithBigValues[i] = values.getKey();
            }
        }
         return lettersWithBigValues;
    }

Where records is a List of data read from a csv file.

And here is the comparator:

    public Comparator<Map.Entry<String, Integer>> valueComparator = (o1, o2) -> {

        Integer v1 = o1.getValue();

        Integer v2 = o2.getValue();

        return v2.compareTo(v1);
    };

You can attain the array of keys as follows:

String [] lettersWithBigValues = myMap.entrySet().stream() // entries of your intial map
        .sorted(valueComparator) // sorted by your comparator
        .map(Map.Entry::getKey) // mapped to only the key e.g. 'd', 'a'
        .toArray(String[]::new); // converted to array

Seems to me that you have mistakenly used a nested for loop, you just need the one:

for (int i = 0; i < lettersWithBigValues.length; i++) {
     lettersWithBigValues[i] = sorted.get(i).getKey();
}

That would return the array: c, a, b, d

As an added suggestion you could also have created the Comparator using the Entry.comparingByValue() method.

Comparator<Entry<String,Integer>> valueComparator = 
                Entry.<String,Integer>comparingByValue().reversed();

It returns a ready made Comparator that compares by the natural order of the value. So to sort in reverse, you just need to tag on the reversed() method which is defined in the Comparator interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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