Removing duplicate values from array list ( elements added from mysql) but not working


#1

`Preformatted text````

         ArrayList<String> stationlist=new ArrayList();//array list declared
        
        ArrayList<String> correcttrainlist=new ArrayList();// another list
      ////////////////////////////  
        ArrayList<String>modellist=new ArrayList();    
        // modellist for add in combobox
       
       
        try ///////////////
        // sql connectivity starts here
            {
         Class.forName("com.mysql.jdbc.Driver");// sql connectivity                                               Connection con=DriverManager.getConnection                                    ("jdbc:mysql:///viru","root","");
           String Query=" Select  * from trains"; // fetching from trains table
           
           PreparedStatement ps=con.prepareStatement(Query);
           
           ResultSet rs=ps.executeQuery();// result set object 
          
           while(rs.next())
           {                               
              // fetching stations from mysql  
              stationlist.add(rs.getString("stations"));//  array list

               Set<String> stationswithoutdup= new HashSet<String>(stationlist); // string type
    // hashset used for removing duplicate values from array list 

              stationlist.clear();// clear list

              stationlist.addAll(stationswithoutdup);// add set to list
              
              
      
              stationlist=new ArrayList<String>(Arrays.asList(stations.split(",")));
    // splitting a column by comma (,)
      
            

             
    if((stationlist.toString().contains(str1))&&stationlist.toString().contains(str2))

    // str1 and  str2 used for from station and to station textfield input
            {
      correcttrainlist.add(rs.getString("trainname")+"--"+(rs.getString("trainno")));  

// fetching trainname and trainno which matched to user inputs

      Set<String> correctlistwithoutdup= new HashSet<String>(correcttrainlist);

      correcttrainlist.clear();// clear previous correct list
![repeat trains|320x183](upload://68TwWq6car4PVgto9862ZvNXYCr.PNG)
      correcttrainlist.addAll(correctlistwithoutdup);// 
      this.revalidate();
      
            }
          
    
    // adding the trainame and trainno to combobox to display in frame
          
                 for(int i=0;i<correcttrainlist.size();i++)     
                 {
                     modellist.add(correcttrainlist.get(i));
                 } 
           String [] resultmodel=modellist.toArray(new String [modellist.size()]); 
   // adding required values to jcombobox for display
           
           DefaultComboBoxModel obj=new DefaultComboBoxModel(resultmodel);
           
          selecttrainbox.setModel(obj);

#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.