繁体   English   中英

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

[英]What's the best way to match a string by its abbreviation

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

而不是将频率保存在“ 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

对于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)).

对于列表中的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比较两个列表会返回一个布尔值,如果两个列表都匹配则会返回一个布尔值。 这里可以找到更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM