0

我正在尝试将 csv 文件中的项目放入哈希图中。通过添加项目,我的意思是对它们进行分类。csv 充满了问题列表中的答案。所以csv是这样建立的:

Person     Question
-          Do you wear glasses

1          Yes
2          Yes
3          No
4          Sometimes

我想做的下一步是读取项目并将答案放入哈希图中,键是答案的名称,值是特定答案的数量。

hashmap 应该与我之前给出的示例类似:

Yes, 2
No, 1
Sometimes, 1

我现在像这样循环:

for (int j = 0; j < dataPersonList.size(); j++) 
{ 
    // looping through the csv
    while (t.hasNext ()) 
    { 
        // looping through the hashmap
        Map.Entry me = (Map.Entry)t.next();

        if (dataPersonVar.glasses.equals(me.getKey())) 
        { 
            // if the item in the csv is the same as the item in the hashmap
            hm.put(me.getKey(), me.getValue() =+ 1); // the value gets +1
        }
        else 
        {
            // a new item is made and gets automatically 1
            hm.put(dataPersonVar.glasses,1); 
        }
    }
}

但显然这行不通。我是哈希图的初学者,但我真的认为哈希图是解决我问题的方法。我在处理工作。有人知道如何解决这个问题吗?如果我不够清楚,请告诉我!

提前致谢

编辑:

