2012-02-08 83 views
0

我必須將多個字符串分組爲一個字符串進行比較。這個分組應該像Java OO範例一樣完成:一個「描述」子字符串的字符串。 例子:在Java中將多個字符串分組到一個通用字符串中

String one = "hammer"; 
String two = "screwdriver"; 
String three = "pliers"; 

現在讓我們假設你想 「形容」 他們:

String str = "tool" 

以上所有字符串是工具。現在,我的代碼有一串,二串,三串,並將它們變成「工具」。所以,例如,字符串1變成了工具,對於字符串2和字符串3來說也是一樣的。 如何「分類」他們? 另一個擴展的例子:

String one = "hammer" 
String two = "screwdriver"; 
String three = "pliers"; 
String four = "horse"; 
String five = "cat"; 
String six = "dog"; 

public void stringConverter(String str) 
{ 
    if ("string match to an animal") 
     str = "animal"; 
    if ("string match to a tool") 
     str = "tool"; 
} 

也許這是實現一個愚蠢的事情,但現在我沒有任何想法!謝謝!

編輯:我的團隊是有限的,我知道我只有貓,狗,馬,錘等...編輯2:很難表達我!它應該是這樣的:

Group Animal = {cat, dog, horse} 
Group Tools = {hammer, screwdriver} 

// methods to recognize to wich one of the two groups is categorizable 

該地圖是一個好主意,但它必須填寫在每個運行時。是不是有些東西像把它們直接寫入大括號一樣?它應該像枚舉一樣,但從未使用過!

+0

我不知道爲什麼你真的願意做這樣的事情,但只是將所有引用字符串添加到每個類別的集合中,然後搜索測試字符串的集合。 http://docs.oracle.com/javase/6/docs/api/java/util/Set.html – smp7d 2012-02-08 21:41:21

+0

編譯時間或運行時間?(編輯:一組值和相關描述 - 動態還是靜態?) – alphazero 2012-02-08 21:55:05

+0

這組值是靜態的,而不是運行時。 @ smp7d地圖很有用,但我必須填寫它們,並且每次都應該完成一次。 – Angelo 2012-02-08 22:04:37

回答

1

構建Map將字符串映射到其「類別」。

Map<String, String> category = new HashMap<String, String>(); 
category.put("hammer", "tool"); 
category.put("screwdriver", "tool"); 
category.put("horse", "animal"); 

然後您只需使用category.get(str)即可獲取該類別。

如果它們是靜態的,那麼最好的方法是使用Guava ImmutableMap,可能使用它的構建器語法。

+0

這是完美的!有沒有辦法以一種方式填充它,而不是每次都調用put()? – Angelo 2012-02-08 21:48:53

+2

取決於你的意思。你在哪裏得到分類?有幾種方法可以做到,但最好的辦法是a)堅持一次一個的put操作,或者使用[Guava的ImmutableMap.builder()](http:// docs .guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ImmutableMap.Builder.html),如果你可以使用第三方庫。 – 2012-02-08 21:51:18

6

我會開始這樣的事情。

public class Category 
{ 
    private final String name; 
    private final Set<String> items; 

    public Category(String name) 
    { 
     this.name = name; 
     this.items = new HashSet<String>(); 
    } 

    public String getName() 
    { 
     return name; 
    } 

    public void add(String... items) 
    { 
     for (String item : items) 
     { 
      this.items.add(item); 
     } 
    } 

    public boolean contains(String item) 
    { 
     return this.items.contains(item); 
    } 
} 

然後,

Category tools = new Category("tool"); 
tools.add("hammer", "screwdriver", "pliers"); 

Category animals = new Category("animal"); 
animals.add("horse", "dog", "cat"); 

最後,

// Guava for brevity 
List<Category> categories = Lists.newArrayList(tools, animals); 

public void stringConverter(String str) 
{ 
    for (Category c : categories) 
    { 
     if (c.contains(str)) return c.getName(); 
    } 

    return "not found"; 
} 
+0

我想你不明白我的問題(也許這是我的錯,對不起我最差的英語)。 – Angelo 2012-02-08 21:43:05

+0

你爲什麼這麼認爲? – 2012-02-08 21:45:22

+0

它應該是自動識別的東西。 A組(動物)= {貓,狗,馬} B組(工具)= {錘子,螺絲刀} 如果我的方法讀取「貓」,它應該說:「你的字符串是A組」 「動物」。 – Angelo 2012-02-08 21:45:39

1

使用Map<String, String>其中地圖的關鍵是你的6根弦,和值是"animal""tool"。使用map.get(str)來獲取字符串的類型。

1

有很多方法可以爲這隻貓(npi;)皮膚 - 這裏有一種方法。註釋是另一種可行的方式(例如,類別是帶有目標類型字段的註釋類型等)。顯然,比較機制不在下面完成,但是這很簡單。

public enum Category { 
    animal, tool 
} 
public interface Categorized { 
    Category getCategory(); 
} 
public enum FarmAnimals implements Categorized { 
    dog, cat, horse, rabbit; 
    public Category getCategory() { 
     return Category.animal; 
    } 
} 
public enum GarageTools implements Categorized { 
    screwdriver, drill, wrench; 
    public Category getCategory() { 
     return Category.tool; 
    } 
} 

[編輯:當然,你的枚舉可以形式狗(「狗」),等等。如果你需要嵌入的空格等]

相關問題