Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7929685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:10:09+00:00 2026-06-03T20:10:09+00:00

I wanted to test how to get intersection between two lists, here is my

  • 0

I wanted to test how to get intersection between two lists, here is my code :

            List<Integer> list = Arrays.asList(16, 17, 18, 19, 20);
        List<Integer> list2 = list.subList(2, 5);

        System.out.println(list.subList(2, 5));
        System.out.println(list.containsAll(list.subList(2, 5)));
        System.out.println(list.retainAll(list2));

This produces :

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(Unknown Source)
    at java.util.AbstractList$Itr.remove(Unknown Source)
    at java.util.AbstractCollection.retainAll(Unknown Source)
    at gov.noaa.nhc.App.main(App.java:48)

But I’m not doing any removes. Why is this producing exception?

Update I :

Using :

List<Integer> list = new ArrayList<Integer>(Arrays.asList(16, 17, 18, 19, 20));
List<Integer> list2 = list.subList(2, 5);

Produces :

java.util.ConcurrentModificationException

Since list2 is backed by list1, removing items from list1 will cause this exception. Finally working version :

List<Integer> list = new ArrayList<Integer>(Arrays.asList(16, 17, 18, 19, 20));
List<Integer> list2 = Arrays.asList(18, 19, 20);

or

List<Integer> list = new ArrayList<Integer>(Arrays.asList(16, 17, 18, 19, 20));
List<Integer> list2 = new ArrayList<Integer>(Arrays.asList(18, 19, 20));

or

 List<Integer> list = new ArrayList<Integer>(Arrays.asList(16, 17, 18, 19, 20));
 List<Integer> list2 = new ArrayList<Integer>(list.subList(2, 5));

Thanks for your answers everyone.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:10:11+00:00Added an answer on June 3, 2026 at 8:10 pm

    When you use Arrays.asList, you get a list backed by the actual array passed in with restricted functionality. If you want a fully modifiable list you have to create a new list. For example:

    List<Integer> list = new ArrayList<Integer>(Arrays.asList(16, 17, 18, 19, 20));
    List<Integer> list2 = new ArrayList<Integer>(list.subList(2, 5));
    list.retainAll(list2);
    System.out.println(list);
    // result: [18, 19, 20]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

instead of writing code in the standard way: $.get('test.xml',function(){ //manipulate the code here })
I wanted to write a program that test if two files are duplicates (have
I wanted to get image url "http://www.test.com/image.jpg" out from the string: "&lt;img align=&quot;right&quot; alt=&quot;Title
I am using boost serialization on windows, and I wanted to test my code
I wanted to test how C++ behaves when the return value of a function
Out of curiosity I wanted to test the number of ticks to compare a
I set up a new webapp Maven project and wanted to test it with
Hi I've been writing a chat client and wanted to test the Java Sound
I have built part of my app and wanted to test it out. All
hm, i have an strange problem... i just wanted to test an app on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.