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 7564595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:56:39+00:00 2026-05-30T13:56:39+00:00

I have following program in which I am adding few number to set and

  • 0

I have following program in which I am adding few number to set and list and then removing them, Could some one please explain why Set and list have different behavior.

public class SetList {
public static void main(String[] args){
    Set<Integer> set = new TreeSet<Integer>();
    List<Integer> list = new ArrayList<Integer>();
    for(int i=-3;i<3;i++){
        set.add(i);
        list.add(i);
    }
    for(int i=0;i<3;i++){
        set.remove(i);
        list.remove(i);
    }
    System.out.println(set+"            "+list);
}

}

and output is

[-3, -2, -1]            [-2, 0, 2]

I am able to understand the behavior of Set but unable to understand the behavior of List output. All help really appreciated.

  • 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-05-30T13:56:40+00:00Added an answer on May 30, 2026 at 1:56 pm

    Set and List are different types of collections. Set is an associative collection, thus Set.remove(i) will remove the element having the value of i. While List is an indexed collection, so List.remove(i) removes the element at the ith position in the list.

    So after removing the elements 0 to 3 from a Set containing elements of -3 … 3, your Set will predictably contain the values -3 to -1.

    With the List, the outcome of the same sequence of removals may be a bit more surprising, but it is actually logical. Originally your list contains:

    Index  0  1  2  3  4  5  6
    Value -3 -2 -1  0  1  2  3
    

    list.remove(0) removes the element at index 0, resulting in

    Index  0  1  2  3  4  5
    Value -2 -1  0  1  2  3
    

    Notice that all elements after the (removed) first have shifted one position forward! Thus, when list.remove(1) removes the element at index 1, it “hops over” the element -2. The result is

    Index  0  1  2  3  4
    Value -2  0  1  2  3
    

    Similarly the next operation, list.remove(2) “hops over” the element 0, resulting in

    Index  0  1  2  3
    Value -2  0  2  3
    

    And last, list.remove(3) removes the last element, giving the end result:

    Index  0  1  2
    Value -2  0  2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let us have a situation in which the following program prints some 10 lines
I have the following c program which launches a Gtk Program on ubuntu: #include
I have the following program to open lot's of sockets, and hold them open
In following program . I have one doubt. I have declared one global variable
I have the following program which compiles fine and with no errors or warnings:
I have the following program which I'd like to get it fixed. Not sure
I have done up a program which requires the following: prompts user input account
At some point in an XSLT program, I have the following: <xsl:for-each select=tags/tag> <xsl:apply-templates
ok so on my program i have some labels (which are in an array
I have the following code which is adding somestrings to an arraylist. It will

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.