d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Hashset/hashmap Question
Add Reply New Topic New Poll
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Jun 25 2019 08:00pm
Code
package com.example.test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Main {
private static Map<String, HeavenlyBody> solarSystem = new HashMap<>();
private static Set<HeavenlyBody> planets = new HashSet<>();

public static void main(String[] args) {

HeavenlyBody temp = new HeavenlyBody("Mercury", 88);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

temp = new HeavenlyBody("Venus", 225);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

temp = new HeavenlyBody("Earth", 365);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

HeavenlyBody tempMoon = new HeavenlyBody("Moon", 27);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon);

temp = new HeavenlyBody("Mars", 687);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

tempMoon = new HeavenlyBody("Deimos", 1.3);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Mars

tempMoon = new HeavenlyBody("Phobos", 0.3);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Mars

temp = new HeavenlyBody("Jupiter", 4332);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

tempMoon = new HeavenlyBody("Io", 1.8);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Jupiter

tempMoon = new HeavenlyBody("Europa", 3.5);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Jupiter

tempMoon = new HeavenlyBody("Ganymede", 7.1);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Jupiter

tempMoon = new HeavenlyBody("Callisto", 16.7);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Jupiter

temp = new HeavenlyBody("Saturn", 10759);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

temp = new HeavenlyBody("Uranus", 30660);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

temp = new HeavenlyBody("Neptune", 165);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

temp = new HeavenlyBody("Pluto", 248);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

System.out.println("Planets");
for (HeavenlyBody planet : planets) {
System.out.println("\t" + planet.getName());
}

HeavenlyBody body = solarSystem.get("Mars");
System.out.println("Moons of " + body.getName());
for (HeavenlyBody jupiterMoon : body.getSatellites()) {
System.out.println("\t" + jupiterMoon.getName());
}

Set<HeavenlyBody> moons = new HashSet<>();
for (HeavenlyBody planet : planets) {
moons.addAll(planet.getSatellites());
}

System.out.println("All Moons");
for (HeavenlyBody moon : moons) {
System.out.println("\t" + moon.getName());
}


}
}








Code
package com.example.test;

import java.util.HashSet;
import java.util.Set;

public final class HeavenlyBody {
private final String name;
private final double orbitalPeriod;
private final Set<HeavenlyBody> satellites;

public HeavenlyBody(String name, double orbitalPeriod) {
this.name = name;
this.orbitalPeriod = orbitalPeriod;
this.satellites = new HashSet<>();
}

public String getName() {
return name;
}

public double getOrbitalPeriod() {
return orbitalPeriod;
}

public boolean addMoon(HeavenlyBody moon) {
return this.satellites.add(moon);
}

public Set<HeavenlyBody> getSatellites() {
return new HashSet<>(this.satellites);
}
}






So i understand how it prints all the plants... but what i'm confused about is how it's getting the moons for mars specifically, how it's adding moons to mars, doens't make sense to me


i also understand how it prints all moons at the end, but i don't get how it's adding moons to mars specifically
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 25 2019 08:47pm
would probably help if you stopped naming your variables "temp" and "tempMoon"
Code

temp = new HeavenlyBody("Mars", 687);
solarSystem.put(temp.getName(), temp);
planets.add(temp);

tempMoon = new HeavenlyBody("Deimos", 1.3);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Mars



renaming:
Code

mars= new HeavenlyBody("Mars", 687);
solarSystem.put(mars.getName(), mars);
planets.add(mars);

moonForMars = new HeavenlyBody("Deimos", 1.3);
solarSystem.put(moonForMars .getName(), moonForMars );
mars.addMoon(moonForMars ); // temp is still Mars



removing some of the code you're fine with:
Code

mars= new HeavenlyBody("Mars", 687);
moonForMars = new HeavenlyBody("Deimos", 1.3);
mars.addMoon(moonForMars ); // temp is still Mars



which part here doesn't make sense to you?
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Jun 25 2019 08:55pm
Hmm renaming it definitely makes it easier to understand ty
but i'm still confused about a few things:


Code
solarSystem.put(mars.getName(), mars);


^the 2nd parameter, what's happening there?... i see it puts the name for first parameter, but what is it setting value to in 2nd parameter for hashmap?



also i'm not sure what this is for:

Code
HeavenlyBody tempMoon = new HeavenlyBody("Moon", 27);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon);


/edit ^It's for printing "moon" when it prints all moons? not sure why he'd do this.....








/edit other then these 2 things, i think i get it completely now ty for making sense of it for me :)

This post was edited by ferf on Jun 25 2019 09:09pm
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Jun 26 2019 06:02pm
Quote (ferf @ 26 Jun 2019 04:55)
Hmm renaming it definitely makes it easier to understand ty
but i'm still confused about a few things:


Code
solarSystem.put(mars.getName(), mars);


^the 2nd parameter, what's happening there?... i see it puts the name for first parameter, but what is it setting value to in 2nd parameter for hashmap?


A hashmap is a mapping structure.
You are mapping from the string "Mars" to the HeavenlyBody object with that name, and an orbital period of 687.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll