Home > DeveloperSection > Forums > How to Create an LinkedList<String, int>
Ashish Pandey

Total Post:128

Points:898
Posted on    October-04-2013 9:12 AM

 Java Java 
Ratings:


 1 Reply(s)
 1102  View(s)
Rate this:

I want to create an LinkedList of couple that the key is a String and the value is an integer ?



jacob rasel
jacob rasel

Total Post:88

Points:616
Posted on    October-04-2013 9:28 AM

You can only use Object in a LinkedList., this means you cant use Java Primitives. However, what you seem to need is a Map structure.

I recommend using java.util.HashMap, it allows you to create a Key, Value pairs.

Example:

    LinkedHashMap<String, Integer> b = new LinkedHashMap<String,Integer>();

    b.put("one",1);

    b.put("two",2);

    b.put("a",3);

    for (String key:b.keySet())

    {

        System.out.println(b.get(key));    // print 1 then 2 finally 3

    }

Hope this is what you were asking (if so, modify your question).


Don't want to miss updates? Please click the below button!

Follow MindStick