How to fill arrays with object parameter based of a value? - java

If I have a certain number of objects which each take multiple parameters, how can I fill an array with one particular parameter for all objects, but have the order of the elements in the array based off another parameter. For example, I have this code:
public CollegeList(double gpa, int act, int sat, String name, String location){
this.gpa = gpa;
this.act = act;
this.sat = sat;
this.name = name;
this.location = location;
if(act/36.0>sat/2400.0){
this.score = 0.6*gpa*25.0+0.4*(act/36.0)*100.0;
}else{
this.score = 0.6*gpa*25.0+0.4*(sat/2400.0)*100.0;
}
this.scoreDistance = Math.abs(this.score-MainActivity.scoreDouble)/MainActivity.scoreDouble;
}
public double getGpa(){
return this.gpa;
}
public int getAct(){
return this.act;
}
public int getSat(){
return this.sat;
}
public String getName(){
return this.name;
}
public String getLocation(){
return this.location;
}
public double getScore(){
return this.score;
}
public double getScoreDistance(){
return this.scoreDistance;
}
Here, I would like the name parameter for all objects that I may create to populate a String array, but have those names go in ascending order by the double scoreDistance in the array. I'm sorry if the wording of this question is bad, but I hope it makes sense.

1) Create a CollegeList[] or ArrayList<CollegeList> containing the objects you want to sort.
2) Create a Comparator<CollegeList> that compares two CollegeList objects by comparing the scoreDistance. In Java 8 (yes, I know this isn't available for Android, but other readers may find this useful):
Comparator<CollegeList> compareByScoreDistance = (CollegeList a, CollegeList b) -> Double.compare(a.getScoreDistance(), b.getScoreDistance());
In Java 7:
Comparator<CollegeList> compareByScoreDistance = new Comparator<CollegeList>() {
#Override
public int compare(CollegeList a, CollegeList b) {
return Double.compare(a.getScoreDistance(), b.getScoreDistance());
}
};
3) Sort the array or ArrayList using the comparator. If it's an array:
Arrays.sort(theArray, compareByScoreDistance);
If it's an ArrayList, use Collections.sort instead of Arrays.sort.
4) Now you can create the string array by going through the CollegeList[] or ArrayList<CollegeList> and creating an array or ArrayList using getName(). For example, if your list is an ArrayList, then you can use this from #user3717646's answer:
for (CollegeList collegeList : theList) {
nameList.add(collegeList.getName());
}
Or using Java 8:
String[] names = theList.stream().map(CollegeList::getName).toArray(String[]::new);
or
ArrayList<String> names = new ArrayList<>(theList.stream().map(CollegeList::getName).collect(Collectors.toList()));
EDIT: Code has now been tested, and several mistakes fixed.

Try Using ArrayLists. Following sample code is given for two CollegeList objects.
ArrayList<CollegeList> collegeLists=new ArrayList<>(); // To store all CollegeList Objects
ArrayList<String> nameList=new ArrayList<>(); // To store Names
CollegeList cl1=new CollegeList(12, 45, 5, "Name1", "Location1");
CollegeList cl2=new CollegeList(12, 45, 5, "Name2", "Location2");
collegeLists.add(cl1);
collegeLists.add(cl2);
for (CollegeList collegeList : collegeLists) {
nameList.add(collegeList.getName());
}
collegeLists stores all CollegeList objects.
then you can get each and every parameter using get methods and put the in to seperate aarraylists.
If you want to sort the arraylist, You can uose Collections.sort(nameList); to do it.

Related

Java Streams - Map two string lines each to one object [duplicate]

