将两个hashsets连接在一起

时间:2015-12-01 16:37:53

标签: java hash java.util.scanner hashset databags

我的目标是创建一个像代码一样的数据库,我在其中添加行星和它们被找到的年份。一旦用户输入行星,它就会显示所有行星和它们被发现的年份,到目前为止我已创建了这个行星。

import java.util.HashSet;
import java.util.Scanner;
import java.lang.*;
public class sky {
  public static void main(String args[]) {
    Scanner in = new Scanner(System.in);

    // HashSet declaration
    HashSet < String > planet =
      new HashSet < String > ();

    // Adding elements to the HashSet
    planet.add("planet A");
    planet.add("planet B");
    planet.add("planet C");
    planet.add("planet D");
    planet.add("planet E");
    //Addition of duplicate elements will not show
    planet.add("planet A");

    System.out.println("enter the word");
    String input = in .next();


    int count = 0;
    for (int i = 0; i < input.length(); i++)

    {
      if (sky.contains(input.HashSet(i))) count++;
    }

    System.out.println("the planets are");
    System.out.println(count);


    HashSet < String > date =
      new HashSet < String > ();

    // Adding elements to the HashSet
    date.add("2001");
    date.add("2002");
    date.add("2003");
    date.add("2004");
    date.add("2005");



    //Displaying HashSet elements
    System.out.println(planet);
    System.out.println(date);

  }
}

错误存在

2 个答案:

答案 0 :(得分:1)

您需要的不是HashSet而是HashMap。看看下面的程序,并尝试了解这里到底发生了什么。我建议你在阅读之前阅读HashMap的工作原理。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class sky {

  private static Map<String, Integer> planetMap = new HashMap<String,Integer>();
  public static void main(String args[]) {
    populateDB();
    Scanner scanner = new Scanner(System.in);

    String planetName = scanner.nextLine();

    if(planetMap.get(planetName) != null) {
      System.out.println("The planet "+ planetName +" was found in "+ planetMap.get(planetName));
    }
    else {
      System.out.println("Invalid Planet Name");
    }

  }

  public static void populateDB() {


    planetMap.put("Earth", 1600);
    planetMap.put("Mars", 1500);
    planetMap.put("Jupiter", 1100);
    planetMap.put("Saturn", 1900);
    planetMap.put("Venus", 1300);
  }
}

答案 1 :(得分:1)

与@bluelurker类似,但在聊天中添加了一个位置字段

使用自定义Planet类而不是HashMap的String可以让您以有条理的方式存储关于每个Planet的更多数据

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class sky
{
  private static Map<String, Planet> planetMap = new HashMap<String, Planet>();

  public static void main(String args[])
  {
    populateDB();
    Scanner scanner = new Scanner(System.in);

    String planetName = scanner.nextLine();
    Planet planet = planetMap.get(planetName);

    if(planet != null)
      System.out.println("The planet " + planet.name + " was found in " + planet.date + " and is currently located " + planet.location + ".");
    else
      System.out.println("Invalid Planet Name");
  }

  private static class Planet
  {
    public final String name;
    public final int date;
    public final String location;

    public Planet(String n, int d, String l)
    {
      name = n;
      date = d;
      location = l;
    }
  }

  public static void populateDB()
  {
    planetMap.put("Earth", new Planet("Earth", 1600, "here"));
    planetMap.put("Mars", new Planet("Mars", 1500, "near us"));
    planetMap.put("Jupiter", new Planet("Jupiter", 1100, "over there"));
    planetMap.put("Saturn", new Planet("Saturn", 1900, "above us"));
    planetMap.put("Venus", new Planet("Venus", 1300, "in the sky"));
  }
}