下面列出了java.util.HashMap#clone ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws Exception {
HashMap<Integer, Object> hm = makeMap();
hm = (HashMap<Integer, Object>)hm.clone();
hm.clear();
// There should no longer be a strong reference to testObject
// the WeakReference should be nulled out by GC. If not,
// we will hang here until timed out by the test harness.
Object[] chain = null;
while (wr.get() != null) {
try {
Object[] allocate = new Object[1000000];
allocate[0] = chain;
chain = allocate;
} catch (OutOfMemoryError oome) {
chain = null;
}
System.gc();
Thread.sleep(100);
}
}
public static void main(String[] args) throws Exception {
HashMap<Integer, Object> hm = makeMap();
hm = (HashMap<Integer, Object>)hm.clone();
hm.clear();
// There should no longer be a strong reference to testObject
// the WeakReference should be nulled out by GC. If not,
// we will hang here until timed out by the test harness.
Object[] chain = null;
while (wr.get() != null) {
try {
Object[] allocate = new Object[1000000];
allocate[0] = chain;
chain = allocate;
} catch (OutOfMemoryError oome) {
chain = null;
}
System.gc();
Thread.sleep(100);
}
}
/** Creates deep copy of Map.
* All items will be cloned. Used internally in this object.
*/
private HashMap deepClone(HashMap map) {
HashMap newone = (HashMap)map.clone();
Iterator it = newone.keySet().iterator();
while (it.hasNext()) {
Object newkey = it.next();
Object deepobj = null, newobj = newone.get(newkey);
if (newobj instanceof HashMap)
deepobj = deepClone((HashMap)newobj);
else if (newobj instanceof String)
deepobj = (Object)new String((String)newobj);
else if (newobj instanceof Vector)
deepobj = ((Vector)newobj).clone();
newone.put(newkey, deepobj);
}
return newone;
}
public static void main(String[] args) throws Exception {
HashMap<Integer, Object> hm = makeMap();
hm = (HashMap<Integer, Object>)hm.clone();
hm.clear();
// There should no longer be a strong reference to testObject
// the WeakReference should be nulled out by GC. If not,
// we will hang here until timed out by the test harness.
Object[] chain = null;
while (wr.get() != null) {
try {
Object[] allocate = new Object[1000000];
allocate[0] = chain;
chain = allocate;
} catch (OutOfMemoryError oome) {
chain = null;
}
System.gc();
Thread.sleep(100);
}
}
/**
* Creates a new CharacterEntityParser and initializes the parser with the given set of entities.
*
* @param characterEntities the entities used for the parser
*/
public CharacterEntityParser( final HashMap characterEntities ) {
if ( characterEntities == null ) {
throw new NullPointerException( "CharacterEntities must not be null" );
}
entities = (HashMap) characterEntities.clone();
charMap = new String[ 65536 ];
final Iterator entries = entities.entrySet().iterator();
while ( entries.hasNext() ) {
final Map.Entry entry = (Map.Entry) entries.next();
final String value = (String) entry.getValue();
final String entityName = (String) entry.getKey();
if ( value.length() != 1 ) {
throw new IllegalStateException();
}
charMap[ value.charAt( 0 ) ] = entityName;
}
}
public static void main(String[] args) throws Exception {
HashMap<Integer, Object> hm = makeMap();
hm = (HashMap<Integer, Object>)hm.clone();
hm.clear();
// There should no longer be a strong reference to testObject
// the WeakReference should be nulled out by GC. If not,
// we will hang here until timed out by the test harness.
Object[] chain = null;
while (wr.get() != null) {
try {
Object[] allocate = new Object[1000000];
allocate[0] = chain;
chain = allocate;
} catch (OutOfMemoryError oome) {
chain = null;
}
System.gc();
Thread.sleep(100);
}
}
public static void main(String[] args) throws Exception {
HashMap<Integer, Object> hm = makeMap();
hm = (HashMap<Integer, Object>)hm.clone();
hm.clear();
// There should no longer be a strong reference to testObject
// the WeakReference should be nulled out by GC. If not,
// we will hang here until timed out by the test harness.
Object[] chain = null;
while (wr.get() != null) {
try {
Object[] allocate = new Object[1000000];
allocate[0] = chain;
chain = allocate;
} catch (OutOfMemoryError oome) {
chain = null;
}
System.gc();
Thread.sleep(100);
}
}
public static void main(String[] args) throws Exception {
HashMap<Integer, Object> hm = makeMap();
hm = (HashMap<Integer, Object>)hm.clone();
hm.clear();
// There should no longer be a strong reference to testObject
// the WeakReference should be nulled out by GC. If not,
// we will hang here until timed out by the test harness.
Object[] chain = null;
while (wr.get() != null) {
try {
Object[] allocate = new Object[1000000];
allocate[0] = chain;
chain = allocate;
} catch (OutOfMemoryError oome) {
chain = null;
}
System.gc();
Thread.sleep(100);
}
}
public static void main(String[] args) throws Exception {
HashMap<Integer, Object> hm = makeMap();
hm = (HashMap<Integer, Object>)hm.clone();
hm.clear();
// There should no longer be a strong reference to testObject
// the WeakReference should be nulled out by GC. If not,
// we will hang here until timed out by the test harness.
Object[] chain = null;
while (wr.get() != null) {
try {
Object[] allocate = new Object[1000000];
allocate[0] = chain;
chain = allocate;
} catch (OutOfMemoryError oome) {
chain = null;
}
System.gc();
Thread.sleep(100);
}
}
public static void main(String[] args) throws Exception {
HashMap<Integer, Object> hm = makeMap();
hm = (HashMap<Integer, Object>)hm.clone();
hm.clear();
// There should no longer be a strong reference to testObject
// the WeakReference should be nulled out by GC. If not,
// we will hang here until timed out by the test harness.
Object[] chain = null;
while (wr.get() != null) {
try {
Object[] allocate = new Object[1000000];
allocate[0] = chain;
chain = allocate;
} catch (OutOfMemoryError oome) {
chain = null;
}
System.gc();
Thread.sleep(100);
}
}
public SegmentationResult(List<CandidateCluster> cloneSegments,
HashMap<GenericSegment, CandidateCluster> cloneClustHash)
{
// clone segment list
segments = new ArrayList<CandidateCluster>();
for (CandidateCluster gs : cloneSegments)
segments.add(gs);
clustHash = (HashMap<GenericSegment, CandidateCluster>)
cloneClustHash.clone();
joinedEdges = new ArrayList<AdjacencyEdge<GenericSegment>>();
remainingEdges = new ArrayList<AdjacencyEdge<GenericSegment>>();
}
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<FicheEmploye>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this._listDataChildOriginal = new HashMap<String, List<FicheEmploye>>();
this._listDataHeaderOriginal = new ArrayList<String>();
this._listDataChildOriginal = (HashMap<String, List<FicheEmploye>>) listChildData.clone();
this._listDataHeaderOriginal.addAll(listDataHeader);
}
public void setSfiRecords(HashMap<Short, byte[]> records) {
try {
this.records = (HashMap<Short, byte[]>) records.clone();
this.records.putAll(records);
}
catch (Exception e) {
this.records = null;
}
}
private void monteCarloFloat() {
boolean firstRun = true;
generateMonteCarloInputs();
for (HashMap<ParentInput, Double> startValues : monteCarloInputsFloat) {
HashMap<Operation, Double> result = performMonteCarloIter((startValues));
monteCarloResultsFloat.add(result);
/* merge the fixpoint runs for value range analysis */
if (firstRun) {
/* not so easy, get the input min and max back into the first result */
HashMap<Operation, Double> tmp = (HashMap<Operation, Double>)result.clone();
tmp.putAll(maxValues);
maxValues = tmp;
tmp = ((HashMap<Operation, Double>) result.clone());
tmp.putAll(minValues);
minValues = tmp;
firstRun = false;
} else {
Util.merge(maxValues, result, Util.MAXMERGE);
Util.merge(minValues, result, Util.MINMERGE);
}
}
/*
for (Operation op: minValues.keySet()) {
System.out.println("Variable " + op.getDebugMessage() + " " + op.getDisplayName() +
" MC-Value Range: [" + minValues.get(op) + ", " +
maxValues.get(op) + "]");
} */
}
public static <String, Employee> HashMap<String, Employee> shallowCopy(HashMap<String, Employee> originalMap) {
return (HashMap<String, Employee>) originalMap.clone();
}
public static <String, Employee> HashMap<String, Employee> copyUsingClone(HashMap<String, Employee> originalMap) {
return (HashMap<String, Employee>) originalMap.clone();
}
private static void processId(
PropertyHolder propertyHolder,
PropertyData inferredData,
SimpleValue idValue,
HashMap<String, IdentifierGeneratorDefinition> classGenerators,
boolean isIdentifierMapper,
MetadataBuildingContext buildingContext) {
if ( isIdentifierMapper ) {
throw new AnnotationException(
"@IdClass class should not have @Id nor @EmbeddedId properties: "
+ BinderHelper.getPath( propertyHolder, inferredData )
);
}
XClass entityXClass = inferredData.getClassOrElement();
XProperty idXProperty = inferredData.getProperty();
//manage composite related metadata
//guess if its a component and find id data access (property, field etc)
final boolean isComponent = entityXClass.isAnnotationPresent( Embeddable.class )
|| idXProperty.isAnnotationPresent( EmbeddedId.class );
GeneratedValue generatedValue = idXProperty.getAnnotation( GeneratedValue.class );
String generatorType = generatedValue != null
? generatorType( generatedValue, buildingContext, entityXClass )
: "assigned";
String generatorName = generatedValue != null
? generatedValue.generator()
: BinderHelper.ANNOTATION_STRING_DEFAULT;
if ( isComponent ) {
//a component must not have any generator
generatorType = "assigned";
}
if ( isGlobalGeneratorNameGlobal( buildingContext ) ) {
buildGenerators( idXProperty, buildingContext );
SecondPass secondPass = new IdGeneratorResolverSecondPass(
idValue,
idXProperty,
generatorType,
generatorName,
buildingContext
);
buildingContext.getMetadataCollector().addSecondPass( secondPass );
}
else {
//clone classGenerator and override with local values
HashMap<String, IdentifierGeneratorDefinition> localGenerators = (HashMap<String, IdentifierGeneratorDefinition>) classGenerators
.clone();
localGenerators.putAll( buildGenerators( idXProperty, buildingContext ) );
BinderHelper.makeIdGenerator(
idValue,
idXProperty,
generatorType,
generatorName,
buildingContext,
localGenerators
);
}
if ( LOG.isTraceEnabled() ) {
LOG.tracev( "Bind {0} on {1}", ( isComponent ? "@EmbeddedId" : "@Id" ), inferredData.getPropertyName() );
}
}
void save() {
HashMap<String, Object> contextData = (HashMap<String, Object>) stack.get(stack.size() - 1);
HashMap<String, Object> clone = (HashMap<String, Object>) contextData.clone();
stack.add(clone);
}
@SuppressWarnings("unchecked")
public synchronized void setMetadata(String key,
HashMap<String, String> metadata) {
blobs.get(key).metadata = (HashMap<String, String>) metadata.clone();
}
/**
* java.util.HashMap#clone()
*/
public void test_clone() {
// Test for method java.lang.Object java.util.HashMap.clone()
HashMap hm2 = (HashMap) hm.clone();
assertTrue("Clone answered equivalent HashMap", hm2 != hm);
for (int counter = 0; counter < hmSize; counter++)
assertTrue("Clone answered unequal HashMap", hm
.get(objArray2[counter]) == hm2.get(objArray2[counter]));
HashMap map = new HashMap();
map.put("key", "value");
// get the keySet() and values() on the original Map
Set keys = map.keySet();
Collection values = map.values();
assertEquals("values() does not work",
"value", values.iterator().next());
assertEquals("keySet() does not work",
"key", keys.iterator().next());
AbstractMap map2 = (AbstractMap) map.clone();
map2.put("key", "value2");
Collection values2 = map2.values();
assertTrue("values() is identical", values2 != values);
// values() and keySet() on the cloned() map should be different
assertEquals("values() was not cloned",
"value2", values2.iterator().next());
map2.clear();
map2.put("key2", "value3");
Set key2 = map2.keySet();
assertTrue("keySet() is identical", key2 != keys);
assertEquals("keySet() was not cloned",
"key2", key2.iterator().next());
// regresion test for HARMONY-4603
HashMap hashmap = new HashMap();
MockClonable mock = new MockClonable(1);
hashmap.put(1, mock);
assertEquals(1, ((MockClonable) hashmap.get(1)).i);
HashMap hm3 = (HashMap) hashmap.clone();
assertEquals(1, ((MockClonable) hm3.get(1)).i);
mock.i = 0;
assertEquals(0, ((MockClonable) hashmap.get(1)).i);
assertEquals(0, ((MockClonable) hm3.get(1)).i);
}