This question already has answers here:
Collect successive pairs from a stream
(22 answers)
Closed 1 year ago.
Say we have a text file that contains (product name, price) pairs. Each pair occupies two lines in the text file where the first line corresponds to the product name, and the second line corresponds to the price of that product. We may assume the text file is in the right format (and has an even amount of lines)
Example:
Ice Cream
$3.99
Chocolate
$5.00
Nice Shoes
$84.95
...
Now I have a simple class representing such pairs:
public class Product {
private final String name;
private final int price;
public Product(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return this.name;
}
public int getPrice() {
return this.price;
}
}
We read the file containing the pairs and now have a string array containing all the individual lines. I need to use Streams to map each two lines to one object of type Product.
How can I group two lines each and then map them to a Product? If there is a simple approach, does it still work with parallel streams?
You can make your own Collector which temporarily stores the previous element/string. When the current element starts with a $, the name of the product is stored in prev. Now you can convert the price to a double and create the object.
private class ProductCollector {
private final List<Product> list = new ArrayList<>();
private String prev;
public void accept(String str) {
if (prev != null && str.startsWith("$")) {
double price = Double.parseDouble(str.substring(1));
list.add(new Product(prev, price));
}
prev = str;
}
public List<Product> finish() {
return list;
}
public static Collector<String, ?, List<Product>> collector() {
return Collector.of(ProductCollector::new, ProductCollector::accept, (a, b) -> a, ProductCollector::finish);
}
}
Since you need to rely on the sequence (line with price follows line with name), the stream cannot be processed in parallel. Here is how you can use your custom collector:
String[] lines = new String[]{
"Ice Cream", "$3.99",
"Chocolate", "$5.00",
"Nice Shoes", "$84.95"
};
List<Product> products = Stream.of(lines)
.sequential()
.collect(ProductCollector.collector());
Note that your prices are not integers which is why I used a double to represent them properly.
if you have the items in an array you can use vanilla java with an intStream and filter on even values and then in the next map you can use index and index+1. Maybe have look here

Sort ArrayList of ArrayList of objects

I'm having trouble using a Comparator to sort an ArrayList of ArrayList of Objects, i have this simple code:
ArrayList<ArrayList<Object>> office = new ArrayList<ArrayList<Object>>(15);
ArrayList<Object> objectArrayList1 = new ArrayList<Object>(15);
objectArrayList1.add("Denver");
objectArrayList1.add(108);
office.add(objectArrayList1);
ArrayList<Object> objectArrayList2 = new ArrayList<Object>(15);
objectArrayList2.add("NewYork");
objectArrayList2.add(109);
office.add(objectArrayList2);
[[Denver, 118], [NewYork, 109]]
How do i sort it by the numbers or the String?
You're going to have to override the compare() method using Collections.sort() and create a new Comparator. Because you are using object it's going to be difficult to write a generic sorter. Here is an example for a sort on an arrayList of a custom object
Collections.sort(finalList, new Comparator<FinalAccount>()
#Override
public int compare(FinalAccount o1, FinalAccount o2) {
Double o1Double = o1.reductionAmount;
Double o2Double = o2.reductionAmount;
return o1Double.compareTo(o2Double);
}
});
but again this is for only one type of object, generics confuse things because you may have any type of object.
Your data model is a little off.
Assuming you want to store data for cities and their associated "scores" to sort on, you need a list of objects that can contain both the city and its score. This entity has to be comparable.
For e.g
Team implements Comparable {
String city;
int score;
Team(String city, int score) {
this.city = city;
this.score = score;
}
#Override
public int compareTo(Team other) {
return this.score - other.score;
}
}
Now you do can sort an ArrayList of Teams
List<Team> teams = Arrays.asList(new Team("New York", 101), new Team("Denver", 108));
Collections.sort(teams); // sorts in-place
You can also use custom comparators like
Collections.sort(teams, new Comparator<Team>(){
#Override
public int compare(Team t1, Team t2) {
return t1.city.compareTo(t2.city);
}
})

Associative arrays in Java

