-1

i want to put another string as the key of the lastEntry of the Hash map.

after check the value of the lastEntry and change it i couldnt use setKey or another func to change the key i want to change the lastEntry.

EditText name; // put that as the key
Map<String,Integer> Names = new HashMap<String,Integer>(10);
// .
// .
// .
name = (EditText) findViewById(R.id.namep);

my func that check the value of the lastEntry and change it:

boolean flagScore = false;
Entry<String, Integer> lastEntry = Names.lastEntry();
if(lastEntry.getValue() < score){
    lastEntry.setValue(score);
    flagScore = true;
}
if(flagScore){
    // put name.getText().toString as key
}
Y123
  • 915
  • 13
  • 30
Adi
  • 27
  • 8

2 Answers2

0

You can use Model class and ArrayList to achieve this. For example

Take a class like this

public class Demo {
  private String name; 
  private integer value;

  //create getters and setters for this variables. 
}

And in your class create array list like this.

private ArrayList<Demo> namesList  = new ArrayList<Demo>;

create object for model class Demo demo = new Demo;

demo.name = "";
demo.value = 1;

Then add model class to ArrayList like this namesList.add(demo);

now you can do what ever you want with ArrayList.

Christian Abella
  • 5,747
  • 2
  • 30
  • 42
gkondati
  • 516
  • 4
  • 16
  • ok but when i got 10 elements in my arraylist and then i want to check agian the last value (your code, The Demo is one object how i can divide it to value and key?) – Adi Apr 23 '15 at 22:25
  • @Adi String name = demo.name; int v = demo.value; – Alex Salauyou Apr 23 '15 at 22:43
  • You can get last value like this nameList.get(nameList.size()-1).getName(); //take this is the key nameList.get(nameList.size()-1).getValue();//take this is the key – gkondati Apr 23 '15 at 22:44
0

This can be implemented by using LinkedHashMap also written a small program, hope it will help.

public class HashMapIssue {
public static void main(String[] args) {
    Map<String,Integer> map=new LinkedHashMap<String, Integer>();
    map.put("Integer2", 1);
    map.put("Integer1", 2);
    map.put("Integer3", 3);
    map.put("Integer4", 4);
    map.put("Integer5", 5);
    System.out.println(map);

    Set  enteries = map.entrySet();
    Iterator it = enteries.iterator();
    String lastkey="";
    Entry entry;
    while(it.hasNext()){
        entry=(Entry) it.next();
        lastkey=(String) entry.getKey();
    }
map.remove(lastkey);
map.put("modifiedkey",0);   
System.out.println(map);                 
   }
}

Output is:

{Integer2=1, Integer1=2, Integer3=3, Integer4=4, Integer5=5}
{Integer2=1, Integer1=2, Integer3=3, Integer4=4, modifiedkey=0}
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91