Results 1 to 6 of 6

Thread: How to create type specific collections in java?

  1. #1
    Join Date
    Nov 2009
    Posts
    45

    How to create type specific collections in java?

    Hello to all,
    I am new to this forum. I am working on live project where I am using Java as front end and SQL as back end. I want to create type specific collections using java program. I tried various method but none of them worked out. Can anyone tell me how to create type specific collections in java? Please help me.
    Thank you.
    Last edited by MAGAR; 01-02-2010 at 03:35 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to create type specific collections in java?

    We have to use Generic to create a type specific collection object. In the following program I have created a Map whose key is an Integer. It also have the value of a String. There is no need to cast value obtained from type specific collection. We have top create the List that will contains a String only values.

    Code:
    package org.kodejava.example.intro;
    
    import java.util.Map;
    import java.util.HashMap;
    import java.util.List;
    import java.util.ArrayList;
    
    public class TSColl {
        public static void main(String[] args) {
            
            Map<Integers, Strings> grades1 = new HashMap<Integesr, Strings>();
    
            grades1.put(1, "X");
            grades1.put(2, "Y");
            grades1.put(3, "Z");
            grades1.put(4, "V");
            grades1.put(5, "B");
    
            
            String values = grades1.get(1);
    
           
            List<String> dayNames = new ArrayList<String>();
            dayNames.add("thursday");
            dayNames.add("firday");
            dayNames.add("saturday");
            dayNames.add("Wednesday");
    
            
            String firstDays = dayNames1.get(0);
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to create type specific collections in java?

    For keeping track of what type of elements collections contain, we have to create type specific collections in java. When we use generics collection it is not treated as a list of Object references. You have to differentiate between a collection of references to Integers and collection of references to Bytes to create type specific collections in java. A collection with a generic type has a type parameter that is used to stored element type in the collection.
    Code:
    import java.util.*;
    
    public class typesafe {
    
      private void testCollection() {
        List lists = new ArrayList();
        lists.add(new String("ghari ja"));
        lists.add(new String("Good day!"));
        lists.add(new Integer(36));
        printCollection(lists);
      }
    
      private void printCollection(Collection c) {
        Iterator k = c.iterator();
        while(k.hasNext()) {
          String items = (String) k.next();
          System.out.println("Item: "+items);
        }
      }
    
      public static void main(String argv[]) {
        Ex1 y = new typesafe();
        y.testCollection();
      }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to create type specific collections in java?

    Generics is used to create a particular list that will only used for holding objects of a certain type. Following example creates a list of Integer objects.

    Code:
    List<Integer> intlists = new ArrayList<Integer>();
     intlists.add(new Integer(456));
    When you declared A list fro holding objects of a type X then that list also hold objects that extend from X. In the following example, a list is created to hold Number objects.

    Code:
    List<Number> numlists = new ArrayList<Number>();
     numlists.add(new Integer(654)); 
    numlists.add(new Float(654));

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to create type specific collections in java?

    I have written following program for creating type specific collections in java. It is very simple program. Just try to understand it. In the following program I have created MAP class to hold Integer and String type. I also have created one list to hold object. Just go thorough it. I have use java.util.Map to import Map class.


    Code:
    import java.util.Map;
    import java.util.HashMap;
    import java.util.List;
    import java.util.ArrayList;
    
    public class Main {
      public static void main(String[] args) {
        Map<Integers, Strings> grades2 = new HashMap<Integers, Strings>();
    
        grades.put(5, "Q");
        grades.put(6, "W");
        grades.put(7, "E");
        grades.put(8, "R");
        grades.put(9, "T");
    
        String value2 = grades2.get(3);
    
        List<String> dayNames = new ArrayList<String>();
        dayNames.add("lomah");
        dayNames.add("Mokan");
        dayNames.add("pokan");
        dayNames.add("Westan");
    
        String lastday = dayNames2.get(0);
      }
    }

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to create type specific collections in java?

    We have to use Generics to create a list that bale to hold only objects of a certain type. We know that null is not a subclass of any type. In this case collection supports null values. We also able to add type-specific collection. Just try to understand following example. You also have to note that a value taken from a type-specific list does not need to be casted.

    Code:
    List<URL> urlLists = new ArrayLists<URL>();
     try { urlLists.add(new URL("http:techarena.in"));
     } catch (MalformedURLException es) { } String ss = urlLists.get(0).getHosts();

Similar Threads

  1. Create shortcuts on taskbar for specific URLs in Windows 7
    By klinsmann in forum Customize Desktop
    Replies: 4
    Last Post: 28-08-2012, 12:06 PM
  2. Collections in Java
    By blueprats in forum Guides & Tutorials
    Replies: 3
    Last Post: 22-03-2010, 02:51 PM
  3. Create a specific file type
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 11:29 AM
  4. How is it possible to get the file type in Java?
    By Quattro in forum Software Development
    Replies: 3
    Last Post: 30-07-2009, 11:04 PM
  5. Create your own Run Command for any specific Application
    By Computer_Freak in forum Tips & Tweaks
    Replies: 1
    Last Post: 26-01-2009, 02:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,518,278.16557 seconds with 17 queries