如何搜索链接列表,其中包含一些字符串的初始值?

时间:2014-06-29 10:20:46

标签: java

我有一个LinkedList,如下所示

   package com;
import java.util.LinkedList;
import org.json.JSONException;
public class Teste {
    public static void main(String[] args) throws JSONException {
        LinkedList<String> list = new LinkedList<String>();
        list.add("Apple(7)");
        list.add("Bucket(2)");
        String strtosearch = "Bucket";
    }
}

我试图以这种方式搜索LinkedList,如果我提供值Bucket,我需要值2.

如果我提供Apple,则必须返回7

有没有简单的方法呢?

我知道String.StartsWith可以提供帮助,但还有其他方法吗?

1 个答案:

答案 0 :(得分:1)

您正在使用wrone数据结构执行此类任务。

import java.util.*;

public class HashMapDemo {

   public static void main(String args[]) {

      HashMap hm = new HashMap();
      hm.put( "Apple", 7);
      hm.put( "Bucket", 2);

      System.out.println( hm.get("Apple") );

 }