Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么';It’用缩写来匹配字符串是最好的方法_Java - Fatal编程技术网

Java 什么';It’用缩写来匹配字符串是最好的方法

Java 什么';It’用缩写来匹配字符串是最好的方法,java,Java,我有一个名为频率[季度、月度、每周等]的列表。通过调用java方法,我希望frecuencia.equals(“Monthly”)这样的语句能够使用缩写词“M”正确计算。对于列表中的contains()方法, boolean contains(Object o) Returns true if this list contains the specified element. More formally, returns true if and only if this list contai

我有一个名为频率[季度、月度、每周等]的列表。通过调用java方法,我希望frecuencia.equals(“Monthly”)这样的语句能够使用缩写词“M”正确计算。对于列表中的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)).
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.
对于列表中的equals()方法:

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

So contains在列表包含元素时返回布尔值,而equals比较两个列表,并在两个列表匹配时返回布尔值。可以找到更多的而不是将频率保存在
列表上
您可以将它们放在
地图上
,键是缩写

例如:

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);
Map-mapping=newhashmap();
映射。出售(“Q”、“季度”);
映射。放置(“M”、“mensional”);
映射。put(“W”,“weekly”);
字符串输入=“M”//扫描、解析或以某种方式从某物接收
字符串频率=mapping.get(输入);
现在
frequency
保持
mensual

contains()
是部分匹配
equals()
是完全匹配。可能您可以将
频率
映射到所需的缩写。如果(frequency.startsWith(缩写)){/*做点什么*/}