Sort table on multiple Comparator items - sorting

I need a way in Java 8 to sort a list of Item objects using on multiple Comparator objects.
The application creates a Comparator on every user action. It's stores the Comparator objects in an ArrayList. It is possible to sort on one Comparator Item but now we need a way to make it possible to sort on multiple Comarator items at the same time.
This line of code I use to sort the list:
tbl.getItems().stream().sorted(groupingComparator);
The variable groupingComparator is from type Comparator<Item>
Now I need to sort on multiple fields that are stored in
ArrayList<Comparator<Item>>
Is this possible in Java 8? And how can I implement this?

You can use the thenComparing(Comparator) method available from the Comparator interface to chain multiple Comparators together. See the example below, to create such a chained Comparator from a List using streams.
The domain class we want to use for sorting:
public static class Order {
String customer;
String item;
public Order(String customer, String item)
{
this.customer = customer;
this.item = item;
}
public String toString() {
return "Customer: " + customer + " Item: " + item;
}
}
Sample code:
public static void main(String... args) throws Throwable
{
List<Order> orders = new ArrayList<>();
orders.add(new Order("A", "Item 1"));
orders.add(new Order("B", "Item 3"));
orders.add(new Order("A", "Item 2"));
orders.add(new Order("B", "Item 1"));
List<Comparator<Order>> comparators =
Arrays.asList((o1, o2) -> o1.customer.compareTo(o2.customer), // comparator for customer names
(o1, o2) -> o1.item.compareTo(o2.item)); // comparator for item names
Comparator<Order> comp =
comparators.stream()
.reduce(Comparator::thenComparing)
// use a default comparator if the comparator list was empty
.orElse(Comparator.comparing(Object::toString));
orders.stream().sorted(comp).forEach((order) -> System.out.println(order));
}
This snippet will print the Orders sorted first by Customer then by Item name.

As simple as that:
public class CompositeComparator implements Comparator<Item> {
private List<Comparator<Item>> comparators;
public CompositeComparator(List<Comparator<Item>> comparators) {
this.comparators = comparators;
}
#Override
public int compare(Item o1, Item o2) {
int result = 0;
for (Comparator<Item> c : comparators) {
result = c.compare(o1, o2);
if (result != 0) {
break;
}
}
return result;
}
}

Related

Java8 Streams: Remove an field from an object of the map value

