Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arraylist containing Integers and Strings

I want to create a Arraylist which should contain Integers and Strings.. Is that possible?

I have created two Arraylist as given below:

ArrayList<Integer> intList=new ArrayList<Integer>();
    intList.add(1);
    intList.add(2);

ArrayList<String> strList=new ArrayList<String>();
    strList.add("India");
    strList.add("USA");
    strList.add("Canada");

I want to put intList & strList into a new ArrayList.

Can I do that?? If so, How??

like image 555
user2648572 Avatar asked Aug 03 '13 13:08

user2648572


4 Answers

You can do this as follows but have to give up on generics for the list container.

List<List> listOfMixedTypes = new ArrayList<List>();

ArrayList<String> listOfStrings = new ArrayList<String>();
ArrayList<Integer> listOfIntegers = new ArrayList<Integer>();

listOfMixedTypes.add(listOfStrings);
listOfMixedTypes.add(listOfIntegers);

But, a better way would be to use a Map to keep track of the two lists since the compiler would no longer be able to prevent you from mixing types like putting a String into an Integer list.

Map<String, List> mapOfLists = new HashMap<String, List>();

mapOfLists.put("strings", listOfStrings);
mapOfLists.put("integers", listOfIntegers);

mapOfLists.get("strings").add("value");
mapOfLists.get("integers").add(new Integer(10));
like image 51
Ravi K Thapliyal Avatar answered Oct 20 '22 02:10

Ravi K Thapliyal


I am providing you that example which is implement in my project inquiryhere.com Click here to Visit

java code...

public class hash_Map {
public HashMap<Integer, String> getQuestionTagWithId(int qId) throws SQLException{
    DatabaseConnection dc = new DatabaseConnection();
    HashMap<Integer, String> map = new HashMap<>();
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try{
        con = dc.getConnection();
        String sql = "select tag_id as unique_id,(select topic_name from topic where unique_id = question_topic_tag.tag_id)topic_name from question_topic_tag where question_id =?";
        ps = con.prepareStatement(sql);
        ps.setInt(1, qId);
        rs = ps.executeQuery();
        while(rs.next()){
            int questionTagId = rs.getInt("unique_id");
            String questionTag = rs.getString("topic_name");
            map.put(questionTagId, questionTag);
        }
    }catch(SQLException msg){
        throw msg;
    }finally{
          if(rs != null){
            try{
                rs.close();
            }catch(SQLException msg){

            }
        }
        if(ps != null){
            try{
                ps.close();
            }catch(SQLException msg){

            }
        }
        if(con != null){
            try{
                con.close();
            }catch(SQLException msg){

            }
        }
    }
    return map;
}}

Jspbean

<jsp:useBean class="com.answer.hash_Map" id="SEO"  scope="page" />

jstl code, I am working with jstl

 <c:forEach items="${SEO.getQuestionTagWithId(param.Id)}" var="tag" varStatus="loop">
                       ${tag.key}${tag.value}
 </c:forEach>
like image 28
Aman Kumar Avatar answered Oct 20 '22 02:10

Aman Kumar


If it's avoidable, please avoid this list of Object type. Go for individual lists.

If not then you should go for type of Object

List<Object> list = new ArrayList<Object>();

which accept all the type Objects, but have to take care while retrieving.

Checking the objects while retrieving

for (Object obj: list) {
    if (obj instanceof String){
        // this  is string 
    } else if (obj instanceof Integer) {
       // this  is Integer 
    }
}
like image 20
Suresh Atta Avatar answered Oct 20 '22 00:10

Suresh Atta


List<Object> oList=new ArrayList<Object>();
like image 33
ihsan kocak Avatar answered Oct 20 '22 01:10

ihsan kocak