2015-08-20 46 views
0

我TreeSet的<>從一個ArrayList中獲得獨特的價值觀,並建立所有的獨特的價值觀,但得到這樣的:TreeSet中定製Android中Java的

Set<String> uniqueCategories; // declared globally 

Checkout checkOut; 
String strCategories = null; 

....... 

public void getDataFromDatabase() { 

    ArrayList<Checkout> detailArrayListFromDB = serviceDatabase.getCheckOut(); 
    uniqueCategories = new TreeSet<>(); 

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

     String strCategory = detailArrayListFromDB.get(i).getCategory(); 

     checkOut = new Checkout(); 
     checkOut.setCategory(strCategory);   
     checkOutArrayList.add(checkOut);  

     uniqueCategories.add(checkOut.getCategory());  
    } 

    // name of unique categories 
    strCategories = uniqueCategories.toString(); 
    System.out.println(strCategories); 
    Log.d("categories:", strCategories);   
} 

[Cars, Bikes] 

我這樣試過

但我想它是這樣的:Cars, Bikes

還有一件事,我該怎麼讓上面這樣的字符串:

Cars 

Bikes 
+0

這似乎更像是一個格式化你的代碼輸出 – SacJn

回答

2

您可以編寫自己的toString

class MyTreeSet extends TreeSet<String> { 

    public String toString() { 
     StringBuilder asString = new StringBuilder(); 
     for (String s : this) { 
      if (asString.length() > 0) { 
       asString.append("\n"); 
      } 
      asString.append(s); 
     } 
     return asString.toString(); 
    } 
} 
+1

我是你如何能幫助奧利奧深刻的印象......我沒能懂得這個問題...偉大的工作! –