-4

I have a class

class polygon
{
    private String name;
    private int quantity;

    // default constructor
    private polygon()
    { }

    public String get name() {
        return name;
    }
    public void setname(String name) {
        this.name = name;
    }

    public int getquantity() {
        return quantity;
    }
    public void setquantity(int quantity) {
        this.quantity = quantity;
    }       
}

and also I have a map like this:

LinkedHashMap<Integer, polygon> polymap = new LinkedHashMap<Integer, polygon>();

I have to ask two questions:

  1. How can I find if there is a member with specific value which has the name like "square"?
  2. How can I get all the member with the lowest quantity?

Thanks.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
H'H
  • 1,638
  • 1
  • 15
  • 39
  • How do you think this would work? I would suggest to iterate over the `Map::values` and filter what you're looking for. – Flown Oct 10 '15 at 13:54
  • since I am more familiar with C++, I know that it is simply possible using bood::bind, but I am a newbie in java. I am wondering if there is any better way than iterating over values. – H'H Oct 10 '15 at 13:57
  • 1. you should compare with each `name` in map 2. Sort map in descending order based on `quantity`. – SatyaTNV Oct 10 '15 at 14:02
  • @Satya How do you "sort a map"? Please explain. – laune Oct 10 '15 at 14:11
  • I don know why guys here gave me -1. I asked a question, if you have a better Idea, just help me. giving me -1 nether helps me nor shows you can answer the question. – H'H Oct 10 '15 at 14:29

2 Answers2

1

What we have learned from comments and updates: Polygon is a class with members String name (possibly but not necessarily unique) and int quantity (no restriction). There is also a timestamp, and this Integer is used as a key in the

Map<Integer,Polygon> polymap

To find all Polygons with a given name:

for( Polygon polygon: polymap.values() ){
    if( polygon.getName().equals( givenName ) ){ //... }
}

Now this is a sequential search and may take some time if the number of entries is very big. Assuming names are unique, O(1) access time can be achieved by creating another Map in parallel to polymap, mapping name strings to Polygon objects. (Consider the additional effort for removing, but Map.remove( ., . ) should help.)

To find all Polygons with the minimum quantity, determine the minimum while keeping a set of the Polygons with that minimum:

int min = Integer.MAX_VALUE;
Set<Polygon> polyset = new HashSet<>();
for( Polygon polygon: polymap.values() ){
    int qtty = polygon.getQuantity();
    if( qtty < min ){
        min = qtty;
        polyset.clear();
        polyset.add( polygon );
    } else if( qtty == min ){
        polyset.add( polygon );
    }
}

Again, a sequential search, but here a second map will require a multimap, i.e., a Map<Integer,Collection<Polygon>>.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Many thanks for your answer, if we assume that the polygon's name in polymap are unique, how I can improve the search? – H'H Oct 10 '15 at 20:09
  • As I wrote: you can create another Map instead of, or in addition to, polymap where the name is the key. But I can't invent code - the creation of polymap - that isn't in your question. -- Or you can modify polymap with equals *and hashCode* (!) if Polymap identity can be based on the name field (which I can't recommend not knowing what else is in that program) - see YoungHobbit's answer. – laune Oct 11 '15 at 03:34
0
LinkedHashMap<Integer, polygon> polymap = new LinkedHashMap<Integer, polygon>();
  1. How can I find if there is a member with specific value which has the name like "square"?

The containsValue() uses equals method for find the match. You can override the equals() method of the polygon class. Then you can use the containsValue(new polygon()) to find whether the value exist or not. The equals method implementation will be completely depended on the name field of the polygon class. Something like this:

@Override
public boolean equals(Object o) {
  if (this == o) return true;
  if (!(o instanceof polygon)) return false;

  polygon polygon = (polygon) o;

  if (!name.equals(polygon.name)) return false;

  return true;
}

Note: You are overriding the equals() method of the class, so could have some side affects on how you are using it at other places in your application.

  1. How can I get all the member with the lowest quantity?

I would suggest you maintain a separate PriorityQueue for finding lowest quantity polygon. Provide a custom Comparator while initializing the queue.

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
  • 1
    Ad hoc changes to a class to "simplify" one task with the risk (as you correctly state) of breaking your program elsewhere is hardly "best practice". Considering your answer regarding #2: why not do the same for #1? – laune Oct 10 '15 at 14:11
  • @laune Maintaining multiple data structure is overhead and error prone. I guess I If he re-think about the first Map data-structure could be great. I just tried to work around with the provided information. – YoungHobbit Oct 10 '15 at 14:18
  • @YuongHobbit thanks for your answer, Do you have better idea regarding map structure? My map contains lots of members. I know that boost::unordered_map would be good start if was writing in C++, but in java I do NOT have much experience. – H'H Oct 10 '15 at 14:31
  • @laune is it possible to change the structure of polygon? The data-structure will also have the same caveat of overriding the equals method for achieving the #1. – YoungHobbit Oct 10 '15 at 14:35