查找部分重复值并保持特定值

时间:2013-09-04 09:13:24

标签: java list duplicates hashset

我有一个列表,其中包含一个值和另一个列表,在本例中是一个trainNumber,其中包含相应的Stations列表。但是有一些重复的trainNumbers,可能有不同的站点。由此我试图创建一个新的列表,保持trainNumbers拥有最多的站点。

例如,如果我有一个包含以下内容的列表:

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]

我希望新列表包含:

String trainNumber = 1, List trainNumber = [Station1, Station3]  
String trainNumber = 2, List trainNumber = [Station1, Station2, Station3]  
String trainNumber = 3, List trainNumber = [Station1, Station3]

我已经看到使用Set删除重复的列表项,但是我需要指定要保留的项目。

HashSet<Trains> newList = new HashSet<Trains>();
    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 > largest) largest = stationCopySize;
            }
        }
        if(togSize >= largest){
            newList.add(tog);
        }
    }

    trainOverview.clear();
    trainOverview.addAll(newList);

现在这种方法有效,但我发现它非常混乱。我正在使用HashSet删除具有相同数量的站点的重复项(也会发生)。肯定有更好的方法来解决这个问题吗?

编辑:谢谢你的答案,但我看到put方法取代了键的值。在我的情况下,我想保留第一次映射到键(trainNumber)的值(站点)以及新的值。

我之前从未使用过Map,但这是我的方法(不确定我是否正确使用它):

    Map<String, List<Station>> overview= new TreeMap<String, List<Station>>();
    for(Trains train: trainOverview){
        List<Stations> 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());
        }   
    }

2 个答案:

答案 0 :(得分:4)

不要使用两个列表,而只使用Map<String, List<Station>>trainNumber将是唯一键,您可以轻松地在地图中的相应列表中添加或删除电台。

<强>更新

正如@ipavlic所建议的那样使用Map<String, Set<Station>>,因为一个集合不允许重复元素。

<强> UPDATE2

这是一个小例子,有一些评论。该示例显示了如何使用与集合相结合的地图。您应该将其复制到代码1:1中,因为这不是面向对象的方法。地图应该封装在某个对象中。

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class MapSetExample
{
  public static void main(String[] args)
  {
    Map<String, Set<String>> map = new HashMap<String, Set<String>>();
    System.out.println("ADDED = " + add(map, "Train1", "Station1"));
    System.out.println("ADDED = " + add(map, "Train1", "Station2"));
    System.out.println("ADDED = " + add(map, "Train1", "Station1"));

    System.out.println("Stations of Train1 = " + map.get("Train1"));

  }

  private static boolean add(Map<String, Set<String>> map, String key, String station) {
    Set<String> set = map.get(key);

    /* If map.get() returns null, that means there is no set
     * in the map associated with given key.
     * 
     * In that case we create a new set.
     * 
     * If there is already a set, we use that one.
     */
    if (set == null) {
      set = new TreeSet<String>();
      map.put(key, set);
    }
    /* False if station is already in set. At this point you could also delete sth. etc. */
    boolean success = set.add(station);
    return success;
  }
}

<强>输出

ADDED = true
ADDED = true
ADDED = false
Stations of Train1 = [Station1, Station2]

答案 1 :(得分:1)

我认为更好的是Map<String, Set<Station>>trainNumber为唯一密钥,Set为工作站以避免重复。