通过缩写匹配字符串的最佳方法是什么

时间:2015-02-26 18:38:26

标签: java

我有一个名为频率的列表[季度,月度,每周等]。通过调用java方法,我希望使用诸如“M”之类的缩写来正确评估诸如frecuencia.equals(“Monthly”)之类的语句。

2 个答案:

答案 0 :(得分:1)

您可以将List上的频率保留在Map上,而不是将频率保留在Map<String,String> mapping = new HashMap<>(); mapping.put("Q","quarterly"); mapping.put("M","mensual"); mapping.put("W","weekly"); String input = "M"; //scan, parse, or received somehow from something String frequency = mapping.get(input); 上。

示例:

frequency

现在mensual拥有{{1}}

答案 1 :(得分:0)

对于List中的contains()方法:

boolean contains(Object o)

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

对于List中的equals()方法:

boolean equals(Object o)

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

因此,如果List包含一个元素,则contains返回一个布尔值,而equals比较两个列表,如果两个列表匹配则返回一个布尔值。可以找到更多here