Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding partial duplicate values and keeping a specific
    primarykey
    data
    text
    <p>I have a list which contains a value and another list, in this case a trainNumber with corresponding list of Stations. However there are duplicate trainNumbers, which might have different stations. From this I am trying to create a new list keeping the trainNumbers which have the most stations.</p> <p>For example if I have a list which contains: </p> <pre><code>String trainNumber = 1, List trainNumber = [Station1, Station3] String trainNumber = 2, List trainNumber = [Station1, Station2, Station3] String trainNumber = 3, List trainNumber = [Station1, Station3] String trainNumber = 3, List trainNumber = [Station3] </code></pre> <p>I want the new list to contain: </p> <pre><code>String trainNumber = 1, List trainNumber = [Station1, Station3] String trainNumber = 2, List trainNumber = [Station1, Station2, Station3] String trainNumber = 3, List trainNumber = [Station1, Station3] </code></pre> <p>I have seen usage of Set to remove duplicate list items, however I need to specify which item I want to keep.</p> <pre><code>HashSet&lt;Trains&gt; newList = new HashSet&lt;Trains&gt;(); for(Trains train: trainOverview){ String trainNumber = train.getTrainNumber(); int stationSize = train.getStations().size(); int largest = 0; for(Trains trainCopy: trainOverview){ if(trainNumber.equals(trainCopy.getTrainNumber())){ int stationCopySize = trainCopy.getStations().size(); if(stationCopySize &gt; largest) largest = stationCopySize; } } if(togSize &gt;= largest){ newList.add(tog); } } trainOverview.clear(); trainOverview.addAll(newList); </code></pre> <p>Now this kinda works, but I find it extremely messy. I am using HashSet to remove duplicates which have the same amount of stations (which also occurs). There surely must be a better way to go about this problem?</p> <p><strong>EDIT</strong>: Thanks for you answers, but i see that the put method replaces the value of the key. Im my case I would like to keep the values (stations) that were mapped the first time to the key(trainNumber) in addition to the new ones.</p> <p>I've never worked with a Map before, but this is my approach (not sure if Im using it correct):</p> <pre><code> Map&lt;String, List&lt;Station&gt;&gt; overview= new TreeMap&lt;String, List&lt;Station&gt;&gt;(); for(Trains train: trainOverview){ List&lt;Stations&gt; lista = overview.get(train.getTrainNumber()); //Merge lists if the key already exists, and replace the old value with the merged list if(overview.containsKey(train.getTrainNumber())){ Set setboth = new HashSet(lista); setboth.addAll(train.getStations()); lista.clear(); lista.addAll(setboth); overview.put(train.getTrainNumber(), lista); } //If no key exists, create a new entry else{ overview.put(train.getTrainNumber(), train.getStations()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload