Java地图<;整数,HashMap<;字符串,字符串>&燃气轮机;
我需要将结果集转换为地图地图。 我之所以不使用映射列表,是因为我不想遍历整个列表以获得特定的行 我现在面临的问题是,如果索引大于16,HashMap的条目将不再排序 现在,我尝试了以下简单测试:Java地图<;整数,HashMap<;字符串,字符串>&燃气轮机;,java,list,collections,map,hashmap,Java,List,Collections,Map,Hashmap,我需要将结果集转换为地图地图。 我之所以不使用映射列表,是因为我不想遍历整个列表以获得特定的行 我现在面临的问题是,如果索引大于16,HashMap的条目将不再排序 现在,我尝试了以下简单测试: public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer, String>(); //creating 20 rows from 1
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
//creating 20 rows from 1 to 20
for (int i = 1; i <= 20; i++){
map.put(i, "ROW "+i);
}
//returning all the rows the map contains. Look at position 16.
for(int key : map.keySet()){
System.out.println(key);
}
}
publicstaticvoidmain(字符串[]args){
Map Map=newhashmap();
//从1到20创建20行
对于(int i=1;iAHashMap
未排序或排序。如果需要按顺序插入Map
请使用java.util.LinkedHashMap
。
如果需要按键排序的Map
,请使用SortedMap
,例如java.util.TreeMap
aHashMap
未排序或排序。如果需要按插入顺序排序的Map
请使用java.util.LinkedHashMap
。
如果您需要一个按键排序的映射
,请使用SortedMap
,例如java.util.TreeMap
您可以使用a来保持插入顺序,因为基本映射不保证键的顺序。您可以使用a来保持插入顺序,因为基本映射不保证键的顺序。您应该d根据您希望元素分别按自然顺序或插入顺序排序,使用或代替哈希映射
HashMap不保证键的顺序,我引用的其他两个Map实现也保证
树映射将根据键的自然顺序对键进行排序,这意味着键必须实现Compariable,或者您必须提供自己的比较器来确定顺序
LinkedHashMap将根据插入密钥的时间对密钥进行排序
由于您要寻找的是自然排序的键,而您恰好是自己按顺序插入键,因此这两种实现都适用于这种情况。您应该使用或而不是哈希映射,这取决于您希望元素分别按自然顺序或插入顺序排序
HashMap不保证键的顺序,我引用的其他两个Map实现也保证
树映射将根据键的自然顺序对键进行排序,这意味着键必须实现Compariable,或者您必须提供自己的比较器来确定顺序
LinkedHashMap将根据插入密钥的时间对密钥进行排序
因为您要寻找的是自然排序的键,而您恰好是自己按顺序插入键,所以这两种实现都适用于这种情况。使用TreeMap而不是HashMap如何。您甚至可以提供自己的排序。使用TreeMap而不是HashMap如何。您可以提供ev你自己点菜吧
我之所以不使用映射列表,是因为我不想遍历整个列表以获得特定的行
因此,将其设置为ArrayList
而不是LinkedList
。这样,list.get(1234)
将直接进入该条目——它不会在其前面的1234条条目上迭代
我之所以不使用映射列表,是因为我不想遍历整个列表以获得特定的行
因此,将其设置为ArrayList
而不是LinkedList
。这样,list.get(1234)
将直接进入该条目——它不会在它前面的1234条条目上迭代。我认为您面临的问题是因为HashMap的默认大小
根据标准java文档
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 16;
/**
* The load factor used when none specified in constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
table = new Entry[DEFAULT_INITIAL_CAPACITY];
init();
}
/**
*默认初始容量-必须是2的幂。
*/
静态最终int默认值初始容量=16;
/**
*构造函数中未指定时使用的负载因子。
*/
静态最终浮动默认荷载系数=0.75f;
/**
*构造一个具有默认初始容量的空哈希映射
*(16)和默认荷载系数(0.75)。
*/
公共HashMap(){
this.loadFactor=默认加载系数;
阈值=(int)(默认初始容量*默认负载系数);
表=新条目[默认初始容量];
init();
}
所以,在第16个元素之后,您没有得到正确的输出
据JavaDoc说,
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* capacity and the default load factor (0.75).
*
* @param initialCapacity the initial capacity.
* @throws IllegalArgumentException if the initial capacity is negative.
*/
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
/**
*构造具有指定初始值的空哈希映射
*容量和默认负载系数(0.75)。
*
*@param initialCapacity初始容量。
*如果初始容量为负,@将引发IllegalArgumentException。
*/
公共哈希映射(int initialCapacity){
这(初始容量、默认负载系数);
}
尝试以下方法(具有初始容量的参数化构造函数)
Map-Map=newhashmap(32);
我认为您面临的问题是因为HashMap的默认大小
根据标准java文档
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 16;
/**
* The load factor used when none specified in constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
table = new Entry[DEFAULT_INITIAL_CAPACITY];
init();
}
/**
*默认初始容量-必须是2的幂。
*/
静态最终int默认值初始容量=16;
/**
*构造函数中未指定时使用的负载因子。
*/
静态最终浮动默认荷载系数=0.75f;
/**
*构造一个具有默认初始容量的空哈希映射
*(16)和默认荷载系数(0.75)。
*/
公共HashMap(){
this.loadFactor=默认加载系数;
阈值=(int)(默认初始容量*默认负载系数);
表=新条目[默认初始容量];
init();
}
所以,在第16个元素之后,您没有得到正确的输出
据JavaDoc说,
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* capacity and the default load factor (0.75).
*
* @param initialCapacity the initial capacity.
* @throws IllegalArgumentException if the initial capacity is negative.
*/
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
/**
*构造具有指定初始值的空哈希映射
*容量和默认负载系数(0.75)。
*
*@param initialCapacity初始容量。
*如果初始容量为负,@将引发IllegalArgumentException。
*/
公共哈希映射(int initialCapacity){
这(初始容量、默认负载系数);
}
tr