Add array from list into a new set in Java - java

I want to get the model numbers from the list only
['brand: Samsung, model number: VA2210-MH, size: 21.5', 'brand: Philipe, model number: 244E1SB, size: 21.5']
And I set create attributes and getter and setter of all attributes(only model number will be shown) in Monitor
public class Monitor{
public String brand;
public String modelNumber;
public double size;
public Monitor(String brand, String modelNumber, double size){
this.brand = brand;
this.modelNumber = modelNumber;
this.size = size;
}
public void setModelNumber(String amodelNumber){
modelNumber = amodelNumber;
}
public String getModelNumber(){
return modelNumber;
}
}
so I create a list and add the information into the list
and a method to create a set with model number by the method modelNumberSet()
import java.util.*;
public class ComputerShop{
private List<Monitor> monitorList = new ArrayList<>();
public void addMonitor(String brand, String modelNumber, double size){
Monitor newMonitor = new Monitor(brand, modelNumber, size);
monitorList.add(newMonitor);
}
public Set<Monitor> modelNumberSet(){
Set<Monitor> NewSet = new HashSet<>();
for (Monitor m : monitorList) {
NewSet.add(m.getModelNumber());
}
return NewSet;
}
}
I hope the model number will be added into a new set, the output looks like
[VA2210-MH, 244E1SB]
So I use for loop to incase I will add more information in the future, but the error comes out when I use add(). Why the array cannot be added into the new set? Am I using the wrong function?
Is there a better solution I should use?

Change Set<Monitor> to Set<String>. You are adding model numbers to the set and their types are String. You are trying to put a String where a Monitor is expected. Square peg in a round hole.
Fix the modelNumberSet() method as follows:
public Set<String> modelNumberSet(){
Set<String> newSet = new HashSet<>();
for (Monitor m : monitorList) {
newSet.add(m.getModelNumber());
}
return newSet;
}

Related

Access existing objects in another class Java