I'm from a PHP background, and I'm trying to create a multidimentional array with a difficulty in understanding the Java way of doing things. I thought this could be achieved using JSON and the GSON library, but I'm failing to understand how this is done having followed several tutorials online.
Here is what I'm after in PHP, how can I achieve the same thing in Java?
function creatCars($id) {
$aCars = array(
0 => array(
'name' => 'vauxhall',
'doors' => 5,
'color' => 'black',
),
1 => array(
'name' => 'peogeot',
'doors' => 3,
'color' => 'red',
),
);
return $aCars[$id];
}
function printFirstCarName($sName) {
$aCar = createCars(0);
echo $aCars['name'];
}
//prints "vauxhall"
printFirstCarName();
Arrays in PHP are not the same as arrays in Java. Here are the differences:
PHP:
PHP arrays are actually dictionaries. They store a value for each key, where a key can be an integer or a string. If you try to use something else as a key, it will be converted to either an integer or a string.
Java:
Arrays in Java
Java arrays are not associative in the same way as they are in PHP. Let's start with one-dimensional arrays in Java:
A one-dimensional array in Java has a fixed length (that cannot be changed) and each key is an integer in the range of 0 to array.length - 1. So keys, actually called indexes, are always integers. Also, in Java, if you have an array with the keys 2 and 4, you also have (at least) the keys 0, 1 and 3, because the length has to be at least 5 then.
Arrays in Java also have exactly one type and each values in the array can only be of the specified type. Neither size nor type of an array can be changed.
When you create an array in Java, you have two possibilities:
explicitly specify the length when creating the array
String[] words = new String[4];
The variable words now holds an array of type String with the length a length of 4. The values of all indexes (0 to 3) are initially set to null.
specify elements when creating the array
String[] words = new String[] {"apple", "banana", "cranberry"};
The variable words now holds an array of type String with a length of 3. The elements contained are as specified with the first element bound to index 0, the second element bound to index 1, and so on.
You can think of multi-dimensional arrays as of an array which holds arrays. A 2-dimensional array could look like this:
String[][] twoD = new String[][] {
{"apple", "banana", "cranberry"},
{"car", "ship", "bicycle"}
}
For this twoD[0][2] would be "cranberry" and twoD[1][1] would be "ship". But the number of dimensions of an array does not influence the fact that the keys are integers.
Maps in Java:
Even though Java has no built-in language construct for associative arrays, it offers the interface Map with various implementations, e.g. HashMap. A Map has a type of which the keys are, and a type of which the values are. You can use maps like this:
HashMap<String, String> map = new HashMap<String, String>();
map.put("car", "drive");
map.put("boat", "swim");
System.out.println("You can " + map.get("car") + " a car.");
System.out.println("And a boat can " + map.get("boat") + ".");
This will output:
You can drive a car.
And a boat can swim.
The answer:
The one-to-one way in Java
The answer to your question is that it is not really possible in a reasonable way becasue some of your values are strings, and some are integers. But this would be the most similar code to your PHP array:
//array of HashMaps which have Strings as key and value types
HashMap<String, String>[] cars = new HashMap<String, String>[2];
HashMap<String, String> first = new HashMap<String, String>();
first.put("name", "vauxhall");
first.put("doors", "5");
first.put("color", "black");
HashMap<String, String> second = new HashMap<String, String>();
second.put("name", "peogeot");
second.put("doors", "3");
second.put("color", "red");
//put those two maps into the array of maps
cars[0] = first;
cars[1] = second;
This solution is not very handy, but it is the way that comes closest to your given datastructure.
The cuter way in Java
It seems however, that each of the entries in your PHP array has exactly three properties: name, doors and color. In this case, you may want to create a class Car with these member variables, and store them in an array. This would look like this:
public class Car {
//member variables
public String name;
public int doors;
public String color;
//constructor
public Car(String name, int doors, String color) {
this.name = name;
this.doors = doors;
this.color = color;
}
}
Now, when you have the class Car, you can create an array of all your cars like this:
Car[] cars = new Car[2];
cars[0] = new Car("vauxhall", 5, "black");
cars[1] = new Car("peogeot", 3, "red");
This is the nicer way to do this in Java.
Instead of creating 2D Array you can create 1 class Car
public class Car{
private String carName;
private String color;
private int noOfDoors;
public car(String carName,int door,String color){
this.carName=carName;
this.door=door;
this.color=color;
}
public String getCarName(){
return getCarName;
}
public void setCarName(String carName){
this.carName=carName;
}
// Same getters(getXXX) and setters(setXXX) for other Variables
}
Now create Objects of above class
Car audi=new Car("audi",2,"Black");
Car bmw=new Car("bmw",4,"White");
Now add these to the List<Cars>
List<Car> listOfCars=new ArrayList<Car>();
listOfCars.add(audi);
listOfCars.add(bmw);
Now to Print First Car Name
Car firstCar=listOfCars.get(0);
System.out.println(firstCar.getCarName()); //here getter Method Helped you
I would suggest to get familiar with HashMaps, Maps and ArrayLists. In Java and many other languages is something analogous to a video game cheat.
private static Map<Integer, HashMap<String, String> > carMap = new HashMap<Integer, HashMap<String, String> >();
But in this case you have to understand how would OO principles help you. You can create a class with Car objects and populate a HashMap etc.
class Car {
private String name, colour;....
public Car(){....}
public void setValues(...){....}
}
To achieve better what you want to I would suggest reading this and getting familiar with some design patterns. It's a bit further down the road, but do it for the lulz and seeing what it's out there. Example : http://howtodoinjava.com/2012/10/23/implementing-factory-design-pattern-in-java/
When moving from scripting to strongly typed languages sometimes you have to change your way of thinking too.
Firstly you should create class Car i.e:
public class Car {
enum ColorType {
BLACK, RED;
}
private String name;
private int doors;
private ColorType color;
Car(String name, int doors, ColorType color) {
this.name = name;
this.doors = doors;
this.color = color;
}
public String getName() {
return name;
}
public int getDoors() {
return doors;
}
public ColorType getColor() {
return color;
}
}
And now you can use arrays but better for you will be use ArrayList:
List<Car> cars = new ArrayList<Car>();
cars.add(new Car("vauxhall", 5, BLACK));
cars.add(new Car("peogeot", 3, RED));
for (Car car : cars ) {
System.out.println("Car name is: " + car.getName());
}
It seems what you are trying to achive is an 'array of cars'. So instead of creating an array of arrays, I recommend to literally implement an 'array of cars'.
To do this, I would define the car first, possibly in a different file:
class Car {
//you can make these private and use 'get' and 'set' methods instead
public String name;
public String color;
public int doors;
public Car() {
name = "";
color = "";
doors = 0;
}
public Car(String name, String color, int doors) {
this.name = name;
this.color = color;
this.doors = doors;
}
}
You can use the car structure in an another module like this:
Car[] cars = new Car[100]; //create one hundred cars
cars[11].doors = 4; //make the 12th car's number of doors to 4
You can use more flexible data structures, like Vectors, List, Maps, etc... Search for Java collections, you will find tones of info.
Java is not a loosely typed language, you have to tell the compiler what each variable is going to be. And to store this kind of structured data in Java, you should first declare a class and instantiate objects of that class. Following is how you would achieve the same thing as your PHP code:
class Car {
private String name, color;
private int doors;
Car(String name, int doors, String color) {
this.name = name;
this.doors = doors;
this.color = color;
}
public String getName() {
return this.name;
}
}
public class CarMainClass {
public static void main(String[] args) {
Car[] aCars = new Car[2];
aCars[0] = new Car("vauxhall", 5, "black");
aCars[1] = new Car("peogeot", 3, "red");
System.out.println("First car name is: " + aCars[0].getName());
}
}
Compile using:
javac CarMainClass.java
Then run:
java CarMainClass
You will have to learn the basics of Java first to understand the above code.

