Frage zu TreeMap

Nali_karamba

Mitglied
Hallo,

ich hab eine kleine TreeMap geschrieben diese hat als Key einen String und als value einen Vector.

klappt auch alles wunderbar, folgendes Problem habe ich nur.

die Keys sehen so aus 1/1 , 1/2 , 1/3 , 1/4 , 1/5 (besteht also aus 2 Integern)

solange wie mit einstelligen Integern gearbeitet wird ist das alles kein Problem.

aber sobald 10/1 eingefügt wird stimmt es mit den Keys nicht mehr dann sieht das ganze so aus --->

1/1
1/2
1/3
10/1
2/1
2/2
.
.
.
9/8

d.h. mit TreeMap.lastKey() bekommt man immer --> 9/8 man sollte aber 10/1 bekommen. -.-

jemand ne Idee !? vielleicht ein nen anderen Key nehmen !?


Lg Jens
 
Da du einen String als Key nimmst wird der nicht nach dem Integer Wert sondern alphabetisch sortiert. Musst du also einen eigenen Comparator schreiben oder eine eigene Key-Klasse die Comparable implementiert.

Kleines Beispiel (weil mir grade langweilig war):
Java:
import java.util.TreeMap;


public class Test {
	public static void main(String[] args){
		TreeMap<Key,Object> test = new TreeMap<Key,Object>();

		test.put(new Key(1,1), new Object());
		test.put(new Key(1,2), new Object());
		test.put(new Key(1,3), new Object());
		test.put(new Key(10,1), new Object());
		test.put(new Key(10,2), new Object());
		test.put(new Key(10,3), new Object());
		test.put(new Key(9,1), new Object());
		test.put(new Key(9,2), new Object());
		test.put(new Key(9,3), new Object());
		System.out.println(test.firstKey()); // 1/1
		System.out.println(test.lastKey()); // 10/3
	}

}

class Key implements Comparable<Key>{

	Integer firstInt;
	Integer secondInt;
	
	public Key(Integer firstInt, Integer secondInt){
		this.firstInt = firstInt;
		this.secondInt = secondInt;
	}
	
	
	public Integer getFirstInt() {
		return firstInt;
	}


	public Integer getSecondInt() {
		return secondInt;
	}

	@Override
    public String toString(){
    	return firstInt + "/" + secondInt;
    }
	@Override
	public int compareTo(Key key) {
		if(firstInt.compareTo(key.getFirstInt()) == 0){
			return secondInt.compareTo(key.getSecondInt());
		} else {
			return firstInt.compareTo(key.getFirstInt());
		}	
	}
}
 
Zuletzt bearbeitet von einem Moderator:
Hey Danke für deine Hilfe 🙂

Mir war schon klar das er String Alphabetisch Ordnet deswegen war meine Idee dann auch führende nullen einzusetzen.
Und da ich eh schon ein Object hatte was aus 2 int's besteht (diese Object hat die Methode toString() welche ich dann als Key benutzt habe) wäre das auch nur eine weitere Methode gewesen.

Aber durch deine Idee kann ich jetzt mein Object direkt als Key benutzen was 1000mal geiler ist.

hier meine Lösung dazu ---->

Java:
package lm.model.DefaultVector;

import java.util.ArrayList;
import lm.Componet.Vector.LMArableID;


public class ArableID implements LMArableID,Comparable<ArableID> {

    private Integer ID;
    private Integer Stufe;
    private int maxStufe;


    public ArableID(){
        this.ID=0;
        this.Stufe=0;
        this.maxStufe=0;
    }

    public ArableID(int ID , int Stufe){
        this.ID=ID;
        this.Stufe=Stufe;
    }

    public ArableID(ArrayList<String> a){
        this.ID=Integer.parseInt(a.get(0));
        this.Stufe=Integer.parseInt(a.get(1));
        this.maxStufe=Integer.parseInt(a.get(2));
    }
    public ArableID(int ID , int Stufe , int maxStufe){
        this.ID=ID;
        this.Stufe=Stufe;
        this.maxStufe=maxStufe;
    }

    public Boolean isEmpty() {
        if(this.ID==0){
            return true;
        }else{
            return false;
        }
    }

    public Boolean isLast() {
        if (this.Stufe==this.maxStufe){
            return true;
        }else{
            return false;
        }
    }
    public Boolean CeckIfCorrect() {
        throw new UnsupportedOperationException(java.util.ResourceBundle.getBundle("ressources/Ressources").getString("NOT SUPPORTED YET."));
    }

    //Getter And Setter MEthods
    //Getter And Setter ------>ID
    public void setID(int i) {
        this.ID=i;
    }
    public int getID() {
        return this.ID;
    }
    //Getter And Setter ------>Stufe
    public void setStufe(int i) {
        this.Stufe=i;
    }
    public int getStufe() {
        return this.Stufe;
    }
    //Getter And Setter ------>MaxStufe
    public void setMaxStufe(int i) {
        this.maxStufe=i;
    }
    public int getMaxStufe() {
        return this.maxStufe;
    }

    public String getCompleteID() {
        String s=java.util.ResourceBundle.getBundle("ressources/Ressources").getString("");
        s=s+ID;
        s=s+java.util.ResourceBundle.getBundle("ressources/Ressources").getString("/");
        s=s+Stufe;
        return s;
    }

    public int compareTo(ArableID t) {
        if(ID.compareTo(t.ID) == 0){
            return Stufe.compareTo(t.getStufe());
        } else {
            return ID.compareTo(t.getID());
        }

    }
}


Anmerkungen !?
 
Generell zum Code:
- du übertreibst es etwas mit
Code:
this
, fast überall kannst du es weglassen.
- [JAVA=44]
public Boolean isLast() {
if (this.Stufe==this.maxStufe){
return true;
}else{
return false;
}
}
//ist nicht anderes als:
public Boolean isLast() {
return this.Stufe==this.maxStufe;
}
[/code]
- Die Konstruktoren kannst du verschachteln, dann musst du nicht ständig das gleich hinschreiben:
Java:
public ArableID(){
  this(0,0,0);
}
public ArableID(int ID , int Stufe){
  this(ID, Stufe, 0)
}
public ArableID(int ID , int Stufe , int maxStufe){
  this.ID=ID;
  this.Stufe=Stufe;
  this.maxStufe=maxStufe;
}
...
- Variablen werden inCamelCase geschrieben
 

Neue Themen


Zurück
Oben