#create n-subarrays from List of Pairs

1 messages · Page 1 of 1 (latest)

cold stag
#

I have a list of pairs of String-double where some keys are the same "Iris-virginica, Iris-versicolor". How do I create n-subarrays containing these Pairs?

List<Pair<String, Double>> pointsPairList = [Iris-virginica=Infinity, Iris-virginica=7.071067811865468, Iris-versicolor=4.47213595499958, Iris-virginica=3.535533905932734, Iris-virginica=3.162277660168382, Iris-virginica=2.2941573387056184, Iris-versicolor=2.294157338705618, Iris-versicolor=2.294157338705618];

Desired output:

subArray1 = [Iris-virginica=Infinity, Iris-virginica=7.071067811865468,Iris-virginica=3.535533905932734, Iris-virginica=3.162277660168382, Iris-virginica=2.2941573387056184];

subArray2 = [Iris-versicolor=4.47213595499958, Iris-versicolor=2.294157338705618, Iris-versicolor=2.294157338705618];
hard zephyrBOT
#

This post has been reserved for your question.

Hey @cold stag! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

quaint quarry
#

what's the result you'd like

cold stag
#

let me edit the post and write that down

cold stag
quaint quarry
#
  1. create the arrays
  2. iterate through the lists
  3. add the first element of the pair to the first array
  4. add the second element of the pair to the second array
cold stag
#

I want a generic solution such that if there are n-different keys it will create n different sub arrays

#

is there a way to do that

quaint quarry
#

do you know what a Map is?

cold stag
#

I know the basic idea

quaint quarry
#

what about not creating multiple arrays but a map of lists?

cold stag
#

yea that seems good

quaint quarry
#

you first create a map

cold stag
#

so Is there a way for me to group multiple double values belonging to one key string in a map? Like mapping the key string into Linked List of doubles

quaint quarry
#

then iterating through all pairs

#

if the key of the pair doesn't exist, put a new empty list onto the map

#

then add the value of the pair to the corresponding list

cold stag
#

let me try this

cold stag
quaint quarry
#

map.get(yourKey).add(yourValue)

cold stag
#

thanks so much! Worked perfectly @quaint quarry