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

  • Home
  • SEARCH
  • 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 8006411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:30:24+00:00 2026-06-04T17:30:24+00:00

public static void main(String[] args) throws Exception { Socket socket = new Socket(127.0.0.1, 2345);

  • 0
public static void main(String[] args) throws Exception {
    Socket socket = new Socket("127.0.0.1", 2345);

    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    Map<Integer, Integer> testMap = new HashMap<Integer, Integer>();

    testMap.put(1,1);
    oos.writeObject(testMap);
    oos.flush();

    testMap.put(2,2);
    oos.writeObject(testMap);
    oos.flush();

    oos.close();
}


public static void main(String[] args) throws Exception {
    ServerSocket ss = new ServerSocket(2345);
    Socket s = ss.accept();
    ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

    System.out.println((HashMap<Integer, Integer>) ois.readObject());
    System.out.println((HashMap<Integer, Integer>) ois.readObject());

    ois.close;
}

The code above is from two files.
When running them, the console prints the same result:

{1=1}
{1=1}

How can this happen?

  • 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-04T17:30:25+00:00Added an answer on June 4, 2026 at 5:30 pm

    An ObjectOutputStream remembers the objects it has written already and on repeated writes will only output a pointer (and not the contents again). This preserves object identity and is necessary for cyclic graphs.

    So what your stream contains is basically:

    • HashMap A with contents {1:1}
    • pointer: “HashMap A again”

    You need to use a fresh HashMap instance in your case.

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

Sidebar

Related Questions

import java.util.regex.*; public class Splitter { public static void main(String[] args) throws Exception {
I tried this code: public static void main(String[] args) throws Exception { String regexp
class Test { public static void main(String[] args) throws Exception { Test t =
The following code public class GenericsTest2 { public static void main(String[] args) throws Exception
public static void main(String[] args) throws FileNotFoundException { double agentID; String type; double price;
public static void main(String[] args) throws IOException, URISyntaxException,TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); String
as the title. public static void main(String[] args) throws InterruptedException { Thread thread =
import java.io.*; import java.util.*; public class Readfilm { public static void main(String[] args) throws
public class Main { public static void main(String[] args){ Class2 class2Object = new Class2();
public class ThreadState { public static void main(String[] args){ Thread t = new Thread(){

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.