HashMap<String, List<ArrayList>> baza = new HashMap<>(); 

It is necessary to add an object called street_num to the baza array by key and индСксу[0] , and then remove the last index from the key

Now using the method with an intermediate variable.

But I would like to directly add an object to the database and remove the extra one without creating an intermediate variable.

 raion = baza.get("key" + num_raion); raion.add(0, street_num); raion.remove(raion.size() -1); baza.put("key" + num_raion, raion); raion.clear(); // "key" + num_raion - ΠΊΠ»ΡŽΡ‡ для hashmap // street_num - ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π½Π°Π΄ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΏΠΎ ΠΏΡ€ΠΈΠ½Ρ†ΠΈΠΏΡƒ ΠΊΠ»ΡŽΡ‡.индСкс // послСднСС Π½Π°Π΄ΠΎ ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ Π² ΠΊΠ»ΡŽΡ‡Π΅ послСдний индСкс 
  • If you delete the last index in the key, then it will cease to be a key. - Roman C

1 answer 1

 baza.computeIfPresent("key" + num_raion, (k, v) -> { v.add(0, street_num); v.remove(v.size() - 1); }); 

And maybe the linked list was better. At the very least, it would save ugly constructions v.add(0, ...) and v.remove(v.size() - 1) .