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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:02:59+00:00 2026-05-24T03:02:59+00:00

I’ve been looking for a flyweight pattern implementation and gave up after reaching page

  • 0

I’ve been looking for a flyweight pattern implementation and gave up after reaching page 20 of Google search. While there are countless stupid examples out there, it seems no one has ever published are reusable implementation in Java.

For me, flyweight really only makes sense if you have to keep many such instances, so it has to be implemented as a collection. What I would like is a Factory that takes a byte/short/int/long mapper implementation, and returns either a List, Set or Map, that looks like a normal Object collection, but stores it’s data internally as an array of primitive, thereby saving lots of ram. The mapper would take an object of type X, and map it to a primitive, or do it the other way around.

Is there something like that available somewhere?

[EDIT] I am looking for a Collection library that support this pattern, not just any example, of which there are hundreds.

  • 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-24T03:03:01+00:00Added an answer on May 24, 2026 at 3:03 am

    If you want to replace List, you can use TByteArrayList instead. If youw ant to replace List where MyClass { int a; T object; } you can use TIntObjectHashMap instead.

    If you want to replace something with two fields which must be ordered or three or more fields, you need to implement your own class which wraps arrays to hold the data. This is using a column based table model.

    e.g.

    class MyClass {
        byte b;
        int i;
        String s;
    }
    
    class MyClassList {
        int size = 0;
        int capacity;
        byte[] bytes;
        int[] ints;
        String[] strings;
    
        MyClassList(int capacity) {
            this.capacity = capacity;
        }
    
        public void add(MyClass myClass) {
            if (size == capacity) resize();
            bytes[size] = myClass.b;
            ints[size] = myClass.i;
            strings[size] = myClass.s;
            size++;
        }
    
        public void get(MyClass myClass, int index) {
            if (index > size) throw new IndexOutOfBoundsException();
            myClass.b = bytes[index]; 
            myClass.i = ints[index];
            myClass.s = strings[index];
        }
    }
    

    From Java 5.0, the auto-boxing caches are examples of flyweights.

    Integer i1 = 1;
    Integer i2 = 1;
    System.out.println(i1 == i2); // true, they are the same object.
    
    Integer i3 = -200;
    Integer i4 = -200;
    System.out.println(i3 == i4); // false, they are not the same object.
    

    If you want to read the code, have a look at Integer.valueOf(int) in your IDE or
    http://www.docjar.com/html/api/java/lang/Integer.java.html line 638

    EDIT:
    Autoboxing for Integer uses IntegerCache which is a collection. An ArrayList is a class which wraps an array and has a size…

    private static class IntegerCache {
        static final int high;
        static final Integer cache[];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.