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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:21:15+00:00 2026-05-18T23:21:15+00:00

I have the following code: import java.util.*; public class SellTransaction extends Transaction { private

  • 0

I have the following code:

import java.util.*;

public class SellTransaction extends Transaction {
    private Map<String,? extends Object> origValueMap;
    public SellTransaction(Map<String,? extends Object> valueMap) {
        super(Transaction.Type.Sell);
        assignValues(valueMap);
        this.origValueMap=valueMap;
    }
    public SellTransaction[] splitTransaction(double splitAtQuantity) {
        Map<String,? extends Object> valueMapPart1=origValueMap;
        valueMapPart1.put(nameMappings[3],(Object)new Double(splitAtQuantity));
        Map<String,? extends Object> valueMapPart2=origValueMap;
        valueMapPart2.put(nameMappings[3],((Double)origValueMap.get(nameMappings[3]))-splitAtQuantity);
        return new SellTransaction[] {new SellTransaction(valueMapPart1),new SellTransaction(valueMapPart2)};
    }
}

The code fails to compile when I call valueMapPart1.put and valueMapPart2.put, with the error:

The method put(String, capture#5-of ? extends Object) in the type Map is not applicable for the arguments (String, Object)

I have read on the Internet about generics and wildcards and captures, but I still don’t understand what is going wrong. My understanding is that the value of the Map‘s can be any class that extends Object, which I think might be redundant, because all classes extend Object. And I cannot change the generics to something like ? super Object, because the Map is supplied by some library.

So why is this not compiling? Also, if I try to cast valueMap to Map<String,Object>, the compiler gives me that ‘Unchecked conversion’ warning.

Thanks!

  • 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-18T23:21:16+00:00Added an answer on May 18, 2026 at 11:21 pm

    If the library specifies extends then they are explicitly disallowing put. You should defensively copy before modifying, since they can quite legitimately change their return type to be immutable in a new version. If copying is expensive, then you can try creating a map type that is of type <String, Object> that first queries their map, and then queries some map you create that has your local modifications.

    If you do know that their return type is immutable and that you solely own it, then the @SuppressWarnings("unchecked") annotations is a legitimate way to work around the warning, but I would double check those assumptions and comment extensively.

    To understand extends vs super, look at it this way.
    Since the value can be any type that extends Object, the following is valid.

    Map<String, Number> strToNum = new HashMap<String, Number>();
    strToNum.put("one", Integer.valueOf(1));  // OK
    
    Map<String, String> strToStr = new HashMap<String, String>();
    strToStr.put("one", "1");  // OK
    
    Map<String, ? extends Object> strToUnk = randomBoolean() ? strToNum : strToStr;
    strToUnk.put("null", null);  // OK.  null is an instance of every reference type.
    strToUnk.put("two", Integer.valueOf(2));  // NOT OK.  strToUnk might be a string to string map
    strToUnk.put("two", "2");  // NOT OK.  strToUnk might be a string to number map
    

    So put doesn’t really work with the extends boundary types.
    But it works perfectly well with reading operations like get:

    Object value = strToUnk.get("one");  // We don't know whether value is Integer or String, but it is an object (or null).
    

    If you want a map to primarily use with “put” instead of “get”, then you can use “super” instead of extends as in:

    Map<String, Number> strToNum = new HashMap<String, Number>();
    Map<String, Object> strToObj = new HashMap<String, Object>();
    
    Map<String, ? super Number> strToNumBase;
    if (randomBoolean()) {
      strToNumBase = strToNum;
    } else {
      strToNumBase = strToObj;
    }
    
    // OK.  We know that any subclass of Number can be used as values.
    strToNumBase.put("two", Double.valueOf(2.0d));
    
    // But now, gets don't work as well.
    Number n = strToNumBase.get("one");  // NOT OK. 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code in Java: import java.util.*; public class longest{ public static void
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
I have the following test code. import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; class MyTask
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code: String inputFile = somefile.txt; FileInputStream in = new FileInputStream(inputFile);
I have following Code Block Which I tried to optimize in the Optimized section
I'm trying to use opengl in C#. I have following code which fails with

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.