How to insert integers and strings in linked list? and how to search integers only from that list in java
-
It is possible. Any try so far from you ? – Suresh Atta Nov 03 '15 at 12:12
-
1Can you explain why you want two different types (with no relation) in the same list? – Tom Nov 03 '15 at 12:13
-
no I was just trying but unable to do so – Pramod Nov 03 '15 at 12:13
-
Why do you want to store int and string both in the same list? – Vivek Singh Nov 03 '15 at 12:14
4 Answers
Ok. To allow a List to store both Integers and Strings, you would need to use a list that operates on the common base class i.e., Object.
List<Object> stringsAndNumbers = new ArrayList<Object>();
You can insert the objects into this List using the add method.
You can write a for-loop and traverse each of the entries using the get() method.
You can use instanceof operator to determine whether you are dealing with a String or an Integer.
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Code it.

- 3,013
- 5
- 27
- 47
-
inserted integers and strings using object but unable to retrieve all the integers from list – Pramod Nov 03 '15 at 12:21
-
Did you get a change to see what 'instanceof' operator does?. You have to get each entry from the list and see if it is an instance of Integer. If so, you proceed. – Pavan Dittakavi Nov 03 '15 at 12:25
-
http://stackoverflow.com/questions/7313559/what-is-the-instanceof-operator-used-for – Pavan Dittakavi Nov 03 '15 at 12:25
ArrayList<Object> al = new ArrayList<>();
al.add("mmm");
al.add(1);
al.add("ka");
for(Object a : al){
if(a instanceof Integer){
System.out.print(a);
}
}

- 325
- 1
- 4
- 12
Well its not good to have different type of objects in one collection. Though you can do it as -
List<Object> l = new LinkedList<Object>();
List<Integer> r = new LinkedList<Integer>();
l.add(new Integer(1));
l.add(new Integer(2));
l.add(new Integer(3));
l.add("a");
l.add("b");
System.out.println(l);
for(Object o: l) {
if (o instanceof Integer) {
r.add((Integer) o);
}
}
System.out.println(r);

- 2,384
- 6
- 37
- 52
-
'its not good to have different type of objects in one collection' - I don't quite agree. Imagine a job portal that is going to send out notification about a new job opening to both registered and guest users. Only registered guys get cellphone intimation. They would have a single collection that has list of Users to be intimated. So they would iterate on this list and invoke the sendIntimation() method on it 'generically' which gets apt overrides in GuestUser and RegisteredUser respectively. This is a valid case of a Collection of parent type, which implictly means heterogenous mixture. – Pavan Dittakavi Nov 03 '15 at 12:23
-
How do you achieve type-safe collection then. Using Generics talks about it right? – Saurabh Nov 03 '15 at 12:28
-
@Pavan such objects would have a common superclass/interface which is not an Object. The superclass/interface would declare the method which is common for both types. And I agree this would be an absolutely valid use. Integer and String however have nothing in common. – Jaroslaw Pawlak Nov 03 '15 at 12:40
-
True. I do agree with such objects having their own superclass/interface. However, I would be interested in knowing if this wrapping of Integer and String breaks anything..principle or some best practise. – Pavan Dittakavi Nov 03 '15 at 14:02
Are you talking about a Map that has a value of the int to retrieve the value of the string? such as:
Map<Integer,String> myMap = new HashMap<Integer,String>();
myMap.containsKey(myInteger)
Or do you mean to have them both types in a list if you want to then you can do something like this
LinkedList<Object> myList = new LinkedList<Object>()
and then you can iterate on the linked list and check like this
public void boolean findValue(LinkedList<Object> myList){
for(Object myElement: ){
if(myElement instanceof Integer && myElement == myValue){
return true}
}
}
return false
}

- 297
- 4
- 23