Sort List based on index value

This is what i have so far, i'm trying to sort a bunch of List<String>'s based on the value of an index.
LinkedHashSet<List<String>> sorted = new LinkedHashSet<List<String>>();
How do i sort the LinkedHashSet in order from Highest to Lowest index 2 value of the List's?
Example input:
List<String> data1 = Database.getData(uuid);
double price = Double.valueOf(data1.get(2))
data1.add("testval");
data1.add("testval");
data1.add("100.00");
sorted.add(data1);
and on another seperate List:
List<String> data2 = Database.getData(uuid);
double price = Double.valueOf(data2.get(2))
data2.add("anotherval");
data2.add("anotherval");
data2.add("50.00");
sorted.add(data2);
Output of the sorted LinkedHashSet in descending order.
testval testval 100.00
anotherval anotherval 50.00
Sorry if this is confusing, im not sure where to go about sorting like this.
Create a new class to represent you complex objects. There is no need to store multiple values in a list when you can do it in objects.
public class ComplexObject {
private String description1;
private String description2;
private Double value;
public ComplexObject(String description1, String description2, Double value) {
this.description1 = description1;
this.description2 = description2;
this.value = value;
}
public void setDescription1(String description1) {
this.description1 = description1;
}
public String getDescription1() {
return description1;
}
public void setDescription2(String description2) {
this.description2 = description2;
}
public String getDescription2() {
return description2;
}
public void setValue(Double value) {
this.value = value;
}
public Double getValue() {
return value;
}
}
Then add elements to the list and sort it using a new, custom, comparator:
public static void main(String[] args) {
List<ComplexObject> complexObjectList = new ArrayList<ComplexObject>();
//add elements to the list
complexObjectList.add(new ComplexObject("testval","testval",100.00d));
complexObjectList.add(new ComplexObject("anotherval","anotherval",50.00d));
//sort the list in descending order based on the value attribute of complexObject
Collections.sort(complexObjectList, new Comparator<ComplexObject>() {
public int compare(ComplexObject obj1, ComplexObject obj2) {
return obj2.getValue().compareTo(obj1.getValue()); //compares 2 Double values, -1 if less , 0 if equal, 1 if greater
}
});
//print objects from sorted list
for(ComplexObject co : complexObjectList){
System.out.println(co.getDescription1()+" "+co.getDescription2()+" "+co.getValue());
}
}
Output:
testval testval 100.0
anotherval anotherval 50.0
Firstly, you shouldn't use a LinkedHashSet but a TreeSet. LinkedHashSet will retain the insertion order without sorting.
Secondly, you need to initialize your TreeSet with a Comparator that compares based on whichever value of your List is required, that is, if you know the index of the String that will represent a double value in advance. Otherwise I would recommend using custom objects instead of List.
If you decide to use custom objects, you don't necessarily need to initialize your TreeSet with a Comparator as second argument.
Instead, you could have your custom objects implement Comparable, and implement a one-time comparation logic there.
It all depends on whether you only need to sort in a particular order.
Finally, custom objects will require you to override equals and hashCode.
First, and extracted from Oracle's Java reference:
This linked list defines the iteration ordering, which is the order in which elements were inserted into the set
So you can't sort your data just inserting it into the LinkedHashSet.
You may be confusing that set implementation with SortedSet. SortedSet allows you to pass a comparator which will determine the elements order in the data structure.
On the other hand, I don't know whether you chose you List<String> arbitrarily but it seems to me a wiser option to aggregate your the 3 strings as a class attributes. The point is that, if your elements are always going to be 3 elements, being the last one a double value: Why do you need a dynamic structure as a List?
EDIT
Here you have a possible better implementation of what you want:
public class Element
{
public Element(String a, String b, double val) {
this.a = a;
this.b = b;
this.val = val;
}
#Override
public String toString() {
return a + "\t" + b + "\t" + val;
}
public String a;
public String b;
public double val;
}
And you can use this class to store your elements. An example of use:
SortedSet<Element> sorted = new TreeSet<>(new Comparator<Element>() {
#Override
public int compare(Element o1, Element o2) {
return (new Double(o1.val)).compareTo(o2.val);
}
});
sorted.add(new Element("testval", "testval", 100.0));
sorted.add(new Element("anotherval", "anotherval", 50.0));
for(Element el: sorted)
{
System.out.println(el);
}
Note that the comparator is given as an instance of an anonympous inner class implementing Java's Comparator interface.

