我目前正在使用java语言在地图上开展大学项目。
地图上填充了有关高尔夫球手的名称,国籍和其他一些统计数据。我遇到的障碍是从南非移除所有玩家(有2个)。
有谁知道在这种情况下我会使用什么功能?我尝试了各种各样的无济于事!
感谢您的帮助
已添加编辑代码
这是我的主要方法
package assessmentmaps;
import java.io.*;
import java.util.*;
public class AssessmentMaps {
public static void main(String[] args)
{
//Create a new map to store golfers and info
Map<String, Golfer> gMap = new HashMap<String, Golfer>();
try
{
//Declare new File, File Reader, and Buffered Reader
File myFile = new File("Top 20 golfers.txt");
FileReader fr = new FileReader(myFile);
BufferedReader br = new BufferedReader(fr);
String line = null;
while((line = br.readLine())!=null)
{
//Tokenize the String
StringTokenizer st = new StringTokenizer(line,",");
//While there are tokens to be read, assign tokens to a new
//golfer. Create Hashcodes for each golfer and add to map.
while(st.hasMoreTokens())
{
Golfer g = new Golfer(st.nextToken()
,st.nextToken()
,st.nextToken()
,st.nextToken());
String key;
key = Integer.toString(g.hashCode());
gMap.put(key, g);
}
}
}
catch(IOException e)
{
//If the file cannot be found, display this message
System.out.println("File not found");
}
//Task 7: Delete the players from RSA
System.out.println("\nTask 7: Delete the players from RSA");
for(Golfer g : values)
{
if(g.getCountry() == "RSA")
{
gMap.remove(g);
}
}
System.out.println(gMap);
}
}
这是我的高尔夫球手
package assessmentmaps;
public class Golfer
{
private String name;
private String country;
private String points;
private String events;
public Golfer(String name, String events, String points, String country)
{
this.name = name;
this.events = events;
this.points = points;
this.country = country;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getCountry()
{
return country;
}
public void setCountry(String country)
{
this.country = country;
}
public String getPoints()
{
return points;
}
public void setPoints(String points)
{
this.points = points;
}
public String getEvents()
{
return events;
}
public void setEvents(String events)
{
this.events = events;
}
@Override
public String toString()
{
return name + " " + events +" " + " " + points + " " + country;
}
@Override
public boolean equals(Object obj)
{
if (obj instanceof Golfer)
{
Golfer GolfPlayer = (Golfer)obj;
if(name.equalsIgnoreCase(GolfPlayer.name))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
@Override
public int hashCode()
{
String hashStr = this.events + Math.round(Double.parseDouble(points));
int hash = Integer.parseInt(hashStr);
return hash;
}
答案 0 :(得分:0)
很难通过提供的详细信息找到一个好的答案。
假设你有表格的地图:
Map<String, ArrayList<String>> golferMap= new HashMap<String, ArrayList<String>>();
String(name)是键和
ArrayList(关于高尔夫球手的其他统计数据)是值
您可以尝试这样的事情(注意:请将此视为参考!):
//after populating the golferMap
for(Iterator<Map.Entry<String, ArrayList<String>>> it = map.entrySet().iterator(); it.hasNext();)
{
Map.Entry<String, ArrayList<String>> entry = it.next();
if(entry.getKey().nationality.equals("South Africa")){
it.remove();
}
}