在 Java 中实现键值对
-
在 Java 中使用
HashMap实现键值对 -
在 Java 中使用
Map.Entry实现键值对 -
使用 Java 中的
AbstractMap.SimpleEntry类实现键值对 -
在 Java 中使用
Map.entry实现键值对 -
在 Java 中使用
AbstractMap.SimpleImmutableEntry实现键值对 -
在 Java 中使用
Maps.immutableEntry实现键值对 -
使用 Java 中的
Properties类实现键值对
本教程介绍了如何在 Java 中实现键值对。
在 Java 中,为了处理键-值对,使用了 Map 接口及其实现类。我们可以使用诸如 HashMap 和 TreeMap 之类的数据将数据存储到键-值对中。除了这些内置的类之外,我们还可以创建自己的类,该类可以容纳键-值对。
在这里,我们将使用 HashMap,用户定义的类,AbstractMap,Map.entry(),AbstractMap.SimpleImmutableEntry() 和属性等。让我们仔细看一下这些示例。
在 Java 中使用 HashMap 实现键值对
Collection 框架中的 Java Map 接口可用于将数据存储到键-值对中。在这里,我们使用 HashMap 类来存储字符串类型的键值对。请参见下面的示例。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting extends Thread {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("name", "Rohan");
map.put("sname", "Kumar");
System.out.println(map);
}
}
输出:
{sname=Kumar, name=Rohan}
在 Java 中使用 Map.Entry 实现键值对
在这里,我们使用 Map.Entry 接口创建一个自定义类,该类将以键值对的形式保存数据。我们创建了一个 Student 类,其中有两个实例变量来保存键和值对。我们还创建了 getters 和 setters 方法来为此类的每个实例设置值。请参见下面的示例。
import java.util.Map;
class Student<K, V> implements Map.Entry<K, V> {
private final K key;
private V value;
public Student(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
V old = this.value;
this.value = value;
return old;
}
}
public class SimpleTesting extends Thread {
public static void main(String[] args) {
Student<String, String> student = new Student<>("name", "Rohan");
String key = student.getKey();
String value = student.getValue();
System.out.println("{" + key + ":" + value + "}");
}
}
输出:
{name:Rohan}
使用 Java 中的 AbstractMap.SimpleEntry 类实现键值对
在这里,我们使用 AbstractMap 类来实现键值对。getKey() 和 getValue() 方法分别用于获取键和值。请参见以下示例。
import java.util.AbstractMap;
import java.util.Map;
public class SimpleTesting extends Thread {
public static void main(String[] args) {
Map.Entry<String, String> entry = new AbstractMap.SimpleEntry<String, String>("name", "Rohan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{" + key + ":" + value + "}");
}
}
输出:
{name:Rohan}
在 Java 中使用 Map.entry 实现键值对
我们可以使用 Map.entry 将数据存储到键和值对中。在这里,我们使用 Entry 接口及其方法 getKey() 和 getValue() 分别获取键和值。请参见以下示例。
import java.util.Map;
import java.util.Map.Entry;
public class SimpleTesting extends Thread {
public static void main(String[] args) {
Entry<String, String> entry = Map.entry("name", "Rohan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{" + key + ":" + value + "}");
}
}
输出:
{name:Rohan}
在 Java 中使用 AbstractMap.SimpleImmutableEntry 实现键值对
我们可以使用 SimpleImmutableEntry 来创建一组不可变的键值对。请参见以下示例。
import java.util.AbstractMap;
import java.util.Map.Entry;
public class SimpleTesting extends Thread {
public static void main(String[] args) {
Entry<String, String> entry = new AbstractMap.SimpleImmutableEntry<>("name", "Rohan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{" + key + ":" + value + "}");
}
}
输出:
{name:Rohan}
在 Java 中使用 Maps.immutableEntry 实现键值对
在这里,我们使用 Map.immutableEntry 在 Java 中创建键值对。我们使用 getKey() 和 getValue() 方法分别获取键和值。
import com.google.common.collect.Maps;
import java.util.AbstractMap;
import java.util.Map;
import java.util.Map.Entry;
public class MainClass extends Thread {
public static void main(String[] args) {
Map.Entry<String, String> entry = Maps.immutableEntry("name", "Rohan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{" + key + ":" + value + "}");
}
}
输出:
{name:Rohan}
使用 Java 中的 Properties 类实现键值对
Java 集合的 Properties 类可用于将数据存储为键值对。Properties 类的 getProperty() 方法返回与键关联的值。请参见以下示例。
import java.util.Properties;
public class MainClass extends Thread {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("name", "Rohan"); // (key, value)
String value = props.getProperty("name");
System.out.println("{name:" + value + "}");
}
}
输出:
{name:Rohan}