liveBook · Manning

Set

As explained in Data-Oriented Programming in Java

A set is a collection that represents distinct elements without treating duplicate occurrences as separate members.

A set models a collection whose members are distinct. In a cart, replacing lists with sets expresses the requirement that items should be unique, but Java set equality considers the entire object. section 9.3.1

A set therefore helps communicate an invariant without necessarily enforcing the domain’s intended identity. Two items with the same product and seller identifiers can remain distinct if another attribute, such as price, differs. section 9.3.1

Cart items

A ValidCart can store active and saved items as Set<Item>. If uniqueness depends on a subset of attributes, such as productId and sellerId, a Map<ProductSeller, Item> represents that identity more directly. section 9.3.1

Table 9.2 a refresher on the requirements
3
When a customer adds items to their cart
3.1
     If the item is already in the Active cart, the service shall add their quantities
3.2
     If the item is already in the Saved cart, the service shall add their quantities and move the item to Active
3.3
     If the item is not in either cart, it shall be added to Active
5.0
The system shall automatically move unavailable Active items to Saved

Cart requirements clarify why adding an existing item must combine quantities and why unavailable active items move to saved. section 9.3.1

How Data-Oriented Programming in Java covers it

How deep it goes
Develops a worked Java design comparison, stopping short of treating Set as sufficient for attribute-subset identity.
Examples
Uses active and saved cart item sets, duplicate identifiers, differing prices, and a ProductSeller-keyed map.
What you can do after
Evaluate whether Set expresses a domain’s uniqueness rule or whether Map better captures identity based on selected attributes.
How it connects
Connects sets with lists, maps, immutability, invariants, records, and object equality.

Also explained in

Compare how 19 books explain set →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.