📜  在Java中迭代不可修改的集合

📅  最后修改于: 2022-05-13 01:55:21.146000             🧑  作者: Mango

在Java中迭代不可修改的集合

集合是一个框架,它提供了一种架构来存储和操作对象组。在Java Collections中,我们执行搜索、排序、迭代给定元素集等操作。

可修改:可修改意味着我们可以在集合数据结构中执行添加、删除、更新元素等操作。

示例: Java List 提供的修改方法包括,add() 方法添加列表中的元素, replaceall()方法更改列表中的元素。

不可修改:不可修改意味着我们不能在集合数据结构中添加、删除、更新元素。

示例:我们可以在任何数据结构之前初始化可修改类,以便我们可以防止它更新给定数据结构中的任何元素。我们可以使用 Collections.unmodifiableCollection(collection) 创建集合的不可修改视图

宣言

public static  Collection unmodifiableCollection(Collection c)

句法:

参数:此方法将集合作为参数,为其返回不可修改的视图。

返回值:此方法返回指定集合的不可修改视图

示例 1:

Java
// Java program to Iterate Over Unmodifiable Collection
  
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
  
public class GFG {
  
    public static void main(String args[])
    {
        // create a list
        List list = new ArrayList();
  
        // add elements
        list.add("welcome");
        list.add("to");
        list.add("geeks for geeks");
        list.add("This");
        list.add("is");
        list.add("Unmodifiable Collection");
  
        System.out.println("Element are added to the list: "
                           + list.get(2));
  
        // create a immutable view of the list
        Collection immutableCol
            = Collections.unmodifiableCollection(list);
  
        // iterator on the immutable list
        Iterator iterator = immutableCol.iterator();
  
        // print the immutable list
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}


Java
// Java program to Iterate Over Unmodifiable Collection 
  
import java.util.*;
  
public class GFG {
    
    public static void main(String args[])
    {
          // create a vector
        Vector v = new Vector();
        
          // add elements
        v.add("welcome");
        v.add("to");
        v.add("geeks for geeks");
        
          // create a immutable vector
        Collection immutableCol
            = Collections.unmodifiableCollection(v);
        
          // iterate and print elements
        Iterator iterator = immutableCol.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}


Java
// Java program to demonstrate
// unmodifiableCollection() method
// for UnsupportedOperationException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of ArrayList
            ArrayList list = new ArrayList<>();
  
            // populate the list
            list.add("Geeks");
            list.add("for");
  
            // printing the list
            System.out.println("Initial list: " + list);
  
            // getting unmodifiable list
            // using unmodifiableCollection() method
            Collection immutablelist
                = Collections.unmodifiableCollection(list);
  
            // Adding element to new Collection
            System.out.println(
                "\nTrying to modify"
                + " the unmodifiableCollection");
  
            immutablelist.add("Geeks");
        }
  
        catch (UnsupportedOperationException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出
Element are added to the list: geeks for geeks
welcome
to
geeks for geeks
This
is
Unmodifiable Collection

示例 2:

Java

// Java program to Iterate Over Unmodifiable Collection 
  
import java.util.*;
  
public class GFG {
    
    public static void main(String args[])
    {
          // create a vector
        Vector v = new Vector();
        
          // add elements
        v.add("welcome");
        v.add("to");
        v.add("geeks for geeks");
        
          // create a immutable vector
        Collection immutableCol
            = Collections.unmodifiableCollection(v);
        
          // iterate and print elements
        Iterator iterator = immutableCol.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
输出
welcome
to
geeks for geeks

示例 3:对于UnsupportedOperationException

Java

// Java program to demonstrate
// unmodifiableCollection() method
// for UnsupportedOperationException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of ArrayList
            ArrayList list = new ArrayList<>();
  
            // populate the list
            list.add("Geeks");
            list.add("for");
  
            // printing the list
            System.out.println("Initial list: " + list);
  
            // getting unmodifiable list
            // using unmodifiableCollection() method
            Collection immutablelist
                = Collections.unmodifiableCollection(list);
  
            // Adding element to new Collection
            System.out.println(
                "\nTrying to modify"
                + " the unmodifiableCollection");
  
            immutablelist.add("Geeks");
        }
  
        catch (UnsupportedOperationException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出

Initial list: [Geeks, for]
Trying to modify the unmodifiableCollection
Exception thrown : java.lang.UnsupportedOperationException