根据要求,我特此发布完整代码:

    import java.util.Iterator;
    import java.util.Map;

    HashMap hm = new HashMap();

    import de.bezier.data.*;
    int totalParticipants;
    int bgWidth = 1000;
    int bgHeight = 1000;
    int x;
    int eSize = 10;
    int opacity = 100;
    float xLocation;
    float yLocation;
    String travelType;
    int travelTime;
    int border=400;

    float dataMinLat=1000;
    float dataMaxLat;
    float dataMinLon=1000;
    float dataMaxLon;
    float xLocationMM = (5.17880);
    float yLocationMM = (52.22541);
    ArrayList dataPersonList = new ArrayList();
    ArrayList dataFunctionList = new ArrayList(100);
    Point[] pointList = new Point[1000];
    float angle;


    XlsReader reader;

    PImage kaart;

    void setup ()
    {

      textSize(10);
      size(bgWidth, bgHeight);
      background(0);
      noStroke();
      smooth();

      //kaart = loadImage("map.png");

      yLocationMM = 0 - yLocationMM;
      reader = new XlsReader( this, "MMdata.xls" );    // assumes file to be in the data folder
      totalParticipants = reader.getLastRowNum();


      for (int i = 1;i < totalParticipants+1; i++) {
        DataPerson dataPerson = new DataPerson();

        dataPerson.function = reader.getString(i, 23);
        dataPerson.firstName = reader.getString(i, 1);
        dataPerson.lastName = reader.getString(i, 2);
        dataPerson.glasses = reader.getString(i, 20);
        dataPerson.longitude = reader.getFloat(i, 55);
        dataPerson.latitude = reader.getFloat(i, 54);
        dataPerson.location = reader.getString(i, 8);
        dataPerson.ownage = reader.getString(i, 14);
        dataPerson.traveltime = reader.getInt(i, 31);
        dataPerson.establishment = reader.getString(i, 58);
        dataPerson.traveltype = reader.getString(i, 17);

        dataPersonList.add(dataPerson);
      }

      for (int i = 0; i < dataPersonList.size(); i++) {
        DataPerson person = (DataPerson) dataPersonList.get(i);
        for (int j = 0; j < dataFunctionList.size() + 1; j++) {

          DataFunction dataFunction = null;
          if (j < dataFunctionList.size())
          {
            dataFunction = (DataFunction) dataFunctionList.get(j);
          }
          if (dataFunction != null) {
            if (person.function.equals(dataFunction.function)) {

              dataFunction.persons.add(person);
              dataFunction.amount ++;
              break;
            }
          }
          else {
            dataFunction = new DataFunction();
            dataFunction.function = person.function;
            dataFunction.amount = 1;
            dataFunction.persons.add(person);
            dataFunctionList.add(dataFunction);
            break;
          }
        }
      }

      for (int i = 0; i < dataPersonList.size(); i++) {

        DataPerson dataPersonVar = (DataPerson) dataPersonList.get(i);
        if (dataPersonVar.longitude > dataMaxLon) {
          dataMaxLon = dataPersonVar.longitude;
        }
        else if (dataPersonVar.longitude < dataMinLon) {
          dataMinLon = dataPersonVar.longitude;
        }
        if (dataPersonVar.latitude > dataMaxLat) {
          dataMaxLat = dataPersonVar.latitude;
        }
        else if (dataPersonVar.latitude < dataMinLat) {
          dataMinLat = dataPersonVar.latitude;
        }
      }
    }

    class DataPerson
    {
      String function;
      String firstName;
      String lastName;
      String glasses;
      float longitude;
      float latitude;
      String location;
      String ownage;
      int traveltime;
      String establishment;
      String traveltype;

      String fullName() 
      {
        return firstName + " " + lastName;
      }
    }

    class DataFunction
    {
      String function;
      int amount;
      ArrayList persons = new ArrayList();

      String getPersonNames()
      {
        String output = "";
        for (int i = 0; i < persons.size(); i++) {
          DataPerson person = (DataPerson) persons.get(i);
          output += person.firstName + "(" + person.glasses + ")   ";
        }
        return output;
      }
    }

    class Point
    {
      float x;
      float y;
    }

    void draw()
    {
      background(0);
      strokeWeight(2);

      x=50;
      int xText=80;
      int procentGlasses;
      noStroke();
      pushMatrix();
      scale(1.40, 0.89);
      //image(kaart, -250, -360);
      popMatrix();
      for (int i = 0; i < dataPersonList.size(); i++) {

        DataPerson dataPersonVar = (DataPerson) dataPersonList.get(i);
        if (dataPersonVar != null) {
          xLocation = map(dataPersonVar.longitude, dataMinLon, dataMaxLon, border, bgWidth-border);
          yLocation = map(dataPersonVar.latitude, dataMinLat, dataMinLat+(dataMaxLon-dataMinLon), border, bgHeight-border);
          travelType = (dataPersonVar.traveltype);
          noFill();

          if (travelType.equals("By car")) {

            stroke(0, 0, 255, opacity+100);
          }
          else if (travelType.equals("By public transport")) {
            stroke(255, 0, 0, opacity);
          }
          else {
            stroke(255, opacity);
          }
          //text(dataPersonVar.firstName, xLocation, yLocation);
          noStroke();
          strokeWeight(4);
          stroke(100, 15);
          strokeWeight(2);
        }
      }

      for (int i = 0; i < dataPersonList.size(); i++) {
        DataPerson dataPersonVar = (DataPerson) dataPersonList.get(i);
        if (dataPersonVar != null) {
          xLocation = map(dataPersonVar.longitude, dataMinLon, dataMaxLon, border, bgWidth-200);
          yLocation = map(dataPersonVar.latitude, dataMinLat, dataMinLat+(dataMaxLon-dataMinLon), border, bgHeight-200);
          noStroke();
          fill(255, 200);
          ellipse(xLocation, yLocation, eSize, eSize);
          //println(dataPersonVar.glasses);
          Iterator t = hm.entrySet().iterator();  // Get an iterator

            for (int j = 0; j < dataPersonList.size(); j++) {

            while (t.hasNext ()) {
              Map.Entry me = (Map.Entry)t.next();
              println("die in de dataPersonshit is "+dataPersonVar.glasses+". En de andere waarde is "+me.getKey());
              if (dataPersonVar.glasses.equals(me.getKey())) {
                hm.put(me.getKey(), ((Integer)me.getValue()) + 1);
                }
              else {
                hm.put(dataPersonVar.glasses, 1);
                //println("YEAH");
              }
            }
          }

          /////////drawing the circle///////////////

          angle = ((PI*2)/15*1)/dataPersonList.size()*i;
          stroke(255, 100, 50, 50);
          noFill();
          strokeWeight(3);
          pointList[i] = new Point();
          pointList[i].x = bgWidth/2+sin(angle)*400; // de punt van x = 400 ( het middelpunt van de cirkel ) + sin((360 graden) / het totaal 
          pointList[i].y = bgHeight/2+cos(angle)*400;
          beginShape();
          vertex(xLocation, yLocation);
          quadraticVertex((xLocation+pointList[i].x)/2, yLocation, pointList[i].x, pointList[i].y);
          endShape();
        }
      }


    }

不幸的是,我不能给你 csv,但我希望代码至少有帮助。

4

1 回答 1

0

您实际上并不需要遍历地图来执行您似乎正在尝试执行的操作。这是一个与您描述的类似 csv 的示例:

import java.util.Map;
Map<String, Integer> answers = new HashMap<String,Integer>();
//String [] fileInput = loadStrings("question.csv");
String [] fileInput = new String [] {
  "1,Yes", "2,No", "3,Yes", "4,Sometimes", "5,Yes", "6,No", "7,No", "8,No"
};
for (int i = 0 ; i < fileInput.length; i++) {
  String [] line = split(fileInput[i],",");
  String newAnswer = line[1];
  if(!answers.containsKey(newAnswer)) answers.put(newAnswer,1);
  else answers.put(newAnswer,answers.get(newAnswer)+1);
}
println(answers);
于 2013-08-31T19:50:45.247 回答