[英]replacing a string using comparator in java
I was wondering if it is possible to compare recursively strings within a file using comparator and replace a string based on an element within it. 我想知道是否有可能使用比较器比较文件中的递归字符串并基于其中的元素替换字符串。 I have some data: 我有一些数据:
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
I would like to get 我想得到
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
Effectively what this should do is compare two strings say 331028124,24,5909,1B,3,1
and 331028124,24,8394,P,3,2
有效地,这应该是比较两个字符串,例如331028124,24,5909,1B,3,1
和331028124,24,8394,P,3,2
and then replace the first string with the second string because of a higher number in the last value of the string... 然后将第一个字符串替换为第二个字符串,因为该字符串的最后一个值中的数字更大。
So far I have: 到目前为止,我有:
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();
}
}
Output: 输出:
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
I want: 我想要:
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
What am I doing wrong? 我究竟做错了什么?
In order to treat BattingOrder
objects the same on certain fields (but still determine who has better ranking) 为了在某些字段BattingOrder
对象视为相同(但仍确定谁的排名更好)
You must override the equals
method as well as the hashCode
as well as put an identifier
您必须覆盖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;
}
Also I made BattingOrder
implement Comparable
我也使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);
}
}
Now in your main
you set up some Set
Collection where you keep track of your BattingOrder
objects 现在在您的main
,设置一些Set
Collection,以跟踪BattingOrder
对象
I am using: 我在用:
HashMap<String,BattingOrder> players = new HashMap<>();
Where the Key is the BattingOrder's identifier
and the Value is the BattingOrder 其中键是BattingOrder的identifier
,而值是BattingOrder
Now for-each line you are reading
do the following: 现在, 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);
}
Once you have read all the lines: 阅读完所有内容后:
Convert your HashMap
into a List
将您的HashMap
转换为List
List<BattingOrder> battingOrderList = new ArrayList<>(players.values());
Collections.sort(battingOrderList);
Here is the code and main tester 这是代码和主要测试人员
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.