KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > util > HashMapUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.util;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21
22 /**
23  * @author Anton Koinov (latest modification by $Author: matze $)
24  * @version $Revision: 1.3 $ $Date: 2004/10/13 11:51:01 $
25  */

26 public class HashMapUtils
27 {
28     //~ Constructors -------------------------------------------------------------------------------
29

30     protected HashMapUtils()
31     {
32         // block public access
33
}
34
35     //~ Methods ------------------------------------------------------------------------------------
36

37     /**
38      * Calculates initial capacity needed to hold <code>size</code> elements in
39      * a HashMap or Hashtable without forcing an expensive increase in internal
40      * capacity. Capacity is based on the default load factor of .75.
41      * <p>
42      * Usage: <code>Map map = new HashMap(HashMapUtils.calcCapacity(10));<code>
43      * </p>
44      * @param size the number of items that will be put into a HashMap
45      * @return initial capacity needed
46      */

47     public static final int calcCapacity(int size)
48     {
49         return ((size * 4) + 3) / 3;
50     }
51
52     /**
53      * Creates a new <code>HashMap</code> that has all of the elements
54      * of <code>map1</code> and <code>map2</code> (on key collision, the latter
55      * override the former).
56      *
57      * @param map1 the fist hashmap to merge
58      * @param map2 the second hashmap to merge
59      * @return new hashmap
60      */

61     public static HashMap JavaDoc merge(Map JavaDoc map1, Map JavaDoc map2)
62     {
63         HashMap JavaDoc retval = new HashMap JavaDoc(calcCapacity(map1.size() + map2.size()));
64
65         retval.putAll(map1);
66         retval.putAll(map2);
67
68         return retval;
69     }
70 }
71
Popular Tags