不能用Hashmap一定要用LinkedHashMap因为是有序的
import java.util.LinkedHashMap;
import java.util.Map;
public class TestGeneral {
public static void main(String[] args) {
//store orignal User and Country
Map hmOriginal = new LinkedHashMap();
hmOriginal.put("John", "Canada");
hmOriginal.put("Bob", "USA");
hmOriginal.put("Tom", "USA");
hmOriginal.put("Alice", "Canada");
hmOriginal.put("Alan", "Canada");
hmOriginal.put("Peter", "UK");
//store Updated data
Map hmUpdated = new LinkedHashMap();
for (Map.Entry entry : hmOriginal.entrySet())
{
String sName = entry.getKey();
String sCountry = entry.getValue();
if( !hmUpdated.containsValue(sCountry))
{
hmUpdated.put(sName, sCountry);
}
}
System.out.println("Original data is :"+hmOriginal );
System.out.println("Updated data is :"+hmUpdated );
}
}
围观各路大神
[本文发送自华新iOS App]