C#难民寻求一些Java收集帮助

时间:2022-07-06 19:10:39

I need to store key/value info in some type of collection. In C#, I'd define a dictionary like this:

我需要在某些类型的集合中存储键/值信息。在C#中,我会定义一个这样的字典:

var entries = new Dictionary<string, int>();
entries.Add("Stop me", 11);
entries.Add("Feed me", 12);
entries.Add("Walk me", 13);

Then I would access the values so:

然后我会访问这些值:

int value = entries["Stop me"];

How do I do this in Java? I've seen examples with ArrayList, but I'd like the solution with generics, if possible.

我如何用Java做到这一点?我已经看过ArrayList的例子,但如果可能的话,我想要使用泛型的解决方案。

6 个解决方案

#1


You want to use a Map

您想使用地图

Map<String, Integer> m = new HashMap<String, Integer>();
m.put("Stop me", 11);
Integer i = m.get("Stop me"); // i == 11

Note that on the last line, I could have said:

请注意,在最后一行,我可以说:

int i = m.get("Stop me");

Which is shorthand for (with Java's auto-unboxing):

这是(使用Java的自动拆箱)的简写:

int i = m.get("Stop me").intValue()

If there is no value in the map at the given key, the get returns null and this expression throws a NullPointerException. Hence it's always a good idea to use the boxed type Integer in this case

如果给定键的映射中没有值,则get返回null,并且此表达式抛出NullPointerException。因此,在这种情况下使用盒装类型的Integer总是一个好主意

#2


Use a java.util.Map. There are several implementations:

使用java.util.Map。有几种实现:

  • HashMap: O(1) lookup, does not maintain order of keys
  • HashMap:O(1)查找,不维护键的顺序

  • TreeMap: O(log n) lookup, maintains order of keys, so you can iterate over them in a guaranteed order
  • TreeMap:O(log n)查找,维护键的顺序,因此您可以按保证顺序迭代它们

  • LinkedHashMap: O(1) lookup, iterates over keys in the order they were added to the map.
  • LinkedHashMap:O(1)查找,按照它们添加到地图的顺序迭代键。

You use them like:

你使用它们像:

Map<String,Integer> map = new HashMap<String,Integer>();
map.put("Stop me", 11);
map.put("Feed me", 12);

int value = map.get("Stop me");

For added convenience working with collections, have a look at the Google Collections library. It's excellent.

为了更方便地使用集合,请查看Google Collections库。这很棒。

#3


You use a Map in Java.

您在Java中使用Map。

Note that you can't use int (or any other primitive type) as a generic type parameter, but because of autoboxing, it still behaves almost as if it were a Map<String, int> instead of a Map<String, Integer>. (You don't want to be doing a lot of autoboxing in performance-sensitive code, though.)

请注意,您不能使用int(或任何其他基本类型)作为泛型类型参数,但由于自动装箱,它的行为几乎就像是Map 而不是Map 。 (但是,您不希望在性能敏感的代码中进行大量的自动装箱。) ,integer> ,int>

Map<String, Integer> entries = new HashMap<String, Integer>();
entries.put("Stop me", 11);
entries.put("Feed me", 12);
entries.put("Walk me", 13);
int value = entries.get("Stop me"); // if you know it exists
// If you're not sure whether the map contains a value, it's better to do:
Integer boxedValue = entries.get("Punch me");
if (boxedValue != null) {
    int unboxedValue = boxedValue;
    ...
}

#4


It looks like you are looking for something like HashMap

看起来你正在寻找像HashMap这样的东西

#5


Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Stop Me", 11);
map.put("Feed Me", 12);
map.put("Walk Me", 13);
Integer x; // little hack
int value = (x = a.get("aaa")) == null? 0 : x;

as alternative you can try Enum:

作为替代,您可以尝试枚举:

enum Action {

    STOP(11),
    FEED(12),
    WALK(13);

    private final int value;

    private Action(int value) {
        this.value = value;
    }

    public int value() {
        return value;
    }

    public static Action valueOf(int value) {
        for (Action action : values()) {
            if (action.value == value) {
                return action;
            }
        }

        return null; // or a null-object
    }
}

