在java中使用比较器替换字符串

时间:2014-01-14 17:55:49

标签: java arrays string sorting comparator

我想知道是否可以使用比较器比较文件中的递归字符串,并根据其中的元素替换字符串。我有一些数据:

331028124,24,7912,CF,1,1
331028124,24,7631,2B,2,1
331028124,24,5909,1B,3,1   
331028124,24,8394,P,3,2       
331028124,24,7245,LF,4,1
331028124,17,9194,SS,1,1

我想得到

331028124,24,7912,CF,1,1
331028124,24,7631,2B,2,1
331028124,24,8394,P,3,2       
331028124,24,7245,LF,4,1
331028124,17,9194,SS,1,1

有效的做法是比较两个字符串331028124,24,5909,1B,3,1331028124,24,8394,P,3,2

然后用第二个字符串替换第一个字符串,因为字符串的最后一个值中有更高的数字...

到目前为止,我有:

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;

    public class BattingOrder {

    String game_ID;
    String team_ID;
    String player_ID;
    String position;
    String battingOrder;
    String subOrder;

    public BattingOrder(String game, String team, String player, String place,
        String batter, String sub) {
        game_ID = game;
        team_ID = team;
        player_ID = player;
        position = place;
        battingOrder = batter;
        subOrder = sub;
    }

    @Override
    public String toString() {
    return game_ID + "," + team_ID + "," + player_ID + "," + position + ","
            + battingOrder;
    }

    public static void main(String[] args) throws IOException {
            FileInputStream fstream = new FileInputStream(
            "BatterInfo.txt");
    DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            List<BattingOrder> sortList = new ArrayList<BattingOrder>();
                for (String line; (line = br.readLine()) != null;) {
                    String delims = "[,]";
                    String[] parsedData = line.split(delims);
                    sortList.add(new BattingOrder(parsedData[0], parsedData[1],
                parsedData[2], parsedData[3], parsedData[4], parsedData[5]));
    }
            System.out.println("Before Sort");
    for (BattingOrder order : sortList) {
        System.out.println(order);
    }

    Collections.sort(sortList, new Comparator<BattingOrder>() {
        @Override
        public int compare(BattingOrder one, BattingOrder two) {
            if (one.game_ID.equals(two.game_ID)
                    || one.team_ID.equals(two.team_ID)
                    || one.battingOrder.equals(two.battingOrder)) {
                return one.subOrder.compareTo(two.subOrder);
            } else {
                return one.team_ID.compareTo(two.team_ID);
            }

        }
    });
    System.out.println("After Sort");
    for (BattingOrder order : sortList) {
        System.out.println(order);
    }

    br.close();

}

}

输出:

Before Sort
  331028124,24,7912,CF,1,1
  331028124,24,7631,2B,2,1
  331028124,24,5909,1B,3,1   
  331028124,24,8394,P,3,2       
  331028124,24,7245,LF,4,1
  331028124,17,9194,SS,1,1    
    After Sort
  331028124,24,7912,CF,1,1
  331028124,24,7631,2B,2,1
  331028124,24,7245,LF,4,1
  331028124,24,5909,1B,3,1   
  331028124,17,9194,SS,1,1    
  331028124,24,8394,P,3,2

我想:

331028124,24,7912,CF,1,1
331028124,24,7631,2B,2,1
331028124,24,8394,P,3,2       
331028124,24,7245,LF,4,1
331028124,17,9194,SS,1,1

我做错了什么?

1 个答案:

答案 0 :(得分:0)

为了在某些字段上对BattingOrder个对象进行相同的处理(但仍然确定谁有更好的排名)

您必须覆盖equals方法以及hashCode以及identifier

/** 
 *  Different BattingOrders who share these 3 fields
 *  will get the same identifier
 */
public String identifier() {
    return game_ID +"-" + team_ID + "-" + battingOrder;
}

@Override
public boolean equals(Object obj) {
    if (obj instanceof BattingOrder) {
        BattingOrder b = (BattingOrder) obj;
        if (this.game_ID.equals(game_ID) && this.team_ID.equals(b.team_ID) && this.battingOrder.equals(b.battingOrder)) {
            return true;
        }
    }
    return false;
}


@Override
public int hashCode() {
    int hash = 5;
    hash = 59 * hash + Objects.hashCode(this.game_ID);
    hash = 59 * hash + Objects.hashCode(this.team_ID);
    hash = 59 * hash + Objects.hashCode(this.battingOrder);
    return hash;
}

我还BattingOrder实施了Comparable

@Override
public int compareTo(BattingOrder o) {
    if (this.identifier().equals(o.identifier())) {
        return this.subOrder.compareTo(o.subOrder);
    } else if (this.game_ID.equals(o.game_ID) && this.team_ID.equals(o.team_ID)) {
        return this.battingOrder.compareTo(o.battingOrder);
    } else {
        return o.team_ID.compareTo(this.team_ID);
    }
}

现在在main您设置了一些Set集合,您可以跟踪BattingOrder个对象

我正在使用:

HashMap<String,BattingOrder> players = new HashMap<>();

是BattingOrder的identifier是BattingOrder

现在for-each line you are reading执行以下操作:

 String[] parsedData = s.split(",");
 BattingOrder bo = new BattingOrder(parsedData[0], parsedData[1],parsedData[2], parsedData[3], parsedData[4], parsedData[5]);
 if(players.containsKey(bo.identifier())) {
     BattingOrder inMap = players.get(bo.identifier());
     if(bo.compareTo(inMap) > 0) {
         players.put(bo.identifier(), bo);
     }
 } else {
     players.put(bo.identifier(), bo);
 }

阅读完所有内容后:

将您的HashMap转换为List

List<BattingOrder> battingOrderList = new ArrayList<>(players.values());
Collections.sort(battingOrderList);

这是the code and main tester