HOW TO CREATE AN LINKEDLIST<STRING, INT>

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
Java 
 1455  View(s)
Ratings:
Rate this:

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

  1. jacob rasel

    Post:88

    Points:616
    Re: How to Create an LinkedList<String, int>

    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).

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!