Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to Iterate over unmodifiable collection in Java

1 Answer

0 votes
package javaapplication1;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class JavaApplication1 {

    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
 
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");

        System.out.println(list.get(0));
        System.out.println(list.get(1));

        Collection<String> unmodifiable = Collections.unmodifiableCollection(list);

        Iterator<String> iterator = unmodifiable.iterator();

        System.out.println();
        while (iterator.hasNext()) 
            System.out.println(iterator.next());
  }
}
  
/*
run:

aaa
bbb

aaa
bbb
ccc
ddd

*/

 



answered Sep 25, 2016 by avibootz

Related questions

4 answers 153 views
153 views asked Nov 23, 2023 by avibootz
2 answers 177 views
1 answer 120 views
4 answers 166 views
166 views asked Mar 16, 2023 by avibootz
1 answer 126 views
2 answers 106 views
106 views asked Mar 15, 2023 by avibootz
...