将 int 转换为字母值
Convert an int to a letter value
出于兴趣,我一直在研究一个非常基础的 encoder/decoder 程序。超级简单的编码,a = 1, b = 2... 然后打印出数字:“1 2 3”。我有那个工作:
final Map<Character, Integer> map;
final String str = "hello world";
map = new HashMap<>();
before Java 7.
map.put('a', 1);
map.put('b', 2);
map.put('c', 3);
map.put('d', 4);
map.put('e', 5);
map.put('f', 6);
map.put('g', 7);
map.put('h', 8);
map.put('i', 9);
map.put('j', 10);
map.put('k', 11);
map.put('l', 12);
map.put('m', 13);
map.put('n', 14);
map.put('o', 15);
map.put('p', 16);
map.put('q', 17);
map.put('r', 18);
map.put('s', 19);
map.put('t', 20);
map.put('u', 21);
map.put('v', 22);
map.put('w', 23);
map.put('x', 24);
map.put('y', 25);
map.put('z', 26);
for(final char c : str.toCharArray())
{
final Integer val;
val = map.get(c);
if(val == null)
{
//error
}
else
{
System.out.print(val + " ");
}
}
System.out.println();
}
现在我正在研究将其转换回来的方法。我想我可以用某种反向哈希图来做,但我无法让它工作。有什么建议么?谢谢
从 documentation 中,您可以检查该值是否具有键原像以及它是什么键。事实上,有两个功能有助于解决这个问题:
boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface
K getKey(Object value)
Gets the key that is currently mapped to the specified value.
If the value is not contained in the map, null is returned.
应该这样做:
Integer integerToFind = ...;
Character matchingKey = null;
for (Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue().equals(integerToFind)) {
matchingKey = entry.getKey();
break;
}
}
这假定没有具有相同值的键。
对于此数据,您根本不需要 HashMap
。利用这些字母的字符值都是连续数字这一事实。也就是说,'a'
是97
,'b'
是98
,...,'z'
是122
。
您可以通过减法将字母字符映射到数字96
。
val = c - 96;
您可以通过添加 96
将数字映射回字母,然后将其转换回 char
。
c = (char) (val + 96);
我喜欢 rgettman 的回答,但如果你想要一个非有序代码,这种方式可能会更好。
这里用到了Guava的BiMap
https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
public class Encoder
{
private BiMap<Character, Integer> mCodeMap = makeCodeMap();
public String encode(String string)
{
string = string.trim();
if (string.length() <= 0) return "";
final StringBuilder results = new StringBuilder();
char c = string.charAt(0);
results.append(mCodeMap.get(c));
for (int i = 1; i < string.length(); ++i) {
c = string.charAt(i);
results.append(' ').append(mCodeMap.get(c));
}
return results.toString();
}
public String decode(String string)
{
string = string.trim();
if (string.length() <= 0) return "";
String[] nums = string.split(" ");
final StringBuilder results = new StringBuilder();
final BiMap<Integer, Character>inverse = mCodeMap.inverse();
for (int i = 0; i < nums.length; ++i) {
final int num = Integer.parseInt(nums[i]);
results.append(inverse.get(num));
}
return results.toString();
}
private static BiMap<Character, Integer> makeCodeMap()
{
BiMap<Character, Integer> codeMap = HashBiMap.create();
codeMap.put('a', 1);
codeMap.put('b', 2);
codeMap.put('c', 3);
codeMap.put('d', 4);
codeMap.put('e', 5);
codeMap.put('f', 6);
codeMap.put('g', 7);
codeMap.put('h', 8);
codeMap.put('i', 9);
codeMap.put('j', 10);
codeMap.put('k', 11);
codeMap.put('l', 12);
codeMap.put('m', 13);
codeMap.put('n', 14);
codeMap.put('o', 15);
codeMap.put('p', 16);
codeMap.put('q', 17);
codeMap.put('r', 18);
codeMap.put('s', 19);
codeMap.put('t', 20);
codeMap.put('u', 21);
codeMap.put('v', 22);
codeMap.put('w', 23);
codeMap.put('x', 24);
codeMap.put('y', 25);
codeMap.put('z', 26);
codeMap.put(' ', 27);
return codeMap;
}
public static void main(String[] args)
{
final String inputString = "this is my test input string";
System.out.println("String = " + inputString);
Encoder encoder = new Encoder();
final String encoded = encoder.encode(inputString);
System.out.println("Encoded = " + encoded);
final String decoded = encoder.decode(encoded);
System.out.println("Decoded = " + decoded);
System.out.println("Correct = " + inputString.equals(decoded));
}
}
出于兴趣,我一直在研究一个非常基础的 encoder/decoder 程序。超级简单的编码,a = 1, b = 2... 然后打印出数字:“1 2 3”。我有那个工作:
final Map<Character, Integer> map;
final String str = "hello world";
map = new HashMap<>();
before Java 7.
map.put('a', 1);
map.put('b', 2);
map.put('c', 3);
map.put('d', 4);
map.put('e', 5);
map.put('f', 6);
map.put('g', 7);
map.put('h', 8);
map.put('i', 9);
map.put('j', 10);
map.put('k', 11);
map.put('l', 12);
map.put('m', 13);
map.put('n', 14);
map.put('o', 15);
map.put('p', 16);
map.put('q', 17);
map.put('r', 18);
map.put('s', 19);
map.put('t', 20);
map.put('u', 21);
map.put('v', 22);
map.put('w', 23);
map.put('x', 24);
map.put('y', 25);
map.put('z', 26);
for(final char c : str.toCharArray())
{
final Integer val;
val = map.get(c);
if(val == null)
{
//error
}
else
{
System.out.print(val + " ");
}
}
System.out.println();
}
现在我正在研究将其转换回来的方法。我想我可以用某种反向哈希图来做,但我无法让它工作。有什么建议么?谢谢
从 documentation 中,您可以检查该值是否具有键原像以及它是什么键。事实上,有两个功能有助于解决这个问题:
boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface
K getKey(Object value)
Gets the key that is currently mapped to the specified value.
If the value is not contained in the map, null is returned.
应该这样做:
Integer integerToFind = ...;
Character matchingKey = null;
for (Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue().equals(integerToFind)) {
matchingKey = entry.getKey();
break;
}
}
这假定没有具有相同值的键。
对于此数据,您根本不需要 HashMap
。利用这些字母的字符值都是连续数字这一事实。也就是说,'a'
是97
,'b'
是98
,...,'z'
是122
。
您可以通过减法将字母字符映射到数字96
。
val = c - 96;
您可以通过添加 96
将数字映射回字母,然后将其转换回 char
。
c = (char) (val + 96);
我喜欢 rgettman 的回答,但如果你想要一个非有序代码,这种方式可能会更好。
这里用到了Guava的BiMap https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
public class Encoder
{
private BiMap<Character, Integer> mCodeMap = makeCodeMap();
public String encode(String string)
{
string = string.trim();
if (string.length() <= 0) return "";
final StringBuilder results = new StringBuilder();
char c = string.charAt(0);
results.append(mCodeMap.get(c));
for (int i = 1; i < string.length(); ++i) {
c = string.charAt(i);
results.append(' ').append(mCodeMap.get(c));
}
return results.toString();
}
public String decode(String string)
{
string = string.trim();
if (string.length() <= 0) return "";
String[] nums = string.split(" ");
final StringBuilder results = new StringBuilder();
final BiMap<Integer, Character>inverse = mCodeMap.inverse();
for (int i = 0; i < nums.length; ++i) {
final int num = Integer.parseInt(nums[i]);
results.append(inverse.get(num));
}
return results.toString();
}
private static BiMap<Character, Integer> makeCodeMap()
{
BiMap<Character, Integer> codeMap = HashBiMap.create();
codeMap.put('a', 1);
codeMap.put('b', 2);
codeMap.put('c', 3);
codeMap.put('d', 4);
codeMap.put('e', 5);
codeMap.put('f', 6);
codeMap.put('g', 7);
codeMap.put('h', 8);
codeMap.put('i', 9);
codeMap.put('j', 10);
codeMap.put('k', 11);
codeMap.put('l', 12);
codeMap.put('m', 13);
codeMap.put('n', 14);
codeMap.put('o', 15);
codeMap.put('p', 16);
codeMap.put('q', 17);
codeMap.put('r', 18);
codeMap.put('s', 19);
codeMap.put('t', 20);
codeMap.put('u', 21);
codeMap.put('v', 22);
codeMap.put('w', 23);
codeMap.put('x', 24);
codeMap.put('y', 25);
codeMap.put('z', 26);
codeMap.put(' ', 27);
return codeMap;
}
public static void main(String[] args)
{
final String inputString = "this is my test input string";
System.out.println("String = " + inputString);
Encoder encoder = new Encoder();
final String encoded = encoder.encode(inputString);
System.out.println("Encoded = " + encoded);
final String decoded = encoder.decode(encoded);
System.out.println("Decoded = " + decoded);
System.out.println("Correct = " + inputString.equals(decoded));
}
}