Java中的 AbstractMap.SimpleEntry toString() 方法及示例
AbstractMap.SimpleEntry
AbstractMap.SimpleEntry
句法:
public String toString()
参数:此方法不接受任何内容。
返回值:此方法返回此映射条目的字符串表示形式。
下面的程序说明了 toString() 方法:
方案一:
// 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 >
arrayList
= new ArrayList >();
// 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
map
= arrayList.get(i);
// get representation of map using toString()
String value = map.toString();
System.out.println(value);
}
}
}
输出:
0=123
1=130
2=994
方案二:
// 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 >
arrayList
= new ArrayList >();
// 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
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: Java/util/AbstractMap.SimpleEntry.html#toString()