I have a hash map like this
Map<String, AttributeValueUpdate> myMap = new HashMap<>;
The class AttributeValueUpdate looks like this:
public class AttributeValueUpdate {
private AttributeValue value;
private String action;
public static class Builder {
private AttributeValue value;
private String action;
public Builder() {
}
public AttributeValueUpdate.Builder withValue(AttributeValue value) {
this.value = value;
return this;
}
public AttributeValueUpdate.Builder withAction(String action) {
this.action = action;
return this;
}
protected void populate(AttributeValueUpdate instance) {
instance.setValue(this.value);
instance.setAction(this.action);
}
public AttributeValueUpdate build() {
AttributeValueUpdate instance = new AttributeValueUpdate();
this.populate(instance);
return instance;
}
}
}
The map has two entries
AttributeValueUpdate att1 = AttributeValueUpdate.builder().withAction("Add").withValue(new AttributeValue("sam").build();
AttributeValueUpdate att2 = AttributeValueUpdate.builder().withAction("Delete").withValue(new AttributeValue("john").build();
myMap.add("entry1", attr1);
myMap.add("entry2", atte2);
I want to modify mymap by deleting the "value field" from all the AttributeValueUpdate (which is value of the map), basically map's value field will be changed by removing "value field" of the AttributeValueUpdate object. How can I achieve this using java streams?
Java Stream API is not a friend with Map as long as it's collection-based (List, Set). You need to stream over the entries of the map.
As far as I understand, you want to remove (= make null) AttributeValue value of each AttributeValueUpdate instance (map's value). Here is the way to go assuming a constructor AttributeValueUpdate(String action):
Map<String, AttributeValueUpdate> updatedMap = myMap.entrySet().stream()
.map(entry -> {
String action = entry.getValue().getAction();
AttributeValueUpdate update = new AttributeValueUpdate(action);
return new SimpleEntry<>(entry.getKey(), update);
})
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
The easiest solution is using Map#replaceAll if you don't mind to mutate the map:
myMap.replaceAll((k, v) -> {
String action = v.getAction();
return new AttributeValueUpdate(action);
});

Using Java8 to find string values in one ArrayList present in another ArrayList using endsWith method

I have following code.
public class ComparingTwoLists {
public static void main(String[] args) {
List<String> small = Arrays.asList("AA", "BB");
List<String> big = Arrays.asList("a.b.AA", "a.b.BB", "a.b.CC");
List<String> list = new ArrayList<String>();
Consumer<String> consumer = (String outer) -> {
Stream<String> stream1 = small.stream();
Consumer<String> innerConsumer = (String inner) -> {
if (outer.endsWith(inner)) {
list.add(outer);
}
};
stream1.forEach(innerConsumer);
};
Stream<String> stream2 = big.stream();
stream2.forEach(consumer);
System.out.println(list);
}
}
I am trying to find out if string values in small is present in string values of big at end of string, using method endWith() method.
Is this efficient way of doing it?
Another problem I am facing in this code is that when I am trying to debug this code, I am able to view/inspect the value of inner, but cannot view the value of outer. Is there a way to check the value of outer while debugging.
You can do like this:
big.stream()
.filter(s -> small.stream().anyMatch(s::endsWith))
.collect(Collectors.toList());

add dynamic row content to vaadin grid

In vaadin7, I used GeneratedPropertyContainer to do this, eg adding row number:
wrappedContainer = new GeneratedPropertyContainer(_container);
wrappedContainer.addGeneratedProperty("#",
new PropertyValueGenerator<Integer>() {
#Override
public Integer getValue(Item item, Object itemId, Object propertyId) {
return (int) _container.indexOfId(itemId) + 1;
}
#Override
public Class<Integer> getType() {
return java.lang.Integer.class;
}
}
);
setContainerDataSource(wrappedContainer);
In vaadin8, since GeneratedPropertyContainer is deprecated, I tried to do like this:
grid.addColumn((v)->((List)_container.getData().getItems()).indexOf(v)+1);
But the index is static, when I sort the rows ascending and descending, the row number is moving too.
What I need is the first row is row number 1 and the last row is row number N, no matter how I sort the rows.
Thanks.
Unfortunately, as far as I can tell there is no simple solution out of the box. However, you can do like the following code:
Grid<MyBean> grid = new Grid<>();
grid.setDataProvider(new RowIndexDataProviderWrapper<>(DataProvider.ofItems(new MyBean("Item 1"), new MyBean("Item 2"), new MyBean("Item 3"))));
grid.addColumn(MyBean::getRowIndex).setCaption("#");
grid.addColumn(MyBean::getName).setCaption("Name");
public interface RowIndexAware {
void setRowIndex(int rowIndex);
int getRowIndex();
}
public class MyBean implements RowIndexAware {
// implement the interface (e.g. store row index in field)
// and add your bean properties
}
public class RowIndexDataProviderWrapper<T extends RowIndexAware, F> implements DataProvider<T, F> {
private DataProvider<T, F> wrapped;
public RowIndexDataProviderWrapper(DataProvider<T, F> wrapped) {
this.wrapped = wrapped;
}
// delegate all methods to be implemented for DataProvider interface
// to wrapped DataProvider with the exception of "fetch":
#Override
public Stream<T> fetch(Query<T, F> query) {
List<T> result = wrapped.fetch(query).collect(Collectors.toCollection(ArrayList::new));
for (int i = 0; i < result.size(); i++) {
result.get(i).setRowIndex(query.getOffset() + i);
}
return result.stream();
}
}
The idea is to get the row index when rows are fetched within the DataProvider and to store them in your bean.

wicket manipulating DefaultDataTable

I was creating a web application in wicket and had created a table which shows the user some information. Now I wanted to manipulate this table so if the cell contained "N" the background color would be RED and if it contained "Y" the background color would be GREEN. At the moment I was having trouble to determine what is actually inside the cell. I create my table by the following:
dataTable = new DefaultDataTable<TableModalInt, String>("table", columns,
new TableModalProvider(), 100000){
#SuppressWarnings("rawtypes")
#Override
protected Item newCellItem(String id, int index, IModel model) {
Item item = super.newCellItem(id, index, model);
if (id == "3"){
item.add(AttributeModifier.replace("align", "center"));
}
return item;
}
};
I am capable of determining the cell which I wanna now check what is being displayed to the user. Any help on how i can do this? to change the color i know I'll have to add item.add(AttributeModifier.replace("bgcolor", "red")); but don't know how to tell whats inside the cell
You should do your checks in the IColumn implementation.
https://github.com/apache/wicket/blob/24e9db6c8af85043ce36e4d25a0e8a2d8dc2f49e/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/PropertyColumn.java#L94 populates the Item with a Label. You need to add a AttributeModifier to the Label.
You can also achieve your goal with pure JavaScript and/or CSS at the client side.
Item is extending list item, so you can try .getModelObject and validate it if it is "X" or "Y"
http://wicket.apache.org/apidocs/1.5/org/apache/wicket/markup/repeater/Item.html
http://wicket.apache.org/apidocs/1.5/org/apache/wicket/markup/html/list/ListItem.html#getModelObject()
This example extracts the cell value when a cell is clicked in a Wicket DataView. The model for this DataView is a Map with String keys and Integer values: Map<String,Integer>.
The PropertyColumn list is created using with column headers ("ALPHA", "BETA", "GAMMA") and property expressions: "alpha", "beta", "gamma". PropertyColumn uses the expressions to retrieve the values from the map.
The DataView is created with the list of PropertyColumns and a DataProvider. DataView uses the DataProvider to populate the PropertyColumn when the table is rendered and reacts to clicks to expose the cell values.
Cells are exposed by overriding the newCellItem(String,int,IModel) method and calling the super-class method to get the cell. This example adds a behavior to react to "onclick" events. Within the event, the cell's first child component should be the Label used to display the cell value.
The innermost model of the cell Label is the PropertyModel from the PropertyColumn.
innerModel.getPropertyExpression(): Our data map key (String).
innerModel.getObject(): The data value (Integer).
innerModel.getInnermostModelOrObject(): The list item (Map<String,Integer>).
Wicket DataView: Extract Cell Value
public class MessageLogStatus
extends WebPage
{
/** Represents serialVersionUID. */
private static final long serialVersionUID = 20150701L;
private static final Logger log = LoggerFactory.getLogger(MessageLogStatus.class);
static final String A = "alpha";
static final String B = "beta";
static final String C = "gamma";
public MessageLogStatus()
{
super();
final List<String> keys = Arrays.asList(A, B, C);
final List<Map<String,Integer>> data = Arrays.asList
(
map(A, 1).put(B, 11).put(C, 21).toMap(),
map(A, 2).put(B, 12).put(C, 22).toMap(),
map(A, 3).put(B, 13).put(C, 23).toMap(),
map(A, 4).put(B, 14).put(C, 24).toMap(),
map(A, 5).put(B, 15).put(C, 25).toMap(),
map(A, 6).put(B, 16).put(C, 26).toMap(),
map(A, 7).put(B, 17).put(C, 27).toMap(),
map(A, 8).put(B, 18).put(C, 28).toMap(),
map(A, 9).put(B, 19).put(C, 29).toMap()
);
// Using a DefaultDataTable
ISortableDataProvider<Map<String,Integer>,String> dataProvider = new SortableDataProvider<Map<String,Integer>,String>()
{
private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
public Iterator<Map<String,Integer>> iterator(long first, long count)
{
int start = Math.max(0, (int) first);
int end = Math.min(data.size(), start + (int) count);
return data.subList(start, end).iterator();
}
public long size()
{
return data.size();
}
public IModel<Map<String,Integer>> model(Map<String,Integer> object)
{
return new CompoundPropertyModel<Map<String,Integer>>(object);
}
};
List<PropertyColumn<Map<String,Integer>,String>> columns = new ArrayList<PropertyColumn<Map<String,Integer>,String>>();
for (String key : keys)
{
columns.add
(
new PropertyColumn<Map<String,Integer>, String>(Model.of(key.toUpperCase()), key)
{
private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
#Override
public void populateItem(Item<ICellPopulator<Map<String, Integer>>> item, String componentId,
IModel<Map<String, Integer>> rowModel)
{
super.populateItem(item, componentId, rowModel);
Map<String, Integer> entity = rowModel.getObject();
String px = getPropertyExpression();
PropertyModel<Object> propModel = new PropertyModel<Object>(rowModel, px);
log.info("Add Label to Cell: PropEx="+px+", Value="+propModel.getObject()+", entity="+entity);
}
}
);
}
//
// Wicket: <table wicket:id="dataTable"></table>
//
DataTable<Map<String,Integer>,String> dataTable =
new DataTable<Map<String,Integer>,String>("dataTable", columns, dataProvider, 5)
{
private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
#Override
protected Item<IColumn<Map<String, Integer>, String>> newCellItem(final String id, final int index,
final IModel<IColumn<Map<String, Integer>, String>> model)
{
final Item<IColumn<Map<String,Integer>, String>> cell = super.newCellItem(id, index, model);
cell.add
(
new AjaxEventBehavior("onclick")
{
private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
#SuppressWarnings("unchecked")
#Override
protected void onEvent(AjaxRequestTarget target)
{
if ( (cell.size() > 0) && (cell.get(0) instanceof Label) )
{
Label cellLabel = (Label) cell.get(0);
PropertyModel<Integer> cellLabelModel = (PropertyModel<Integer>) cellLabel.getInnermostModel();
String property = cellLabelModel.getPropertyExpression();
Integer value = cellLabelModel.getObject();
Map<String, Integer> entity = (Map<String,Integer>) cellLabelModel.getInnermostModelOrObject();
log.info("OnClick: Index="+index+", PropEx="+property+", Value="+value+", Entity="+entity);
}
}
}
);
return cell;
}
};
dataTable.addBottomToolbar(new NavigationToolbar(dataTable));
dataTable.addTopToolbar(new HeadersToolbar<String>(dataTable, null));
add(dataTable);
}
// Make building the data structure a little more fun :)
private MapBuilder<String, Integer> map(String key, Integer value)
{
return new MapBuilder<String, Integer>().put(key, value);
}
private static class MapBuilder<K, V>
{
Map<K, V> map = new HashMap<K, V>();
MapBuilder<K, V> put(K key, V value)
{
map.put(key, value);
return this;
}
Map<K, V> toMap()
{
return map;
}
}
}
Output
OnClick: Index=0, PropEx=alpha, Value=5, Entity={gamma=25, alpha=5, beta=15}
OnClick: Index=1, PropEx=beta, Value=15, Entity={gamma=25, alpha=5, beta=15}
OnClick: Index=2, PropEx=gamma, Value=25, Entity={gamma=25, alpha=5, beta=15}

Key comparisons for Linq GroupBy using Default EqualityComparer

I'm trying to do a Linq GroupBy on some objects using an explicit key type. I'm not passing an IEqualityComparer to the GroupBy, so according to the docs:
The default equality comparer Default is used to compare keys.
It explains the EqualityComparer<T>.Default property like this:
The Default property checks whether
type T implements the
System.IEquatable<T> generic interface
and if so returns an
EqualityComparer<T> that uses that
implementation.
In the code below, I'm grouping an array of Fred objects. They have a key type called FredKey, which implements IEquatable<FredKey>.
That should be enough to make the grouping work, but the grouping is not working. In the last line below I should have 2 groups, but I don't, I just have 3 groups containing the 3 input items.
Why is the grouping not working?
class Fred
{
public string A;
public string B;
public FredKey Key
{
get { return new FredKey() { A = this.A }; }
}
}
class FredKey : IEquatable<FredKey>
{
public string A;
public bool Equals(FredKey other)
{
return A == other.A;
}
}
class Program
{
static void Main(string[] args)
{
var f = new Fred[]
{
new Fred {A = "hello", B = "frog"},
new Fred {A = "jim", B = "jog"},
new Fred {A = "hello", B = "bog"},
};
var groups = f.GroupBy(x => x.Key);
Debug.Assert(groups.Count() == 2); // <--- fails
}
}
From MSDN
If you implement IEquatable, you should also override the base class implementations of Object::Equals(Object) and GetHashCode() so that their behavior is consistent with that of the IEquatable::Equals method. If you do override Object::Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. This ensures that all invocations of the Equals() method return consistent results.
add this to FredKey and it should work
public override int GetHashCode()
{
return A.GetHashCode();
}
Here is a complete example with a Fiddle. Note: the example differs slightly from the question's example.
The following implementation of IEquatable can act as the TKey in GroupBy. Note that it includes both GetHashCode and Equals.
public class CustomKey : IEquatable<CustomKey>
{
public string A { get; set; }
public string B { get; set; }
public bool Equals(CustomKey other)
{
return other.A == A && other.B == B;
}
public override int GetHashCode()
{
return string.Format("{0}{1}", A, B).GetHashCode();
}
}
public class Custom
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
public static void Main()
{
var c = new Custom[]
{
new Custom {A = "hello", B = "frog" },
new Custom {A = "jim", B = "jog" },
new Custom {A = "hello", B = "frog" },
};
var groups = c.GroupBy(x => new CustomKey { A = x.A, B = x.B } );
Console.WriteLine(groups.Count() == 2);
}

Resources