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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:29:37+00:00 2026-05-27T09:29:37+00:00

In the following example: public static void main(String[] args){ List<String> list = new ArrayList<String>();

  • 0

In the following example:

public static void main(String[] args){

        List<String> list = new ArrayList<String>();

        list.add("hi");
        list.add("hi");
        list.add("hi");

        list.remove("hi");

        System.out.println(list); //prints [hi, hi]

}

The ArrayList is reduced by one but which one does it remove? Does it remove the last inserted or the earliest inserted one?

  • 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-27T09:29:38+00:00Added an answer on May 27, 2026 at 9:29 am

    Since List#add() is documented to add to the end of the list, and List#remove(Object) is documented to return the first matching item it encounters, your call above will remove the earliest inserted instance of the string “hi.”

    Since the printed representation of the three objects in the list are all the same, it’s hard to see the difference in behavior. However, if you look at the instance addresses in a debugger, noting which one entered the list first, you’ll confirm that it’s also the first—and only—one to be removed.

    In your case, given that you’re using string literals, they are interned by the compiler (per §3.10.5 of the JLS), so you’ll see three of the same instance present in your list. In order to produce distinct String instances, try changing your three insertion calls to the following:

    /* not final */ String h = "h";
    list.add(h + "i"); // not interned, instance 1
    list.add(h + "i"); // not interned, instance 2
    list.add(h + "i"); // not interned, instance 3
    

    Compare what you see in the debugger using those statements with your original program. Do you see a difference?

    Note that if the String instance h above is declared as final, then the three concatenations will in fact all be interned, yielding the same String instance being added to the list three times. Thanks to @xehpuk for correcting my initial mistake regarding the final qualifier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class ZiggyTest{ public static void main(String[] args){ List<Integer> list = new LinkedList<Integer>(); list.add(4); list.add(5);
The following code public class GenericsTest2 { public static void main(String[] args) throws Exception
I was trying following example: class BaseClass { public void methodA(Class<?> cl) { System.out.println(Base.methodA());
public class Main { public static void main (String args[]) { int nums[]= {2,
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
Say suppose I have the following Java code. public class Example { public static
Let me use the following example to explain my question: public string ExampleFunction(string Variable)
Take the following example, I have a class public class SomeItem { public string
I was browsing over the following code example: public class GenericTest { public static

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.