Total members 11890 |It is currently Thu Apr 25, 2024 10:19 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Difference in order between HashSet and ArrayList for a group of strings objects :
Code:

import java
.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class 
Main {

    public static 
void main(String[] args) {

        
String stringsArr[] = {"adam""tom""ali""mina""saly"};


        List 
arraylist = new ArrayList();
        
Set hashSet = new HashSet();
        for (
String str stringsArr) {
            
arraylist.add(str);
            
hashSet.add(str);
        }
        
Iterator listIterator arraylist.iterator();
        
Iterator setIterator hashSet.iterator();
        
System.out.println("Iterations for array list");
        while (
listIterator.hasNext()) {
            
System.out.print(listIterator.next() + "\t");
        }
        
/*
         *  Notice that the output of set object is not in order.
         * 
         */
        
System.out.println("\nIterations for hash set.");
        while (
setIterator.hasNext()) {
            
System.out.print(setIterator.next() + "\t");
        }
    }
}
 


The output for this snippet :
Code:
Iterations for array list.
adam        tom        ali        mina        saly       
Iterations for hash set.
saly        ali        adam        mina        tom




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Difference in order between HashSet and ArrayList
 how to make ArrayList ThreadSafe     -  
 clone arrayList elements     -  
 SQL ORDER BY     -  
 rank order filter effects on image     -  
 How to display WooCommerce customer order details     -  
 How to change the default order on forums from ascending to     -  
 Return an array with elements in reverse order     -  
 Input three numbers and then sort non descending order     -  
 SELECT-Group by and Order by,Having Clause,count(),Joins     -  
 difference between the >> and >>> operators     -  



Topic Tags

Java Collections
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com