creating names for linked lists while looping (Java)

I have implemented a simple linked list class and I would now like to use it in a loop. I am wondering how to best assign names to the list in each iteration of the loop.
Essentially I am looping over some integers, and I would like to just give each list the name of that integer, but I cannot say
List i = new List();
right ?
There probably is an easy way to do this, but I m not sure how, and would be grateful for
I think you're confusing the role of variables with the role of collections. From your question I gather that you want to create a list for each index in your loop, and you would like to later be able to access that list by its index:
ArrayList<LinkedList<String>> listOfLists = new ArrayList<LinkedList<String>>();
for(int i = 0; i < 10; i++)
{
LinkedList<String> list = new LinkedList();
listOfLists.add(list);
// do stuff to the list...
}
// access
LinkedList<String> thirdList = listOfLists.get(2); // index 2 = third entry
So you see, the LinkedLists are not named according to the value of i, but you can still access them by a given value of i.
First of all, if if not for learning purposes, it is highly recommended not to implement your own classes for stuff that is already implemented in libraries.
For Lists, you should check out the collection framework: http://docs.oracle.com/javase/tutorial/collections/
I am not sure what you mean by "the name of that integer".
I assume you want to create a List of elements that contain both an integer, and a String representing the name of the value that is hold in the integer.
If it is the case, the best way to do this probably is to create your own Object:
class NamedInteger {
private int value;
private String name;
public NamedInteger(int value, String name) {
this.value = value;
this.name = name;
}
public int getValue() {
return value;
}
public String getName() {
return name;
}
public void setValue(int value) {
this.value = value;
}
public void setName(String name) {
this.name = name;
}
}
The advantage of this method, is that later, if you want to add other information to your object, it is very easy to do so.
And then, just have a List of those objects....
public static void main(String[] args) {
List<NamedInteger> list = new LinkedList<NamedInteger>();
list.add(new NamedInteger(1, "Hello");
...
}

Categories