将字母数字字符串转换为整数?

时间:2016-05-19 12:56:57

标签: java hashmap

我有一个hashMap,包含键和值'String'。我从我的selenium自动化脚本中的网页获取这些值。

我的hashmap有以下

<Italy, 3.3 millions>
<Venezuela, 30.69 millions>
<Japan, 127.1 millions>

如何将所有字符串字母数字值转换为整数,以便我可以在hashmap上应用排序?

我必须显示“数百万”这个词。

4 个答案:

答案 0 :(得分:1)

据我所知,你的问题是你需要做的是能够对这些值进行排序,所以你需要的是Comparator

以下是可以解决这个问题的Comparator

Comparator<String> comparator = new Comparator<String>() {
    @Override
    public int compare(final String value1, final String value2) {
        return Double.compare(
            Double.parseDouble(value1.substring(0, value1.length() - 9)),
            Double.parseDouble(value2.substring(0, value2.length() - 9))
        );
    }
};
System.out.println(comparator.compare("3.3 millions", "30.69 millions"));
System.out.println(comparator.compare("30.69 millions", "30.69 millions"));
System.out.println(comparator.compare("127.1 millions", "30.69 millions"));

输出

-1
0
1

答案 1 :(得分:0)

如果你只有数百万,你可以尝试这样的事情

  String str = "3.3 Millions"; 
  String[] splitted = str.split(" ");
  double i = Double.valueOf(splitted[0])*1000000;
  System.out.println(i);

或根据子字符串进行计算

答案 2 :(得分:0)

不确定这是否是你要找的..如果我做对了你必须改变你的地图

<String, String> to <String, Double>.

请参阅下面的示例:

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

public class NewClass9 {

public static void main(String[] args) throws ParseException{     
  Map<String,String> oldMap = new HashMap<>();
  oldMap.put("Italy", "3.3 millions");
  oldMap.put("Venezuela", "30.69 millions");
  oldMap.put("Japan", "127.1 millions");
  Map<String,Double> newMap = new HashMap<>();
  for(String key : oldMap.keySet()){
      newMap.put(key, convert(oldMap.get(key)));
  }

  for(String key : newMap.keySet()){
      System.out.printf("%.0f millions\n" ,newMap.get(key));
  }
}

private static double convert(String str) {       
   String[] splitted = str.split(" ");
   return Double.valueOf(splitted[0])*1000000;
}   
}

答案 3 :(得分:0)

有点矫枉过正,但这应该是可扩展的。

注意:我只介绍了乘数查找。

/**
 * Possible units and their multipliers.
 */
enum Unit {
    Unit(1),
    Hundred(100),
    Thousand(1000),
    Million(1000000),
    Billion(1000000000),
    Squillion(Integer.MAX_VALUE);

    private final int multiplier;

    Unit(int multiplier) {
        this.multiplier = multiplier;
    }
}

/**
 * Comparator that matches caseless and plurals
 *
 * NB: Not certain if this is consistent.
 */
private static final Comparator<String> COMPARECASELESSANDPLURALS
        = (String o1, String o2) -> {
            // Allow case difference AND plurals.
            o1 = o1.toLowerCase();
            o2 = o2.toLowerCase();
            int diff = o1.compareTo(o2);
            if (diff != 0) {
                // One character different in length?
                if (Math.abs(o1.length() - o2.length()) == 1) {
                    // Which may be plural?
                    if (o1.length() > o2.length()) {
                        // o1 might be plural.
                        if (o1.endsWith("s")) {
                            diff = o1.substring(0, o1.length() - 1).compareTo(o2);
                        }
                    } else if (o2.endsWith("s")) {
                        // o2 might be plural.
                        diff = -o2.substring(0, o2.length() - 1).compareTo(o1);
                    }
                }
            }
            return diff;
        };

// Build my lookup.
static final Map<String, Integer> MULTIPLIERS
        = Arrays.stream(Unit.values())
        // Collect into a Map
        .collect(Collectors.toMap(
                // From name of the enum.
                u -> u.name(),
                // To its multiplier.
                u -> u.multiplier,
                // Runtime exception in case of duplicates.
                (k, v) -> {
                    throw new RuntimeException(String.format("Duplicate key %s", k));
                },
                // Use a TreeMap that ignores case and plural.
                () -> new TreeMap(COMPARECASELESSANDPLURALS)));

// Gives the multiplier for a word.
public Optional<Integer> getMultiplier(String word) {
    return Optional.ofNullable(MULTIPLIERS.get(word));
}

public void test() {
    String[] tests = {"Million", "Millions", "Thousand", "Aardvark", "billion", "billions", "squillion"};
    for (String s : tests) {
        System.out.println("multiplier(" + s + ") = " + getMultiplier(s).orElse(1));
    }
}