0

What I want to achieve ?

I want to store the following table in Java (in-memory) for further processing..!

List : sorted list of string a,b,c,..

e.g

  • a = "Apple"
  • b = "Lemon"
  • c = "Orange"

Length : length of List[a,b,c,..]

Frequency : number of times the List[a,b,c..] is in the database

Fig:

Is there any default data-structure available in Java to store this type of data?

If not,

What is an efficient way to store this type of data in Java ?

Thanks..!

edit 1 :- The data structure have following requirements :-

  • getter(List l1), setter(List l1), delete(List l1)

  • E.g:

  • during the time of insertion of data from xlsx sheet

    • if List l1[a,b,c, ..] of length 'n' already present, then increase its frequency by 1
    • else, add it to the map at appropriate location with frequency 1.
2
  • for further processing for what processing? Commented Oct 9, 2016 at 12:25
  • form my main program, I want to do something like :- get all List[a,b,c,..] of length 7 with their frequency. Commented Oct 9, 2016 at 12:29

3 Answers 3

1

Below mentioned approach you can follow. Here Node class will contains a single record which will include it's frequency. Upper level class will contains List of such Nodes who have same length and We will save length against List of Nodes in Map for faster retrieval.

For frequency count you have to generate hashCode and equals method in Node class which will check equality of different Node.

Here if you want data of specific length you can direct retrieve from Map , because key is length only.

    Map<Integer,List<Node>>  allDataNodes = new HashMap<Integer,List<Node>>();


    class Node{
        private List<String> data = new ArrayList<String>();
        private Integer frequency;

    }
Sign up to request clarification or add additional context in comments.

3 Comments

so lists with same frequencies are identical? and how do you fetch all nodes with same freequency? sequential search? that's o(n)
if you want to fetch by frequency in that case add one more map. Map<Integer,Node> allDataNodes = new HashMap<Integer,Node>(); but in comment he has mentioned what he wants.
@cody123 how to do something like : if map.contains(List l1 of length 7) then increase the frequency by 1 , else insert it in map for key 7
1

I have the following suggestion:

1) the "node" or "element" holds the sorted list and every other associated data, like one row in the diagram

public class Node
{
    public List<String> list;
    public int length;  // may be calculated by ctor...
    public int frequency;
    // perhaps some more data on list
}

2) the container holds several Maps (or possibly other data struvtures) that allow for search and retrieval by desired criteria

// key: frequency, value: all lists with key frequency
Map<Integer, List<Node>> getByfrequency = new HashMap<>();
// key: length, value: all lists with key length
Map<Integer, List<Node>> getByLength = new HashMap<>(); 

3) the advantage of this approach over the other suggestions is that it does not require a "hierarchy" of containers, and you can build as many maps as your search requires. plus, all searches are o(1)

4) the disadvantage is that when you add a new Node, you have to add it to all maps. this is an error-prone process...

6 Comments

So you are storing length as well in Node class. Why to store that field and increase space complexity when length can be calculated using any element of the list.
yes, length can be calculated. however, if length of list is considered one of the properties of a Node, especially if its a search criteria, then I am in favor of consistency over the neglegent complexity gain. the code should have an explicit list of properties that are important for the business logic.
however, i accept that instead of a length instance variable, a getter getListLength() can display the same significance
@sharonbn how to do something like : if map.contains(List l1 of length 7) then increase the frequency by 1 , else insert it in map for key 7
@PCP, it would be better if you listed all requirements that you have in the question. adding new requirements may make the answers irrelevant or require modifications to them
|
0

Should be something as this :

This as each element of kind list, with it's getLength() method,

public class ListElement 
{      
   // this will be each [a,b,c,d,...] object
   List<object> mylist; 
   public int getLength()
   {
        return myList.length();
   }
}

And this a container for the prior class :

public class MyTable
{
   List<ListElement> Elements; // wil contain each [a,b,c...] and it's length

   public int getFrequency(ListElement e)
   {
      // returns the number of repetitions of that element e;
      // (...)
   }
}

This should be the general schema. Of course depending on the way you'd like to process that data you should create more methods about.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.