📜  Java中的属性 entrySet() 方法和示例

📅  最后修改于: 2022-05-13 01:54:26.729000             🧑  作者: Mango

Java中的属性 entrySet() 方法和示例

Properties 类entrySet()方法用于获取此 Properties 映射的 Set 视图。

句法:

public Set entrySet()

参数:此方法不接受任何参数。

返回:此方法返回此 Properties 对象的映射的Set视图。

下面的程序说明了 entrySet() 方法:

方案一:

// Java program to demonstrate
// entrySet() method.
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // Create a properties and add some values
        Properties properties = new Properties();
        properties.put("Pen", 10);
        properties.put("Book", 500);
        properties.put("Clothes", 400);
        properties.put("Mobile", 5000);
  
        // Print Properties details
        System.out.println("Current Properties: "
                           + properties.toString());
  
        // Using entrySet() to get the set view
        System.out.println("The set is: "
                           + properties.entrySet());
    }
}
输出:
Current Properties: {Book=500, Mobile=5000, Pen=10, Clothes=400}
The set is: [Book=500, Mobile=5000, Pen=10, Clothes=400]

方案二:

// Java program to demonstrate
// entrySet() method.
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // Create a properties and add some values
        Properties properties = new Properties();
  
        properties.put(1, "100RS");
        properties.put(2, "500RS");
        properties.put(3, "1000RS");
  
        // print Properties details
        System.out.println("Current Properties: "
                           + properties.toString());
  
        // Using entrySet() to get the set view
        System.out.println("The set is: "
                           + properties.entrySet());
    }
}
输出:
Current Properties: {3=1000RS, 2=500RS, 1=100RS}
The set is: [3=1000RS, 2=500RS, 1=100RS]

参考资料:https://docs.oracle.com/javase/9/docs/api/ Java/util/Properties.html#entrySet–