test:

public void action() {
    Action action = Action.valueOf("FEED"); 
    // or Action.FEED for more compile-time safety
    int value = action.value();
    // instantiating by code 
    Action walk = Action.valueOf(13);
}

#6


You definitely want a HashMap, which is the Java version of C# Dictionary.

你肯定想要一个HashMap,它是C#Dictionary的Java版本。

#1


You want to use a Map

您想使用地图

Map<String, Integer> m = new HashMap<String, Integer>();
m.put("Stop me", 11);
Integer i = m.get("Stop me"); // i == 11

Note that on the last line, I could have said:

请注意,在最后一行,我可以说:

int i = m.get("Stop me");

Which is shorthand for (with Java's auto-unboxing):

这是(使用Java的自动拆箱)的简写:

int i = m.get("Stop me").intValue()

If there is no value in the map at the given key, the get returns null and this expression throws a NullPointerException. Hence it's always a good idea to use the boxed type Integer in this case

如果给定键的映射中没有值,则get返回null,并且此表达式抛出NullPointerException。因此,在这种情况下使用盒装类型的Integer总是一个好主意

#2


Use a java.util.Map. There are several implementations:

使用java.util.Map。有几种实现:

  • HashMap: O(1) lookup, does not maintain order of keys
  • HashMap:O(1)查找,不维护键的顺序

  • TreeMap: O(log n) lookup, maintains order of keys, so you can iterate over them in a guaranteed order
  • TreeMap:O(log n)查找,维护键的顺序,因此您可以按保证顺序迭代它们

  • LinkedHashMap: O(1) lookup, iterates over keys in the order they were added to the map.
  • LinkedHashMap:O(1)查找,按照它们添加到地图的顺序迭代键。

You use them like:

你使用它们像:

Map<String,Integer> map = new HashMap<String,Integer>();
map.put("Stop me", 11);
map.put("Feed me", 12);

int value = map.get("Stop me");

For added convenience working with collections, have a look at the Google Collections library. It's excellent.

为了更方便地使用集合,请查看Google Collections库。这很棒。

#3


You use a Map in Java.

您在Java中使用Map。

Note that you can't use int (or any other primitive type) as a generic type parameter, but because of autoboxing, it still behaves almost as if it were a Map<String, int> instead of a Map<String, Integer>. (You don't want to be doing a lot of autoboxing in performance-sensitive code, though.)

请注意,您不能使用int(或任何其他基本类型)作为泛型类型参数,但由于自动装箱,它的行为几乎就像是Map 而不是Map 。 (但是,您不希望在性能敏感的代码中进行大量的自动装箱。) ,integer> ,int>

Map<String, Integer> entries = new HashMap<String, Integer>();
entries.put("Stop me", 11);
entries.put("Feed me", 12);
entries.put("Walk me", 13);
int value = entries.get("Stop me"); // if you know it exists
// If you're not sure whether the map contains a value, it's better to do:
Integer boxedValue = entries.get("Punch me");
if (boxedValue != null) {
    int unboxedValue = boxedValue;
    ...
}

#4


It looks like you are looking for something like HashMap

看起来你正在寻找像HashMap这样的东西

#5


Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Stop Me", 11);
map.put("Feed Me", 12);
map.put("Walk Me", 13);
Integer x; // little hack
int value = (x = a.get("aaa")) == null? 0 : x;

as alternative you can try Enum:

作为替代,您可以尝试枚举:

enum Action {

    STOP(11),
    FEED(12),
    WALK(13);

    private final int value;

    private Action(int value) {
        this.value = value;
    }

    public int value() {
        return value;
    }

    public static Action valueOf(int value) {
        for (Action action : values()) {
            if (action.value == value) {
                return action;
            }
        }

        return null; // or a null-object
    }
}

test:

public void action() {
    Action action = Action.valueOf("FEED"); 
    // or Action.FEED for more compile-time safety
    int value = action.value();
    // instantiating by code 
    Action walk = Action.valueOf(13);
}

#6


You definitely want a HashMap, which is the Java version of C# Dictionary.

你肯定想要一个HashMap,它是C#Dictionary的Java版本。