java 哈希图对键排序_Java Collection Framework哈希图

java 哈希图对键排序

Java HashMap class is an implementation of Map interface based on hash table. It stores elements in key & value pairs which is denoted as HashMap<Key, Value> or HashMap<K, V>.

Java HashMap类是基于哈希表的Map接口的实现。 它以键和值对的形式存储元素,表示为HashMap <Key,Value>或HashMap <K,V>。

It extends AbstractMap class, and implements Map interface and can be accessed by importing java.util package. Declaration of this class is given below.

它扩展了AbstractMap类,并实现了Map接口,可以通过导入java.util包进行访问。 此类的声明在下面给出。

HashMap声明 (HashMap Declaration)

public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V>,Cloneable, Serializable

重要事项: (Important Points:)

  • It is member of the Java Collection Framework.

    它是Java Collection Framework的成员。

  • It uses a hashtable to store the map. This allows the execution time of get() and put() to remain same.

    它使用哈希表存储地图。 这样可以使get()put()的执行时间保持不变。

  • HashMap does not maintain order of its element.

    HashMap不维护其元素的顺序。

  • It contains values based on the key.

    它包含基于键的值。

  • It allows only unique keys.

    它仅允许唯一键。

  • It is unsynchronized.

    它是不同步的。

  • Its initial default capacity is 16.

    其初始默认容量为16。

  • It permits null values and the null key

    它允许空值和空键

  • It is unsynchronized.

    它是不同步的。

HashMap构造函数 (HashMap Constructors)

HashMap class provides following four constructors.

HashMap类提供以下四个构造函数。

HashMap()
HashMap(Map< ? extends k, ? extends V> m)
HashMap(int capacity)
HashMap(int capacity, float loadfactor)

示例:创建一个HashMap (Example: Creating a HashMap)

Lets take an example to create a hashmap that can store integer type key and string values. Initially it is empty because we did not add elements to it. Its elements are enclosed into curly braces.

让我们以创建一个可存储整数类型键和字符串值的哈希图为例。 最初它是空的,因为我们没有向其中添加元素。 其元素包含在花括号中。

import java.util.*;
class Demo
{
  public static void main(String args[])
  {
  // Creating HashMap
    HashMap<Integer,String> hashMap = new HashMap<Integer,String>();

    // Displaying HashMap
    System.out.println(hashMap);
  }
}

{}

{}

向HashMap添加元素 (Adding Elements To HashMap)

After creating a hashmap, now lets add elements to it. HashMap provides put() method that takes two arguments first is key and second is value. See the below example.

创建哈希图之后,现在让我们向其中添加元素。 HashMap提供了put()方法,该方法接受两个参数,第一个是键,第二个是值。 请参见以下示例。

import java.util.*;
class Demo
{
  public static void main(String args[])
  {
  // Creating HashMap
    HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
    // Adding elements
    hashMap.put(1, "One");
    hashMap.put(2, "Two");
    hashMap.put(3, "Three");
    hashMap.put(4, "Four");
    // Displaying HashMap
    System.out.println(hashMap);
  }
}

{1=One, 2=Two, 3=Three, 4=Four}

{1 =一个,2 =两个,3 =三个,4 =四个}

从HashMap中删除元素 (Removing Elements From HashMap)

In case, we need to remove any element from the hashmap. We can use remove() method that takes key as an argument. it has one overloaded remove() method that takes two arguments first is key and second is value.

以防万一,我们需要从哈希图中删除任何元素。 我们可以使用以key作为参数的remove()方法。 它有一个重载的remove()方法,该方法需要两个参数,第一个是key,第二个是value。

import java.util.*;
class Demo
{
  public static void main(String args[])
  {
  // Creating HashMap
    HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
    // Adding elements
    hashMap.put(1, "One");
    hashMap.put(2, "Two");
    hashMap.put(3, "Three");
    hashMap.put(4, "Four");
    // Displaying HashMap
    System.out.println(hashMap);
    // Remove element by key
    hashMap.remove(2);
    System.out.println("After Removing 2 :\n"+hashMap);
    // Remove by key and value
    hashMap.remove(3, "Three");
    System.out.println("After Removing 3 :\n"+hashMap);
    
  }
}

{1=One, 2=Two, 3=Three, 4=Four} After Removing 2 : {1=One, 3=Three, 4=Four} After Removing 3 : {1=One, 4=Four}

{1 =一个,2 =两个,3 =三个,4 =四个}删除2之后:{1 =一个,3 =三个,4 =四个}删除3之后:{1 =一个,4 =四个}

遍历元素 (Traversing Elements)

To access elements of the hashmap, we can traverse them using the loop. In this example, we are using for loop to iterate the elements.

要访问哈希图的元素,我们可以使用循环遍历它们。 在此示例中,我们使用for循环迭代元素。

import java.util.*;
class Demo
{
  public static void main(String args[])
  {
  // Creating HashMap
    HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
    // Adding elements
    hashMap.put(1, "One");
    hashMap.put(2, "Two");
    hashMap.put(3, "Three");
    hashMap.put(4, "Four");
    // Traversing HashMap
    for(Map.Entry<Integer, String> entry : hashMap.entrySet()) {
      System.out.println(entry.getKey()+" : "+entry.getValue());
    }   
  }
}

1 : One 2 : Two 3 : Three 4 : Four

1:1 2:2 3:3 4:4

替换HashMap元素 (Replace HashMap Elements)

HashMap provides built-in methods to replace elements. There are two overloaded replace methods: first takes two arguments one for key and second for the value we want to replace with. Second method takes three arguments first is key and second is value associated with the key and third is value that we want to replace with the key-value.

HashMap提供了内置的方法来替换元素。 有两种重载的replace方法:第一种带有两个参数,一个用于键,第二个用于我们要替换的值。 第二个方法接受三个参数,第一个是键,第二个是与键关联的值,第三个是我们要用键值替换的值。

import java.util.*;
class Demo
{
  public static void main(String args[])
  {
  // Creating HashMap
    HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
    // Adding Elements
    hashMap.put(1, "One");
    hashMap.put(2, "Two");
    hashMap.put(3, "Three");
    hashMap.put(4, "Four");
    // Traversing HashMap
    for(Map.Entry<Integer, String> entry : hashMap.entrySet()) {
      System.out.println(entry.getKey()+" : "+entry.getValue());
    }
    // Replacing Elements of HashMap
    hashMap.replace(1, "One-1");
    System.out.println(hashMap);
    hashMap.replace(1, "One-1", "First");
    System.out.println(hashMap);
  }
}

1 : One 2 : Two 3 : Three 4 : Four {1=One-1, 2=Two, 3=Three, 4=Four} {1=First, 2=Two, 3=Three, 4=Four}

1:一个2:两个3:三个4:四个{1 =一个-1,2 =两个,3 =三个,4 =四个} {1 =第一,2 =两个,3 =三个,4 =四个}

翻译自: https://www.studytonight.com/java/hashmap-in-collection-framework.php

java 哈希图对键排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值