当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java AbstractMap.SimpleEntry toString()用法及代码示例


AbstractMap.SimpleEntry <K,V>用于维护键和值条目。可以使用setValue方法更改该值。此类有助于构建自定义Map实现。

AbstractMap.SimpleEntry <K,V>的toString()方法返回此SimpleEntry映射条目的String表示形式。此方法返回此Map条目的键的字符串表示形式,后跟等号字符(“=”),然后返回此条目值的字符串表示形式。

用法:


public String toString()

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

返回值:此方法返回此映射条目的String表示形式。

以下示例程序旨在说明toString()方法:
程序1:

// Java program to demonstrate 
// AbstractMap.SimpleEntry.toString() method 
  
import java.util.*; 
import java.util.AbstractMap.SimpleEntry; 
  
public class GFG { 
  
    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    public static void main(String[] args) 
    { 
  
        // create a ArrayList of Map 
        ArrayList<AbstractMap 
                      .SimpleEntry<Integer, Integer> > 
            arrayList 
            = new ArrayList<AbstractMap 
                                .SimpleEntry<Integer, Integer> >(); 
  
        // add values 
        arrayList.add(new AbstractMap.SimpleEntry(0, 123)); 
        arrayList.add(new AbstractMap.SimpleEntry(1, 130)); 
        arrayList.add(new AbstractMap.SimpleEntry(2, 994)); 
  
        // print keys 
        for (int i = 0; i < arrayList.size(); i++) { 
  
            // get map from list 
            AbstractMap.SimpleEntry<Integer, Integer> 
                map 
                = arrayList.get(i); 
  
            // get representation of map using toString() 
            String value = map.toString(); 
  
            System.out.println(value); 
        } 
    } 
}
输出:
0=123
1=130
2=994

程序2:

// Java program to demonstrate 
// AbstractMap.SimpleEntry.toString() method 
  
import java.util.*; 
  
public class GFG { 
  
    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    public static void main(String[] args) 
    { 
  
        // create a ArrayList of Map 
        ArrayList<AbstractMap 
                      .SimpleEntry<String, String> > 
            arrayList 
            = new ArrayList<AbstractMap 
                                .SimpleEntry<String, String> >(); 
  
        // add values 
        arrayList.add(new AbstractMap 
                          .SimpleEntry(" 001AB ", " Emp 1")); 
        arrayList.add(new AbstractMap 
                          .SimpleEntry(" 011AC ", " Emp 2")); 
        arrayList.add(new AbstractMap 
                          .SimpleEntry(" 111AD ", " Emp 3")); 
        arrayList.add(new AbstractMap 
                          .SimpleEntry(" 101BE ", " Emp 4")); 
        arrayList.add(new AbstractMap 
                          .SimpleEntry(" 110CE ", " Emp 5")); 
  
        // print keys 
        for (int i = 0; i < arrayList.size(); i++) { 
  
            // get map from list 
            AbstractMap.SimpleEntry<String, String> 
                map 
                = arrayList.get(i); 
  
            // get representation of map using toString() 
            String value = map.toString(); 
  
            System.out.println(value); 
        } 
    } 
}
输出:
001AB = Emp 1
011AC = Emp 2
111AD = Emp 3
101BE = Emp 4
110CE = Emp 5

参考文献: https://docs.oracle.com/javase/10/docs/api/java/util/AbstractMap.SimpleEntry.html#toString()



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 AbstractMap.SimpleEntry toString() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。