I'm using Bluej to create a basic Java project which I'm not supposed to use Arrays or collections and I'm having difficulty to understand how I can display a specific existing object from another class.
Here's my Class Listing:
public class Listing
{
//name of the listing
private String listingName;
//number of the listing
private int listingNumb = 0;
//price of the listing
private double price;
//checks if the listing is for sale
private boolean isForSale;
//House data of the listing
private HouseData housedata;
private boolean isSold;
/**
* Constructor for objects of class Listing
*/
public Listing(String newListing, double newPrice, HouseData housedata)
{
listingName = newListing;
listingNumb++;
price = newPrice;
isForSale = false;
this.housedata = housedata;
isSold = false;
}
//Returns if the listing is sold or not
public boolean getIsSold(){
return isSold;
}
//Set the listing as sold or not
public boolean setIsSold(){
isSold = true;
return isSold;
}
//returns if the listing is for sale or not
public boolean getIsForSale(){
return isForSale;
}
//set the listing for sale or not
public boolean setIsForSale(boolean sale){
isForSale = sale;
return isForSale;
}
//Return the price of the listing
public double getPrice(){
return price;
}
//Return the listing name
public String getListingName(){
return listingName;
}
//Return the listing number
public int getListingNumb(){
return listingNumb;
}
//checks if the listing is located at a specific city
public boolean isLocatedAt(String city){
if(housedata.city() == city){
return true;
}else{
return false;}}
public double getSurface(){
return housedata.getSurface();
}
}
Here's my Class RealEstateAgency where I want to call the objects from class Listing:
public class RealEstateAgency
{
//the name of the agency
private String agency;
//the amount of profit from property sales
private double profit;
//identifies the seller of the property
private String agent;
private boolean isOnSale;
//the name of the property
private Listing listing;
private Listings lists;
/**
* Constructor for objects of class RealEstateAgency
*/
public RealEstateAgency()
{
profit = 0;
}
//Returns total profit from agency
public double getProfit(){
return profit;
}
//Display listing that is on sale with the name of one city
//This is the method where I want to retrieve all listing existing objects that have a specific city
public void displayListingsLocatedAt(String city){
System.out.println("The available listings for this city are: " + ;
}
}
And this is the main class where I initialize the objects:
public class AppStart
{
public static void main(String[] args)
{
Address address1 = new Address("street", "postalcode", "city");
HouseData house1 = new HouseData(2, true, address1);
Listing list1 = new Listing("Imóvel1", 9000, house1);
Listing list2 = new Listing("Imóvel2", 9000, house1);
Listing list3 = new Listing("Imóvel3", 8000, house1);
RealEstateAgency real1 = new RealEstateAgency();
}
}
Thing is, you are creating these Listing classes that are not part of any class. After your initialization of RealEstateAgency, there should be a call something like this:
real1.listing = list1;
(I think it will work, altough it's set to private...)
It will add your list1 into your real1 object, then able to call it's getters like
real1.listing.getPrice();
Or something like this.
Thing is, you can reach the class variables by the dot selector, even if that variable is an another class, in that case you can use your selectors as a chain.
However, with your current setup, you can hold only 1 Listing in each RealEstateAgency, plus I don't think "Listings lists" works. Either that's an errorenous code, or part of your code is missing that explains that line.
An another note, your listingNumb currently does nothing, since it's held by the Listing class, so after calling the constructor of a Listing, it will be just set to 1 all the time.

How to Create Immutable Class with List<String> Element in class

Immutable Class with List
package com.text.immutable;
import java.util.Collections;
import java.util.List;
// An immutable class Student
public final class Student
{
final String name;
final int regNo;
final List<String> courses; // want to make Immutable
public Student(String name, int regNo, List<String> courses)
{
this.name = name;
this.regNo = regNo;
this.courses = Collections.unmodifiableList(courses);
}
public String getName()
{
return name;
}
public int getRegNo()
{
return regNo;
}
public List<String> getCourses() {
return courses;
}
}
Testing Immutable Class to Break Immutability
package com.text.immutable;
import java.util.ArrayList;
import java.util.List;
class ImmutablityTest
{
public static void main(String args[])
{
List<String> courses = new ArrayList<String>();
courses.add("java");
courses.add("spring");
courses.add("hibernate");
courses.add("rest");
Student s = new Student("ABC", 101, courses);
System.out.println("Before Update List");
System.out.println(s.getName());
System.out.println(s.getRegNo());
System.out.println(s.getCourses());
courses.add("Hibernate"); // Able to Change which affect final OutCome
//s.getCourses().add("SpringBoot"); // giving Exception in thread "main" java.lang.UnsupportedOperationException
System.out.println("After Update List");
System.out.println(s.getName());
System.out.println(s.getRegNo());
System.out.println(s.getCourses());
}
}
Output is
Before Update List
ABC
101
[java, spring, hibernate, rest]
After Update List
ABC
101
[java, spring, hibernate, rest, Hibernate]
why and how this new Course element added into the List as its from Client Side can be added up any time so how we can fix this issue as this immutable class should not allow to modifying after once created
this.courses = Collections.unmodifiableList(courses);
That creates, as the name says, an unmodifiable list. But that is just a view on the original list. Thus changes to that original list become visible in your "unmodifiable" view.
When in doubt: clone your list, like:
this.courses = new ArrayList<>(courses);
And then ensure that your getter does:
return Collections.unmodifiableList(courses);
Not the best in context of memory, but works:
// An immutable class Student
public final class Student
{
final String name;
final int regNo;
final List<String> courses; // want to make Immutable
public Student(String name, int regNo, List<String> courses)
{
this.name = name;
this.regNo = regNo;
this.courses = new ArrayList(courses);
}
public String getName()
{
return name;
}
public int getRegNo()
{
return regNo;
}
public List<String> getCourses() {
return new ArrayList(courses);
}
}
On input (in constructor) you create copy of list and on output (in getter) you create copy.
read about immutableLists and you'll find that an Immutable and Unmodifiable Are Not the Same.
I guess (from your question) you are expecting an unmodifiable list which you simply don't create...
see this answer for a proper solution
With Collections.unmodifiableList, it creates a wrapper around the original list and that wrapper object is unmodifiable. The original list can still be updated.
So, in order for the List<String> courses list to be immutable, you can use Apache collection common library.
List<String> immutableList =
com.google.common.collect.ImmutableList.of("Geeks", "For","Geeks");
ImmutableList has overridden the List.add method to always throw exception java.lang.UnsupportedOperationException
Second alternative is to create the list inside the constructor itself.
public Student(String name, int regNo, String... args)
{
this.name = name;
this.regNo = regNo;
courses = (List)Arrays.asList(args);
}
And call it like this :
Student s = new Student("ABC", 101, "a","a","a","a");

How can I return int and string attributes in a single Java method?

I would like to return the values of all the attributes from the BaseballPlayer class. The method that needs to do this must be the public string getBaseballPlayer(int i) method (because I need to reference this method inside getBaseballPlayers() to return all the values as an arraylist of strings) I'm having trouble doing this because all the attributes have different datatypes (int, String, Height).
I've tried doing this:
public String getBaseballPlayer(int i){
ArrayList <String> bArray = new ArrayList <String>();
bArray.add(getHometown());
bArray.add(getState());
bArray.add(getHighSchool());
bArray.add(getPosition());
However, it only works for the string methods, and doesn't necessarily return the actual values but rather the get methods for each string attribute.
public class BaseballPlayer extends Player implements Table {
private int num;
private String pos;
public BaseballPlayer( int a, String b, String c, int d,
String e, String f, String g, Height h){
super(a,ft,in,c,d,e,f,ht);
num = a;
pos = b;
}
public BaseballPlayer(){}
//Returns the value of a specific attribute. The input parameter start
with 0 for the first attribute, then 1 for the second attribute and so
on.
//you can use getBaseballPlayer(int i) in getBaseballPlayers( ) with a for
loop getting each getBaseballPlayer(int i).
public String getBaseballPlayer(int i){
ArrayList <String> bArray = new ArrayList <String>();
bArray.add(getHometown());
bArray.add(getState());
bArray.add(getHighSchool());
bArray.add(getPosition());
return (bArray);
}
//Returns the value of all attributes as an ArrayList of Strings.
public ArrayList <String> getBaseballPlayers(){
}
I'm just looking for the simplest way to return each attributes value, then using that method return each value as an arraylist of strings in another method.
It is not a good practice to return the whole object as one String. Unless and otherwise, you are forced to do this, do not try and do.
Well, if your requirement can't be changed, and if you want everything from the Baseball object to be in one string, you can concatenate all the parameters with a delimiter like ":".
Eg:
public String getBaseballPlayer(int i){
return getHometown() + ":" + getState() + ":" +getHighSchool() + ":" + getPosition();
}
On the invoking side, you can get the individual values from this String using "split()" method of String.
What you want to do, if you want to do it perfectly, is what Gson is for. It rides on top of simple JSON and can encode arbitrary classes, data structures, and other types into JSON such that you can reconstruct those objects easily from the JSON representation. It's easy to use considering how powerful what it does really is.
It's even easier to use regular JSON if you don't need to encode types that JSON won't handle by itself. In your case, it seems that this could be good enough. The great thing about JSON is that it's a standard. You don't have to choose an encoding scheme, and you already have libraries written in any language you can think of that can read your String'ified data.
Here's an example that roughly follows what your code is doing:
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
public class BaseballPlayer {
private String name;
private String hometown;
private String state;
private int age;
private double height;
private String position;
static ObjectMapper mapper = new ObjectMapper();
public BaseballPlayer( String name, String hometown, String state, int age, double height, String position) {
this.name = name;
this.hometown = hometown;
this.state = state;
this.age = age;
this.height = height;
this.position = position;
}
public void setName(String name) {
this.name = name;
}
public void setHometown(String hometown) {
this.hometown = hometown;
}
public void setState(String state) {
this.state = state;
}
public void setAge(int age) {
this.age = age;
}
public void setHeight(float height) {
this.height = height;
}
public void setPosition(String position) {
this.position = position;
}
public String toString() {
return String.format("Name: %s from %s, %s (height: %.1f)", name, hometown, state, height);
}
public BaseballPlayer(){}
// Turn a BaseballPlayer object into a String
public String getAsJSON() {
Map<String, Object> info = new HashMap<>();
info.put("name", name);
info.put("hometown", hometown);
info.put("state", state);
info.put("age", age);
info.put("height", height);
info.put("position", position);
try {
return mapper.writeValueAsString(info);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// Here's the method you ask for. I don't know what 'i' is supposed
// to do, since we're in the class for a single baseball player. You
// could create a class that contains a list of baseball players, but
// why not just use a List by itself, as I've done.
public String getBaseballPlayer(int i) {
return getAsJSON();
}
// Turn a list of BaseballPlayer objects into a list of Strings
public static List<String> playersToStrings(List<BaseballPlayer> players) {
List<String> r = new ArrayList<>();
for (BaseballPlayer player : players) {
r.add(player.getAsJSON());
}
return r;
}
// Turn a list of Strings into a list of BaseballPlayer objects
public static List<BaseballPlayer> stringsToPlayers(List<String> playerStrings) {
List<BaseballPlayer> r = new ArrayList<>();
for (String playerString : playerStrings) {
try {
BaseballPlayer player = mapper.readValue(playerString, BaseballPlayer.class);
r.add(player);
} catch (IOException e) {
e.printStackTrace();
}
}
return r;
}
public static void main(String... args) {
// Create a list of BaseballPlayer objects and print them
List<BaseballPlayer> players = new ArrayList<>();
players.add(new BaseballPlayer("Joe", "Boston", "MA", 25, 6.1, "First Base"));
players.add(new BaseballPlayer("Sam", "San Francisco", "CA", 28, 5.8, "Pitcher"));
players.add(new BaseballPlayer("Kelly", "Chicago", "IL", 32, 6.4, "Catcher"));
System.out.println(players);
// Convert the list to a list of Strings and print the list
List<String> playerStrings = playersToStrings(players);
System.out.println(playerStrings);
// Convert the Strings back into BaseballPlayer objects and print them
players = stringsToPlayers(playerStrings);
System.out.println(players);
}
}
and here's the resulting output:
[Name: Joe from Boston, MA (height: 6.1), Name: Sam from San Francisco, CA (height: 5.8), Name: Kelly from Chicago, IL (height: 6.4)]
[{"hometown":"Boston","name":"Joe","state":"MA","position":"First Base","age":25,"height":6.1}, {"hometown":"San Francisco","name":"Sam","state":"CA","position":"Pitcher","age":28,"height":5.8}, {"hometown":"Chicago","name":"Kelly","state":"IL","position":"Catcher","age":32,"height":6.4}]
[Name: Joe from Boston, MA (height: 6.1), Name: Sam from San Francisco, CA (height: 5.8), Name: Kelly from Chicago, IL (height: 6.4)]
Here, each player is turned into JSON individually. A few more lines of code, and you could turn an array of Baseball Player objects into a single String.
If this JSON-only solution isn't good enough for you, check out Gson. It can preserve all Java types. It just takes a bit more setup to describe how each of your objects should be turned into JSON and back.

Java array list returning 0 value

I have created a class like this, which contains a bunch of arraylist as you can see. I've been setting the array with the methods add.. and then retrieving it with get.., when i tried to System.out.println numberofcitizen for example it is returning 0. Note that i have instantiated the class in another class to set the values.
public int numberOfCitizen;
private final ArrayList<Integer> citizenid = new ArrayList<>();
private final ArrayList<String> citizenName = new ArrayList<>();
private final ArrayList<Integer> citizenWaste = new ArrayList<>();
private final ArrayList<Float> longitude = new ArrayList<>();
private final ArrayList<Float> latitude = new ArrayList<>();
private final ArrayList<String> address = new ArrayList<>();
public void working() {
System.out.println("executing fine");
}
public void setnoOfcit(int number) {
this.numberOfCitizen = number;
}
public int getnumber() {
return this.numberOfCitizen;
}
public void addCitizenId(int citizen) {
citizenid.add(citizen);
}
public int getCitizenid(int i) {
int citId = citizenid.get(i);
return citId;
}
public void addCitizenName(String citizenname) {
citizenName.add(citizenname);
}
public String getCitizenName(int i) {
return citizenName.get(i);
}
public void addCitizenWaste(int waste) {
citizenWaste.add(waste);
}
public int getCitizenWaste(int i) {
return citizenWaste.get(i);
}
public void addLatitude(float lat) {
latitude.add(lat);
}
public float getLat(int i) {
return latitude.get(i);
}
public void addlng(float lng) {
longitude.add(lng);
}
public float getlng(int i) {
return longitude.get(i);
}
com.graphhopper.jsprit.core.problem.VehicleRoutingProblem.Builder vrpBuilder = com.graphhopper.jsprit.core.problem.VehicleRoutingProblem.Builder.newInstance();
public void runVPRSolver() {
System.out.println(numberOfCitizen);
System.out.println(getCitizenName(0));
//create a loop to fill parameters
Probable source of problem :
numberOfCitizen is a member attribute that you seem to never change. If you want it to represent the number of elements in your lists, either use citizenName.size() or increment the value of numberOfCitizen in one of the add methods.
Design flaw :
Your design takes for granted that your other class always use that one properly. Anytime you or someone uses that class, he must make sure that he add every single element manually. This adds code that could be grouped inside your class, which would be cleaner and easier to maintain.
So instead of several add method like this :
addCitizenid();
addCitizenName();
addCitizenWaste();
addLongitude();
addLatitude();
addAddress();
Design an other Citizen class which will contain those elements, and use a single list of instances of that class. That way you can use only one method :
private List<Citizen> citizenList = new ArrayList<>();
public void addCitizen(Citizen c) {
/*Add element in your list*/
citizenList.add(c);
}
This programming methodology is called "Encapsulation" which you can read about here
You need to increment numberOfCitizen in your add methods. For example:
public void addCitizenId(int citizen){
citizenid.add(citizen);
numberOfCitizen++;
}
I would also suggest encapsulating your variables into Objects, so create a citizen class:
public class Citizen {
private Integer id;
private Integer name;
private Integer waste;
}
And change your variable to an ArrayList of objects:
ArrayList<Citizen> citizens;

How to use setter to set a value to a attribute which is a private array?

I had made an array of roll number so how to give a user input using setter to the private attribute which is roll number.
I made an object of class Students which is a students and tried thisstudents.for(int i=0;i<n;i++)
{(setRollno[i](sc.next()))};
But it did not worked.
class Students{
private String[] rollno = new String[1000];
private int[] intel = new int[1000];
private int[] type = new int[1000];
private String[] name = new String[1000];
public void setRollno(String[] rollno) {
this.rollno = rollno;
}
public void setName(String[] name) {
this.name = name;
}
public void setIntel(int[] intel) {
this.intel = intel;
}
public void setType(int[] type) {
this.type = type;
}
public String[] getRollno() {
return rollno;
}
public String[] getName() {
return name;
}
public int[] getIntel() {
return intel;
}
public int[] getType() {
return type;
}
}
Possibly I'm misunderstanding the aim of your data structure, but it sounds like you're trying to create a collection of student data all in one place. If this is the case then I'd strongly recommend creating a Student class which represents just one student at a time, and then using one or more standard types from the Java Collection Framework to make it easy to find a particular student.
For example, your Student class could simply contain fields which hold the roll number, name, and other data for one student. Then, assuming that you'll want to find students quickly by roll number you could create a Map<String, Student> where the map keys are roll numbers and the corresponding map value is the student who has that roll number. Simply put a new Student object into the Map after constructing it. If you need to find students by name then you could create a Map<String, Collection<Student>> (because more than one student might have the same name) and put each new Student object into this name map after constructing it.
This is likely to lead to code which is a lot easier to read, maintain, and use than an all-in-one custom collection class such as the one shown in your question.
As a rough code example:
String rollNumber = getNewRollNumber(); // wherever roll numbers come from
String name = getStudentName(); // wherever the name comes from
Student newStudent = new Student(rollNumber, name, etc);
studentsByRollNumber.put(rollNumber, newStudent);
studentsByName.computeIfAbsent(name,
n -> new ArrayList<>(1)).add(student);
Student studentWithParticularRollNumber =
studentsByRollNumber.get("123456");
Collection<Student> studentsWithParticularName =
studentsByName.get("Perry, Fred");
The Map#computeIfAbsent method will create a new ArrayList under the given student name only if no entry already exists under that name, or will fetch the existing list if that name already exists in the map, and then will put the new student into the list.
Within the call to computeIfAbsent the lambda expression t -> new ArrayList<>(1) simply means "take the value of the map key, and whatever it is just create a new ArrayList of size one". It simply guarantees that if there is not already a Collection<Student> stored under the given student name then a new ArrayList<Student> will be created and stored there.
setRollno[i](sc.next()); isn't the good syntax. Your function setRollno take an array of strings as parameter, and change all the array you have. If that's what you want, you must pass an array of Strings as parameter:
If you want to set one specific String in your rollno, you must create another function:
setRollNoAtIndex(int i, String s) {
this.rollno[i] = s;
}
If you need to call this in a loop, you can then simply do:
for(int i=0; i < n ;i++) {
students.setRollNoAtIndex(i, sc.next());
}
As per Berger's comment:
The syntax you were trying to use was probably either:
for(int i=0; i < n ;i++) {
students.getRollno()[i] = sc.next();
}
or
String[] list = new String [1000];
for(int i=0; i < n ;i++) {
list[i] = sc.next();
}
students.setRollno(list);
I think that design of this class is not correct, because we have access to internal arrays directly. Moreover, in that case you do not have to init all these arrays on new instance creation. I offer a little bit another implementation of this class, that I found more suitable for this goal:
final class Students {
private static final int TOTAL = 1000;
private final Map<String, Student> students = new HashMap<>(TOTAL);
public void setName(String rollno, String name) {
getOrCreateStudent(rollno).name = name;
}
public void setIntel(String rollno, int intel) {
getOrCreateStudent(rollno).intel = intel;
}
public void setType(String rollno, int type) {
getOrCreateStudent(rollno).type = type;
}
public Set<String> getRollno() {
return students.keySet();
}
public String getName(String rollno) {
return students.getOrDefault(rollno, Student.NULL).name;
}
public int getIntel(String rollno) {
return students.getOrDefault(rollno, Student.NULL).intel;
}
public int getType(String rollno) {
return students.getOrDefault(rollno, Student.NULL).type;
}
private Student getOrCreateStudent(String rollno) {
Student student = students.get(rollno);
if (student == null)
students.put(rollno, student = new Student(rollno));
return student;
}
private static final class Student {
private static final Student NULL = new Student(null);
private final String rollno;
private int intel;
private int type;
private String name;
public Student(String rollno) {
this.rollno = rollno;
}
}
}

Categories

Resources