Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

deep copy für Hashtable

Empfohlene Antworten

Veröffentlicht

Hallo,

kennt jemand zufällig eine einfach Möglichkeit eine "deep copy" für eine Hashtable zu machen??

Denn über folg. Sourcecode erhält man ja keine deep copy:

Hashtable newHashtable = new Hashtable();

newHashtable.putAll(hashtableOldValues);

Gruss/Regards

Ich wüsste keine API-Lösung, da Du dafür ja sicherstellen müsstest, das Deine Keys und Values Clonable sind.

Du kannst Dir aber recht einfach eine Lösung bauen, indem Du Deine eigene Collection definierst, die intern mit einer Hashtable arbeitet, aber nur Cloneable Objekte übernimmt.

Dann kannst Du in der Methode clone() Deiner Collection über das keySet iterieren, zu jedem Key das Value ermitteln, beide Klonen und in die neue Hashtable schreiben.

Peter

Hi,

das geht mit:

hashtable.clone();

Voraussetzung ist allerdings das alle Objekte und Unterobjekte in der Hashtable das Interface Clonable implementieren und die Funktion clone() korrekt überschreiben.

Gruß Jaraz

Servus Jaraz,

das hatte ich erst auch gedacht, aber die API sagt zu Hashtable#clone():

Creates a shallow copy of this hashtable. All the structure of the hashtable itself is copied, but the keys and values are not cloned. This is a relatively expensive operation.

Peter

das hatte ich erst auch gedacht, aber die API sagt zu Hashtable#clone():

Creates a shallow copy of this hashtable. All the structure of the hashtable itself is copied, but the keys and values are not cloned. This is a relatively expensive operation.

Skandal! ;)

Quick and Dirty Workaround!

Alles Serialisierbar machen und durch nen Stream jagen.

Gruß Jaraz

  • 1 Monat später...

Hi,

Eine Liste, die garantiert eine tiefe Kopie ALLER enthaltenen Objekte macht, egal was drin liegt? CoOL

Ich stell mir dann immer Listen vor, in denen Listen sind, die Listen mit Listen speichern MUAAHAA

Oder was ähnliches...

Übel wird diese Tiefenkopie allerdings, wenn in der Letzen Liste eine Referenz auf die Ursprüngliche Liste gespeichert ist. Dann läuft man ins Nirvana. Ein Teufelskreis ;)

Also sind diese generischen "All-in-Wonder" - Tiefenkopien eigentlich nicht so sinnvoll... finde ich jedenfalls...

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.