如何将字符串排序到映射中并打印结果

发布于 2024-11-23 21:50:25 字数 251 浏览 1 评论 0原文

我有一个格式为 nm=Alan&hei=72&hair=brown 的字符串,

我想将此信息分开,添加对第一个值的转换并以

nm Name Alan
hei Height 72
hair Hair Color brown

I' 格式打印结果我们研究了使用 split 函数和哈希图的各种方法,但没有将它们拼凑在一起。

任何建议对我来说都非常有用。

I have a string in the format nm=Alan&hei=72&hair=brown

I would like to split this information up, add a conversion to the first value and print the results in the format

nm Name Alan
hei Height 72
hair Hair Color brown

I've looked at various methods using the split function and hashmaps but have had no luck piecing it all together.

Any advice would be very useful to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜中书 2024-11-30 21:50:25
Map<String, String> aliases = new HashMap<String, String>();
aliases.put("nm", "Name");
aliases.put("hei", "Height");
aliases.put("hair", "Hair Color");

String[] params = str.split("&"); // gives you string array: nm=Alan, hei=72, hair=brown

for (String p : params) {
    String[] nv = p.split("=");
    String name = nv[0];
    String value = nv[1];
    System.out.println(nv[0] + " " + aliases.get(nv[0]) + " " + nv[1]);
}

我真的不明白你的问题是什么......

Map<String, String> aliases = new HashMap<String, String>();
aliases.put("nm", "Name");
aliases.put("hei", "Height");
aliases.put("hair", "Hair Color");

String[] params = str.split("&"); // gives you string array: nm=Alan, hei=72, hair=brown

for (String p : params) {
    String[] nv = p.split("=");
    String name = nv[0];
    String value = nv[1];
    System.out.println(nv[0] + " " + aliases.get(nv[0]) + " " + nv[1]);
}

I really do not understand what you problem was...

遗心遗梦遗幸福 2024-11-30 21:50:25

尝试这样的事情:

static final String DELIMETER = "&"
Map<String,String> map = ...
map.put("nm","Name");
map.put("hei","Height");
map.put("hair","Hair color");

StringBuilder builder = new StringBuilder();
String input = "nm=Alan&hei=72&hair=brown"
String[] splitted = input.split(DELIMETER);
for(Stirng str : splitted){
     int index = str.indexOf("=");     
     String key = str.substring(0,index);
     builder.append(key);
     builder.append(map.get(key));
     builder.append(str.substring(index));
     builder.append("\n");
}

Try something like this:

static final String DELIMETER = "&"
Map<String,String> map = ...
map.put("nm","Name");
map.put("hei","Height");
map.put("hair","Hair color");

StringBuilder builder = new StringBuilder();
String input = "nm=Alan&hei=72&hair=brown"
String[] splitted = input.split(DELIMETER);
for(Stirng str : splitted){
     int index = str.indexOf("=");     
     String key = str.substring(0,index);
     builder.append(key);
     builder.append(map.get(key));
     builder.append(str.substring(index));
     builder.append("\n");
}
乞讨 2024-11-30 21:50:25

HashMap 由许多键、值对组成。因此,当您使用 split,设计一个合适的正则表达式 (&)。获得字符串数组后,您可以使用其中一个元素作为键(考虑哪个元素将成为最佳键)。但是,您现在可能想知道“如何将其余元素放置为值?”。也许您可以创建一个新类来存储其余元素并使用此类的对象作为哈希图的值。

然后打印就变得容易了——只需搜索相应键的值即可。该值将是一个对象;在此对象上使用适当的方法来检索元素,您应该能够打印所有内容。

另外,请记住处理代码中的异常。例如检查空值等。

另一件事:你的 qn 提到了“排序”这个词。我不完全明白这在这种情况下意味着什么......

A HashMap consists of many key, value pairs. So when you use split, devise an appropriate regex (&). Once you have your string array, you can use one of the elements as the key (think about which element will make the best key). However, you may now be wondering- "how do I place the rest of elements as the values?". Perhaps you can create a new class which stores the rest of the elements and use objects of this class as values for the hashmap.

Then printing becomes easy- merely search for the value of the corresponding key. This value will be an object; use the appropriate method on this object to retrieve the elements and you should be able to print everything.

Also, remember to handle exceptions in your code. e.g. check for nulls, etc.

Another thing: your qn mentions the word "sort". I don't fully get what that means in this context...

叫思念不要吵 2024-11-30 21:50:25
Map<String, String> propsMap = new HashMap<String, String>();
Map<String, String> propAlias = new HashMap<String, String>();

propAlias.put("nm", "Name");
propAlias.put("hei", "Height");
propAlias.put("hair", "Hair Color");

String[] props = input.split("&");

if (props != null && props.length > 0) {
    for (String prop : props) {
        String[] propVal = prop.split("=");

        if (propVal != null && propVal.length == 2) {
            propsMap.put(propVal[0], propVal[1]);
        }
    }
}

for (Map.Entry tuple : propsMap.getEntrySet()) {
    if (propAlias.containsKey(tuple.getKey())) {
        System.out.println(tuple.getKey() + " " + propAlias.get(tuple.getKey()) + " " + tuple.getValue());
    }
}
Map<String, String> propsMap = new HashMap<String, String>();
Map<String, String> propAlias = new HashMap<String, String>();

propAlias.put("nm", "Name");
propAlias.put("hei", "Height");
propAlias.put("hair", "Hair Color");

String[] props = input.split("&");

if (props != null && props.length > 0) {
    for (String prop : props) {
        String[] propVal = prop.split("=");

        if (propVal != null && propVal.length == 2) {
            propsMap.put(propVal[0], propVal[1]);
        }
    }
}

for (Map.Entry tuple : propsMap.getEntrySet()) {
    if (propAlias.containsKey(tuple.getKey())) {
        System.out.println(tuple.getKey() + " " + propAlias.get(tuple.getKey()) + " " + tuple.getValue());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文